Chameleon template global variables

644 views Asked by At

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?

1

There are 1 answers

2
Martijn Pieters On BEST ANSWER

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>