phpnette

Nette Framework - custom attribute macros


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?


Solution

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