Can i use {plink} as n:macro

amik
Member | 124
+
0
-

Hi,
is there any option to write the {plink} as n:macro, for writing presenter links inside components?
I use n:href in all <a> tags, so, in the several occasions when I need to link a presenter from inside a component, do I have to use href=“{plink}”? It kindof makes the way of writing links inconsistent.

Thanks.

Patrik Votoček
Member | 2249
+
+2
-

Hi,
in native Nette package is not supported but you can write your own “phref” n macro.

something like this:

<?php

namespace App;

use Nette,
    Latte,
    Latte\MacroNode,
    Latte\PhpWriter;


class UIMacros extends Latte\Macros\MacroSet
{

    public static function install(Latte\Compiler $compiler)
    {
        $me->addMacro('phref', NULL, NULL, function(MacroNode $node, PhpWriter $writer) use ($me) {
            return ' ?> href="<?php ' . $me->macroLink($node, $writer) . ' ?>"<?php ';
        });
    }


    /********************* macros ****************d*g**/


    /**
     * n:phref="destination [,] [params]"
     */
    public function macroLink(MacroNode $node, PhpWriter $writer)
    {
        $node->modifiers = preg_replace('#\|safeurl\s*(?=\||\z)#i', '', $node->modifiers);
        return $writer->using($node, $this->getCompiler())
            ->write('echo %escape(%modify($_presenter->link(%node.word, %node.array?)))');
    }

}

Comments

greeny:

There is missing

$me = $this

But +1 for solution

5 years ago
amik:

Or simply use $this instead of $me in PHP 5.4+, I prefer this as PHP 5.3 is actually not supported anymore and these $me things are a bit messy.
Anyway, thanks for answer.

5 years ago