是否可以将可变参数传递给Mako中的include标记?

时间:2013-07-08 16:25:46

标签: include arguments mako function

我有一个不止一次包含mako模板B的mako模板A. Mako模板B需要某些参数,我需要在include上将它们设置为不同的值。

在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'"
    />

在B.mak:

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

这种事情甚至可能吗?我知道如果我将lItems设置为“some_value&#39;和&#39; some_other_value&#39; (即:直接编码到A.mak中的字符串)但我想用some_variable = [some,craze,list]some_other_variable = [some,other,craze,list]渲染A.mak。

上面的代码给出了错误:

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

我也试过这样做包括:

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

但那是语法错误......

我也尝试使用def:

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

得到NameError: Undefined

所以我的问题是:如何将变量传递给模板&#39;模板?

1 个答案:

答案 0 :(得分:6)

你快到了那里:

在A.mak:

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

在B.mak:

<%page args="lItems, foo"/>
%for dItem in lItems:
    ${foo}
%endfor