From ca7c6bcff69d4c59d211b833f6aa3c6d99274a11 Mon Sep 17 00:00:00 2001 From: Adrian Demleitner Date: Mon, 24 Dec 2012 07:07:03 -0800 Subject: [PATCH] Create MtHamlTwig.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Working example here: https://github.com/ichbinadrian/Slim-MtHaml-Twig It's the MtHaml to Twig View for Slim. --- Views/MtHamlTwig.php | 98 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 Views/MtHamlTwig.php 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); + } +} + +?>