[Moin-user] Inject CSS for a particular page

Paul Boddie paul at boddie.org.uk
Sun Jul 21 10:29:27 EDT 2013


On Sunday 21 July 2013 06:36:01 Emmanuel Mayssat wrote:
> I would like to change the style of headers for a subset of pages.
> How can I inject my css stylesheet in a particular page?

For all pages, you can obviously change the "stylesheets" configuration 
setting like this (not tested):

  stylesheets = [("screen", "/css/mystyles.css")]

This augments the stylesheets mentioned in the theme class. See the ThemeBase 
class in MoinMoin/theme/__init__.py for the default definitions.

To affect only some pages, it might be necessary to change the theme code or 
to override a theme with extra code to insert references to styles and 
scripts (with regard to your other question). The html_head method in a theme 
(see the ThemeBase class for details) invokes various other methods to do 
this work and either those methods or html_head itself could be customised to 
insert extra stylesheet and script references in a more flexible fashion.

You might test the page name and insert extra stuff in html_head like this:

  html = ...
  if d["page"].page_name in ("SpecialPage", ...):
      # Refer to .../common/js/special.js
      html.append(self.externalScript("special"))
      # Refer to .../theme/css/mystyles.css
      # (you have to install mystyles.css in the theme's css directory)
      html.append(self._stylesheet_link(True, "screen", "mystyles.css"))
  return '\n'.join(html)

You could make a new theme and override html_head, adding your own stylesheet 
and script references to the string that comes back from the superclass's 
html_head, or you could change the existing themes. I can't think of a nice 
and quick way of doing this for all themes at the moment, but maybe there are 
some tricks that can be used. (You could change html_stylesheets in 
ThemeBase, for example, but that would leave you with local modifications to 
Moin itself, which isn't always desirable.)

Paul




More information about the Moin-user mailing list