Adding own macros after migration
- DavidTheNewbie
- Member | 79
Hi guys. I would like you help. I am migrating from 2.1.9 to 2.2.2
I have my own macro registered like this under 2.1.9. So far so good, fully working:
File: MyMacros
use Nette\Latte\Macros\MacroSet;
use Nette\Latte\Compiler;
use Nette\Latte\MacroNode;
use Nette\Latte\PhpWriter;
class MyMacros extends MacroSet
{
public static function install(Compiler $compiler)
{
$me = new static($compiler);
$me->addMacro('partial', array($me, 'macroPartial'));
}
public function macroPartial(MacroNode $node, PhpWriter $writer)
{
$code = $writer->write(
'Nette\Latte\Macros\CoreMacros::includeTemplate($presenter->findPartialTemplateFile(%node.word), %node.array? + $template->getParameters(), $_l->templates[%var])',
$this->getCompiler()->getTemplateId()
);
if ($node->modifiers) {
return $writer->write('echo %modify(%raw->__toString(TRUE))', $code);
} else {
return $code . '->render()';
}
}
}
With 2.2 in place, I have newly successfully targeted the macro file via:
nette:
latte:
macros:
- MyApp\Templating\Latte\MyMacros
After an attempt to migrate to 2.2 (with fixed namespaces):
File: MyMacros
use Latte\Macros\MacroSet;
use Latte\Compiler;
use Latte\MacroNode;
use Latte\PhpWriter;
class MyMacros extends MacroSet
{
public static function install(Compiler $compiler)
{
$me = new static($compiler);
$me->addMacro('partial', array($me, 'macroPartial'));
}
public function macroPartial(MacroNode $node, PhpWriter $writer)
{
$code = $writer->write(
'\Latte\Macros\CoreMacros::includeTemplate($presenter->findPartialTemplateFile(%node.word), %node.array? + $template->getParameters(), $_l->templates[%var])',
$this->getCompiler()->getTemplateId()
);
if ($node->modifiers) {
return $writer->write('echo %modify(%raw->__toString(TRUE))', $code);
} else {
return $code . '->render()';
}
}
}
Unfortunately, I get:
Fatal Error – Call to undefined method Latte\Macros\CoreMacros::includeTemplate()
The thing with the partial macro is that the CoreMacros::includeTemplate(…) function is gone with 2.2. Is there anything similar to the CoreMacros::includeTemplate(…) function from 2.2?
I have tried to substitute the missing function by:
File: MyMacros
use Latte\Macros\MacroSet;
use Latte\Compiler;
use Latte\MacroNode;
use Latte\PhpWriter;
class MyMacros extends MacroSet
{
public static function install(Compiler $compiler)
{
$me = new static($compiler);
$me->addMacro('partial', array($me, 'macroPartial'));
}
public function macroPartial(MacroNode $node, PhpWriter $writer)
{
$code = $writer->write(
'\MyApp\Templating\Latte\MyMacros::includeTemplate($presenter->findPartialTemplateFile(%node.word), %node.array? + $template->getParameters(), $_l->templates[%var])',
$this->getCompiler()->getTemplateId()
);
if ($node->modifiers) {
return $writer->write('echo %modify(%raw->__toString(TRUE))', $code);
} else {
return $code . '->render()';
}
}
/**
* Substitute for the missing function since 2.2
*/
public static function includeTemplate($destination, array $params)
{
$tpl = new \Nette\Bridges\ApplicationLatte\Template(new \Latte\Engine);
$tpl->setFile($destination);
$tpl->setParameters($params);
return $tpl;
}
}
It partially did the trick but cannot be used as it apparently cannot detect default Nette macros:
User Error – Exception in Nette\Bridges\ApplicationLatte\Template::__toString(): Unknown macro {link} in …/partials/file.latte:31 in /var/www/html/…
Would you be so kind as to point me in the right direction? I fail to find anything similar in the 2.2 docs.
Last edited by DavidTheNewbie (2017-02-12 12:29)