【发布时间】:2012-03-16 10:04:07
【问题描述】:
在Nette Framework 中定义新属性宏的最佳方法是什么?
另外,是否可以在配置文件中这样做?
【问题讨论】:
在Nette Framework 中定义新属性宏的最佳方法是什么?
另外,是否可以在配置文件中这样做?
【问题讨论】:
在 Nette Framework 中定义自己的宏非常简单, 首先你必须创建宏集:
$latte = new Nette\Latte\Engine;
$set = new Nette\Latte\Macros\MacroSet($latte->compiler);
然后使用 args 创建新宏:
$set->addMacro('if', 'if (%node.args):', 'endif');
第二个问题的解决方案:
Class MyMacroSet extends Nette\Latte\Macros\MacroSet
{
public static function install(Nette\Latte\Compiler $compiler)
{
$compiler->addMacro('if', 'if (%node.args):', 'endif');
}
}
您可以在 config.neon 中注册您的宏集:
nette.latte:
setup:
- MyMacroSet::install($service->compiler)
【讨论】: