Created
July 8, 2011 19:44
-
-
Save Mikulas/1072648 to your computer and use it in GitHub Desktop.
FriendlyMacros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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