Nette Framework - custom attribute macros
Asked Answered
M

1

8

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?

Maag answered 26/2, 2012 at 19:34 Comment(0)
H
16

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)
Hooke answered 26/2, 2012 at 21:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.