BLOCK foo ... END和MACRO foo BLOCK .. END之间的区别

时间:2013-10-24 09:19:12

标签: template-toolkit

之间有什么区别
MACRO foo BLOCK
...
END

BLOCK foo
...
END

从历史上看,我总是使用后者,但他们来到了一家新店,他们似乎总是使用前者,并且想知道差异是什么。

1 个答案:

答案 0 :(得分:1)

MACRO块实际上是一个函数,每次调用它时都会被重新评估,并且传递的任何变量都是宏的本地变量。命名块是静态的,或者通过PROCESSINCLUDE调用,其中有关变量范围的常见警告适用。

e.g。

MACRO foo (arg1, arg2) BLOCK;
    ... do something with arg1 and arg2
END;

...

Foo is a [% foo(bar,baz) %]

BLOCK foo
    ... do something with arg1 and arg2
END;

...

Foo is a [% PROCESS foo arg1=bar arg2=baz %]

长话短说,MACRO语法简洁得多,特别是对于外观和行为类似于函数调用的代码。

这是fairly well documented in the fine manual