【问题标题】:Is it possible to pass variable arguments to the include tag in Mako?是否可以将变量参数传递给 Mako 中的 include 标签?
【发布时间】:2013-07-06 01:45:45
【问题描述】:

我有一个 mako 模板 A,其中不止一次包含 mako 模板 B。 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”和“some_other_value”(即:直接编码到 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

所以我的问题是:如何将变量传递给“模板内”的模板?

【问题讨论】:

    标签: include arguments mako function


    【解决方案1】:

    你快到了:

    在 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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-08
    • 2017-07-08
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多