2

I am using Pyramid 1.3 and their templates written in Chameleon. As different pages or templates might need a global stylesheet change per page, I would like to be able to redefine classes of the body tag on the page (of course, I would prefer to do this from the templates).

<body class="${global_variable_or_something_else}"></body>

What I have tried:

  • Using tal:define="global body_class" from the submacros. Doesn't work.
  • I have tried to use <metal:div metal:define-slot="vars">...variable definition...</metal:div> and <metal:div metal:fill-slot="vars">...redefinition...</metal:div>, and redefining the variables in the submacros. It does work, but what if I have several variable definitions in the parent template and I need to redefine only one?

Is there a good way of doing this better with METAL?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Andrey Cizov
  • 695
  • 10
  • 20

1 Answers1

3

Globals in page templates, like globals in Python, can be overridden. So you do the following instead:

<rootelement metal:define-macro="macroname"
             tal:define="global var1 value1; global var2 value2">
    <metal:overrides define-slot="overrides"></metal:overrides>
    <!-- some template code using var1 and var2 -->
</rootelement>

Then use a metal:fill-slot to provide overrides; you can override as few or as many of the variables as you want:

<rootelement metal:use-macro="macroname">
    <metal:overrides fill-slot="overrides"><tal:defines define="global var1 differentvalue" /></metal:overrides>
</rootelement>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • What version of Chameleon did you use to test this solution? – Andrey Cizov Oct 26 '12 at 08:44
  • @AndreyCizov: none; I am making assumptions here based on (a lot of) experience with TAL (zope page templates). It could be that Chameleon's implementation has optimized this codepath away. – Martijn Pieters Oct 26 '12 at 10:22