diff --git a/Views/MtHamlTwig.php b/Views/MtHamlTwig.php new file mode 100644 index 0000000..2724494 --- /dev/null +++ b/Views/MtHamlTwig.php @@ -0,0 +1,98 @@ + + */ +class MtHamlTwig extends \Slim\View +{ + + /** + * @var string The path to the directory containing the "MtHaml" folder without trailing slash. + */ + public static $mthamlDirectory = null; + + /** + * @var string The path to the templates folder WITH the trailing slash. + */ + public static $mthamlCacheDirectory = null; + + /** + * @var string The path to the directory containing the "MtHaml" folder without trailing slash. + */ + public static $twigDirectory = null; + + /** + * Renders a template using Haml.php. + * + * @see View::render() + * @throws RuntimeException If MtHaml lib directory does not exist. + * @throws RuntimeException If Twig lib directory does not exist. + * @param string $template The template name specified in Slim::render() + * @return string + */ + public function render($template) + { + if ( !is_dir(self::$mthamlDirectory) ) { + throw new \RuntimeException('Cannot set the MtHaml lib directory : ' . self::$mthamlDirectory . '. Directory does not exist.'); + } + if ( !is_dir(self::$twigDirectory) ) { + throw new \RuntimeException('Cannot set the Twig lib directory : ' . self::$twigDirectory . '. Directory does not exist.'); + } + + require_once self::$mthamlDirectory . '/MtHaml/Autoloader.php'; + require self::$twigDirectory . '/Twig/Autoloader.php'; + + \MtHaml\Autoloader::register(); + $mthaml = new \MtHaml\Environment('twig', array('enable_escaper' => false)); + $twig_filesystem = new \Twig_Loader_Filesystem(array($this->getTemplatesDirectory())); + $twig_loader = new \MtHaml\Support\Twig\Loader($mthaml, $twig_filesystem); + + $twig = new \Twig_Environment($twig_loader, array( + //'cache' => self::$mthamlCacheDirectory + )); + $twig->addExtension(new \MtHaml\Support\Twig\Extension()); + echo $twig->render($template); + } +} + +?>