[英]Mako: def composition (at render time) not evaluating properly
In the process of understanding Mako (template engine for Python), I started playing with defs constructs. 在理解Mako(Python的模板引擎)的过程中,我开始使用defs构造。
One thing I attempted to do was producing a general "if" statement (say, a JavaScript one) out of def calls. 我试图做的一件事是在def调用中生成一个通用的“ if”语句(例如,一个JavaScript)。 Here's the text:
这是文本:
<%def name="if_statement(x)">if (${x})</%def>
<%def name="sample_condition()">3 == 3</%def>
${if_statement(sample_condition())}
The output is not the expected if(3 == 3)
, but 3 == 3if()
, just like if_statement
's argument was evaluated before any other content of the def and rendered at the front. 输出不是预期的
if(3 == 3)
,而是3 == 3if()
,就像if_statement
的参数在def的任何其他内容之前求值并在最前面呈现一样。
Is this the expected behavior? 这是预期的行为吗? And if yes, why?
如果是,为什么? Also, how could I achieve what I was trying to do?
另外,我如何才能实现自己的目标?
Yes, it's a desired behavior due to buffering , but you can easily get it working as you wanted with use of built-in capture()
function. 是的,由于有缓冲 ,这是一种理想的行为,但是您可以使用内置的
capture()
函数轻松地使其按需工作。 Here's the working example: 这是工作示例:
<%def name="if_statement(x)">if (${x})</%def>
<%def name="sample_condition()">3 == 3</%def>
${if_statement(capture(sample_condition))}
Result is if (3 == 3)
. 结果是
if (3 == 3)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.