Nette Framework - custom attribute macros

送分小仙女□ 提交于 2019-12-23 08:57:09

问题


What is the best way to define new attribute macros in the Nette Framework?

Moreover, would it be possible to do so in the config file?


回答1:


define your own macro is really simple in Nette Framework, first you must create MacroSet:

$latte = new Nette\Latte\Engine;
$set = new Nette\Latte\Macros\MacroSet($latte->compiler);

then create new Macro with args:

$set->addMacro('if', 'if (%node.args):', 'endif');

And solution for your second question:

Class MyMacroSet extends Nette\Latte\Macros\MacroSet
{
    public static function install(Nette\Latte\Compiler $compiler)
    {
        $compiler->addMacro('if', 'if (%node.args):', 'endif');
    }
}

and in config.neon you can register your macroSet:

nette.latte:
                setup:
                        - MyMacroSet::install($service->compiler)


来源:https://stackoverflow.com/questions/9456439/nette-framework-custom-attribute-macros

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!