pythonpyramidchameleontemplate-metal

Chameleon template global variables


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:

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


Solution

  • 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>