Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass variable arguments to the include tag in Mako?

I have a mako template A that includes mako template B more than once. Mako template B expects certain arguments and I need to set them to different values on include.

In A.mak:

<%include 
    file="/components/B.mak" 
    args="lItems=some_variable, foo='bar'"
    />

<%include 
    file="/components/B.mak" 
    args="lItems=some_other_variable, foo='moooo'"
    />

In B.mak:

<%page args="lItems, foo"/>
%for dItem in lItems:
    etc

Is this sort of thing even possible? I know it will work if I set lItems to 'some_value' and 'some_other_value' (ie: strings coded directly into A.mak) but I want to render A.mak with some_variable = [some,craze,list] and some_other_variable = [some,other,craze,list].

The code above gives me the error:

File ".../mako/runtime.py", line 202, in __str__
    raise NameError("Undefined")
NameError: Undefined

I also tried doing the includes like so:

 <%include 
    file="/components/B.mak" 
    args="lItems=${some_other_variable}, foo='moooo'"
    />

but that's a syntax error...

I tried it using a def also:

${the_B_def(foo='bar',lItems=some_variable)}

and got NameError: Undefined.

So my question is: How can I pass variables to templates 'within' a template?

like image 341
Sheena Avatar asked Jul 08 '13 16:07

Sheena


1 Answers

You were almost there:

In A.mak:

<%include 
    file="/components/B.mak" 
    args="lItems=some_other_variable, foo='moooo'"
    />

In B.mak:

<%page args="lItems, foo"/>
%for dItem in lItems:
    ${foo}
%endfor
like image 168
Régis B. Avatar answered Sep 24 '22 17:09

Régis B.