Skip to content

Instantly share code, notes, and snippets.

@Mikulas
Created July 8, 2011 19:44
Show Gist options
  • Save Mikulas/1072648 to your computer and use it in GitHub Desktop.
Save Mikulas/1072648 to your computer and use it in GitHub Desktop.
FriendlyMacros
<?php
namespace Nette\Latte\Macros;
use Nette,
Nette\Latte,
Nette\Latte\MacroNode,
Nette\Latte\ParseException,
Nette\Utils\Strings as String;
/**
* Macros for Nette\Application\UI.
*
* - @href automatically appends `data-current` attribute link is current
*
* @author Mikuláš Dítě
*/
class FriendlyMacros extends MacroSet
{
public static function install(Latte\Parser $parser, \Nette\Application\UI\Presenter $presenter = NULL)
{
$me = new static($parser);
$me->addMacro('@href', function(MacroNode $node, $writer) use ($me, $presenter) {
$link = String::replace($node->args, '~,.*$~');
return '?> <?php echo $presenter->isLinkCurrent(\'' . $link . '\') ? \'data-current="data-current"\' : \'\'; ?> href="<?php ' . $me->macroLink($node, $writer) . ' ?>"<?php ';
});
}
/**
* {link destination [,] [params]}
* {plink destination [,] [params]}
* n:href="destination [,] [params]"
*/
public function macroLink(MacroNode $node, $writer)
{
return $writer->write('echo %escape(' . ($node->name === 'plink' ? '$presenter' : '$control') . '->link(%node.word, %node.array?))');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment