public function render($name, $params = []) { $params = $this->mergeParams($name, $params); $template = $this->resolver->resolve($name); if (!$template) { throw new Exception\TemplateNotFoundException("Couldn't resolve '{$name}'"); } if ($template->isCompiled()) { $compiled = (string) $template; } else { try { $compiled = $this->handlebars->precompile((string) $template); } catch (\V8JsScriptException $e) { throw new Exception\TemplateCompilationException("Error compiling '{$name}'", 0, $e); } if ($this->resolver instanceof SavingResolverInterface) { $this->resolver->save($template->getKey(), $compiled); } } try { $template = $this->handlebars->template($compiled); return $template($params); } catch (\V8JsScriptException $e) { throw new Exception\TemplateExecutionException("Error running '{$name}'", 0, $e); } }
/** * We don't cache PHP helpers ourselves, but rely on bytecode cache instead * @param Handlebars $handlebars * @param array $helpers */ private function injectPhpHelpers(Handlebars $handlebars, $helpers) { $phpHelpers = []; foreach ($helpers as $fileName) { $helper = (include $fileName); $handlebars->registerHelper($this->getName($fileName), $helper); } $handlebars->registerHelper($phpHelpers); }
public function testLogDebug() { $hb = Handlebars::create(); $logs = []; $logger = $this->getLogger($logs)->reveal(); $hb->setLogger($logger); $template = $hb->compile('{{ log "Debug!" level="debug" }}'); $result = $template([], ['data' => ['level' => LogLevel::DEBUG]]); $this->assertContains("Debug!", $logs[LogLevel::DEBUG]); }