Nette 2.2 – Unknown macro

Notice: This thread is very old.
bohacf
Backer | 7
+
0
-

Hi,
after migration from 2.1 to 2.2 I have problem with registering my own macro. My macros I registering in BasePresenter. But app falls on Latte\CompileException “Unknown macro {scache}”.

Do you have any idea how to fix it?

<?php
// BasePresenter:
public function createTemplate($class = NULL) {
    $template = parent::createTemplate($class);

    $latteEngine = new Nette\Latte\Engine;

    $compiler = $latteEngine->getCompiler();
    Nette\Bridges\FormsLatte\FormMacros::install($compiler);
    $macros = new Latte\Macros\MacroSet($compiler);
    $macros->addMacro('scache', '?>?<?php echo strtotime(date(\'Y-m-d hh \')); ?>"<?php');
    ...
?>
Jan Tvrdík
Nette guru | 2595
+
+2
-

Well, that is because you create new instance of Nette\Latte\Engine instead of using the existing one.

protected function createTemplate($class)
{
	$template = parent::createTemplate($class);
	$latte = $template->getLatte();
	$compiler = $latte->getCompiler();
	...

	return $template;
}
bohacf
Backer | 7
+
0
-

Works perfectly – thanks a lot!

bohacf
Backer | 7
+
0
-

But if I have file template, $template->getLatte() returns null.

Solution:

<?php
  // old style: $template = new \Nette\Templating\FileTemplate($fileName);
  $latte = new \Latte\Engine();
  $template = new \Nette\Bridges\ApplicationLatte\Template($latte);
  $template->setFile($fileName);
?>

Last edited by bohacf (2014-08-21 12:52)