Writing custom macros in new revision
In commit (at
github ) of 22.1.2012 the way of writing custom atr-macros changed.
This document comments how to change your code.
Note these changes:
- first argument: no more “@” character in macro name
- second argument (string|callback) is opening code . (Previously was attr code for attr macro)
- third argument (string|callback) remains the same
- (new)fourth argument (string|callback) is attr code
$l->parser→$l->compiler(since 12.1.)
About the macros
- src
- usage
<img n:src="{$image}">Where $image is binary data of image, thus you can use directly Nette\Image instance. - This macro just writes the src attribute
(
src="data:image/jpeg;base64,/9j/4AAQS...). We dont need any open/closing code. Move second argument to foufth position.
- usage
- helper
- usage
<em n:helper="upper">{$text}</em>. - This macro in actually adds no attribute to the tag, so fourth argument is empty. Second and third arg is same.
- usage
Old style is commented.
function templatePrepareFilters($t)
{
$t->registerFilter($l = new Nette\Latte\Engine);
$l = new Nette\Latte\Macros\MacroSet($l->compiler); // in 12.1. $l->parser ---> $l->compiler
/*
$l->addMacro('@src', 'echo \'src="\' , $template->dataStream(%node.word) , \'"\'');
*/
$l->addMacro('src', null, null, 'echo \'src="\' , $template->dataStream(%node.word) , \'"\'');
/*
$l->addMacro('@helper', 'ob_start()', function($node) {
*/
$l->addMacro('helper', 'ob_start()', function($node) {
$w = new \Nette\Latte\PhpWriter($node->tokenizer, $node->args);
return $w->write('echo %modify(ob_get_clean())');
});
}
This article updates custom macros (in cs)
Schmutzka | 17. 3. 2012, 0:44 | comment
If you want to register more your own macros, you can use MacroLoader