Registering own macro in config turns off snippets

5 years ago

medhi
Bronze Partner | 189
+
0
-

I would like to set my own macro. So I've created a new class, which extends Latte\Macros\UIMacros:

namespace MyProject\Latte\Macros;

class ThemeMacros extends Latte\Macros\UIMacros
{
    public static function install(Latte\Compiler $compiler)
    {
        $me = new static($compiler);
        $me->addMacro('myMacro', array($me, 'myMacro'));
    }

    /**
     * {myMacro} macro extends {include #block} macro and tests if block exists
     *
     */
    public function macroMyMacro(MacroNode $node, PhpWriter $writer)
    {
        return 'if (isset( $_l->blocks[\'' . $node->args . '\'])) { ' . $this->macroInclude($node, $writer) . '; }';
    }

}

But if I register this class in config:

nette:
    latte:
        macros:
            - MyProject\Latte\Macros\ThemeMacros

MyMacro works fine as expected.

But it causes, that snippets (which is also defined in UIMacros) will stop working. Invalidation of any snippet returns nothing. But Payload object works fine.

Any ideas?

5 years ago

Aurielle
Member | 1283
+
0
-

Wouldn't the problem lie in overloaded install() method? Because other macros will probably stop working if you won't call parent method.

Edit: that being said, by quickly looking at the code I don't see how could you extend the parent method with your own macro registration without duplicating it (no way to get to $me).

Last edited by Aurielle (2014-02-22 12:50)

5 years ago

medhi
Bronze Partner | 189
+
0
-

With parent::install($compiler); is still doesn't work. (I had it there before, but tried to remove it, if it start work).

So what is the recommended way to create own macros (with config registering)?

5 years ago

medhi
Bronze Partner | 189
+
0
-

Problem will be in registration of macro in config. Even registration of empty class

namespace MyProject\Latte\Macros;

class ThemeMacros extends Latte\Macros\UIMacros
{

}

causes that snippets stop working.

5 years ago

David Grudl
Nette Core | 6850
+
0
-

Generated PHP code is the same in both cases?

5 years ago

medhi
Bronze Partner | 189
+
0
-

Problem is in extending from UIMacros. If extending from MacroSet, everything works fine. If extending from UIMacros, in template PHP header is added this:

// prolog MyProject\Latte\Macros\ThemeMacros

// snippets support
if (!empty($_control->snippetMode)) {
    return Nette\Latte\Macros\UIMacros::renderSnippets($_control, $_l, get_defined_vars());
}

Which is probably bad. And you'll tell me to extend MacroSet. OK. But I need to call one of UIMacros macro. How one can do it?

What I need it for? I need alias for {include} macro, to wrap it by isset(). I know, it shouldn't be, but this case is special. My special macro will be used by template-makers, which won't know, if included block exists or not.