Vlastni makra po migraci z 2.1.9 na 2.2.2
- DavidTheNewbie
- Člen | 79
Ahojte. Rad bych pozadal o pomoc. Migruji z 2.1.9 na 2.2.2
Vlastni makra v 2.1.9 registruji viz nize. Toto pod starsi verzi funguje:
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()';
}
}
}
S verzi 2.2 nove zacilim soubor s vlastnimi makry skrze neon viz nize:
nette:
latte:
macros:
- MyApp\Templating\Latte\MyMacros
Soubor s vlastnimi makry nove vypada takto (s opravenymi jmennymi prostory):
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()';
}
}
}
Generuje se vsak error:
Fatal Error – Call to undefined method Latte\Macros\CoreMacros::includeTemplate()
Jde o to, ze nyni partial macro odkazuje na jiz neexistujici funkcni CoreMacros::includeTemplate(…) , ktera se do 2.2 nedostala. Existuje neco podobneho od verze 2.2?
Pokusil jsem se o nahradu viz nize:
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;
}
}
Castecne pomohlo, ale neni pouzitelne, jelikoz reseni ocividne nedetekuje defaultni Nette macra:
User Error – Exception in Nette\Bridges\ApplicationLatte\Template::__toString(): Unknown macro {link} in …/partials/file.latte:31 in /var/www/html/…
Rad bych pozadal o nakopnuti spravnym smerem? V 2.2 dokumentaci se mi nedari nic podobneho dohledat.
Editoval DavidTheNewbie (12. 2. 2017 12:43)
- David Matějka
- Moderator | 6445
staci se inspirovat u include makra: https://github.com/…reMacros.php#L207
- DavidTheNewbie
- Člen | 79
David Matějka napsal(a):
staci se inspirovat u include makra: https://github.com/…reMacros.php#L207
Dekuju. Co prosim znamena to magicke $_b->templates[%var]?
Editoval DavidTheNewbie (12. 2. 2017 18:35)