1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- class TwigWrapper
- {
- private $htmlPath;
- public function __construct($htmlPath)
- {
- $this->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
- {
- }
|