StringTemplate
Třída pro použití šablony z řetězce.
| Autor | David Grudl |
|---|
Třída
class StringTemplate extends BaseTemplate
{
public $content;
/**
* Renders template to output.
* @return void
*/
public function render()
{
$cache = Environment::getCache('StringTemplate');
$key = md5($this->content);
$content = $cache[$key];
if ($content === NULL) { // not cached
if (!$this->getFilters()) {
$this->onPrepareFilters($this);
}
$cache[$key] = $content = $this->compile($this->content);
}
$this->__set('template', $this);
/*Nette\Loaders\*/LimitedScope::evaluate($content, $this->getParams());
}
}
Použití
$template = new StringTemplate();
$template->presenter = Environment::getApplication()->getPresenter(); // nutné např. pro rozchození linků
$template->registerFilter(new LatteFilter);
$template->content = $text; // obsah šablony (řetězec)
// vrátíme vygenerovanou šablonu
return $template->__toString();
// nebo ji vypíšeme na výstup
$template->render();
jtousek | 12. 10. 2010, 12:57 | comment
Pozor na to, že pro Nette 2.0 musí třída extendovat Nette\Template a nikoli BaseTemplate.