From 8e4ffaefa2c9ec7b9de9f0511a8fa17732c9af3b Mon Sep 17 00:00:00 2001 From: WanWizard Date: Wed, 17 Jan 2018 14:30:25 +0100 Subject: [PATCH] experimental support for Handlebar templates using the zordius/lightncandy renderer --- bootstrap.php | 1 + classes/view/handlebars.php | 63 +++++++++++++++++++++++++++++++++++++ config/parser.php | 38 +++++++++++++++------- 3 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 classes/view/handlebars.php diff --git a/bootstrap.php b/bootstrap.php index 8156e12..3aee2eb 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -22,6 +22,7 @@ 'Parser\\View_Twig' => __DIR__.'/classes/view/twig.php', 'Parser\\View_HamlTwig' => __DIR__.'/classes/view/hamltwig.php', 'Parser\\View_Jade' => __DIR__.'/classes/view/jade.php', + 'Parser\\View_Handlebars' => __DIR__.'/classes/view/handlebars.php', 'Parser\\View_Haml' => __DIR__.'/classes/view/haml.php', 'Parser\\View_Smarty' => __DIR__.'/classes/view/smarty.php', 'Parser\\View_Phptal' => __DIR__.'/classes/view/phptal.php', diff --git a/classes/view/handlebars.php b/classes/view/handlebars.php new file mode 100644 index 0000000..2ee0201 --- /dev/null +++ b/classes/view/handlebars.php @@ -0,0 +1,63 @@ +file_name; + + // compiled template path + $path = rtrim(\Config::get('parser.View_Handlebars.compile_dir', APPPATH.'tmp'.DS.'handlebars'),DS).DS; + + // construct the compiled filename + $compiled = md5($file); + $compiled = $path.substr($compiled, 0, 1).DS.substr($compiled, 1, 1).DS.substr($compiled, 2).'.'.$this->extension; + + // do we need to compile? + if ( ! is_file($compiled) or filemtime($file) > filemtime($compiled) or \Config::get('parser.View_Handlebars.force_compile', true)) + { + file_put_contents($compiled, ' function($cx, $name) { return \Finder::search('views', $name, '.'.$this->extension, false, false); } + ) + \Config::get('parser.View_Handlebars.environment', array()) + )); + } + + // fetch the compiled template and render it + try + { + $result = include($compiled); + $result = $result($this->get_data()); + } + catch (\Exception $e) + { + // Delete the output buffer & re-throw the exception + ob_end_clean(); + throw $e; + } + + $this->unsanitize($data); + return $result; + } +} diff --git a/config/parser.php b/config/parser.php index d995d14..e6f15a9 100644 --- a/config/parser.php +++ b/config/parser.php @@ -21,23 +21,26 @@ * This will allow you to upgrade fuel without losing your custom config. */ +use LightnCandy\LightnCandy; + return array( // ------------------------------------------------------------------------ // Register extensions to their parsers, either classname or array config // ------------------------------------------------------------------------ 'extensions' => array( - 'php' => 'View', - 'twig' => 'View_Twig', - 'mthaml' => array('class' => 'View_HamlTwig', 'extension' => 'haml'), - 'mustache' => 'View_Mustache', - 'md' => 'View_Markdown', - 'dwoo' => array('class' => 'View_Dwoo', 'extension' => 'tpl'), - 'jade' => 'View_Jade', - 'haml' => 'View_Haml', - 'smarty' => 'View_Smarty', - 'phptal' => 'View_Phptal', - 'lex' => 'View_Lex', + 'php' => 'View', + 'twig' => 'View_Twig', + 'mthaml' => array('class' => 'View_HamlTwig', 'extension' => 'haml'), + 'mustache' => 'View_Mustache', + 'md' => 'View_Markdown', + 'dwoo' => array('class' => 'View_Dwoo', 'extension' => 'tpl'), + 'jade' => 'View_Jade', + 'handlebars' => 'View_Handlebars', + 'haml' => 'View_Haml', + 'smarty' => 'View_Smarty', + 'phptal' => 'View_Phptal', + 'lex' => 'View_Lex', ), // ------------------------------------------------------------------------ @@ -196,4 +199,17 @@ 'scope_glue' => '.', 'allow_php' => false, ), + + // Handlebars ( https://github.com/zordius/lightncandy ) + // Packagist url: https://packagist.org/packages/zordius/lightncandy + // ------------------------------------------------------------------------ + 'View_Handlebars' => array( + 'force_compile' => true, + 'compile_dir' => APPPATH.'tmp'.DS.'handlebars'.DS, + 'environment' => array( + 'flags' => LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_ELSE | LightnCandy::FLAG_HBESCAPE | LightnCandy::FLAG_JS, + 'helpers' => array(), + 'helperresolver' => function($cx, $name) { return; }, + ), + ), );