setHtmlPath($htmlPath);
$this->initTwig();
}
private function initTwig()
{
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem($this->htmlPath);
$this->twig = new Twig_Environment($loader, array());
}
public function setHtmlPath($htmlPath)
{
if(!file_exists($htmlPath))
{
throw new TwigClassException('Path does not exist');
}
$this->htmlPath = $htmlPath;
}
public function render($file, $options)
{
$options['all'] = array_keys($options);
return $this->twig->render($file, $options);
}
public function createSimpleFunc($name, /*callable*/ $function)
{
if(!is_callable($function))
{
throw new TwigClassException('Second param must be a function');
}
return $this->twig->addFunction(new Twig_SimpleFunction($name, $function));
}
public function createSimpleFilter($name, /*callable*/ $function)
{
if(!is_callable($function))
{
throw new TwigClassException('Second param must be a function');
}
return $this->twig->addFilter(new Twig_SimpleFilter($name, $function));
}
}
class TwigClassException extends Exception
{
}