【问题标题】:Nette Framework - custom attribute macrosNette Framework - 自定义属性宏
【发布时间】:2012-03-16 10:04:07
【问题描述】:

Nette Framework 中定义新属性宏的最佳方法是什么?

另外,是否可以在配置文件中这样做?

【问题讨论】:

    标签: php nette


    【解决方案1】:

    在 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      • 2011-03-27
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多