Skip to content

Commit

Permalink
experimental support for Handlebar templates
Browse files Browse the repository at this point in the history
using the zordius/lightncandy renderer
  • Loading branch information
WanWizard committed Jan 17, 2018
1 parent 3bd46e2 commit 8e4ffae
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
1 change: 1 addition & 0 deletions bootstrap.php
Expand Up @@ -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',
Expand Down
63 changes: 63 additions & 0 deletions classes/view/handlebars.php
@@ -0,0 +1,63 @@
<?php
/**
* Fuel
*
* Fuel is a fast, lightweight, community driven PHP5 framework.
*
* @package Fuel
* @version 1.8
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2017 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Parser;

use LightnCandy\LightnCandy;

class View_Handlebars extends \View
{
// default extension used by this template engine
public $extension = 'hbs';

protected function process_file($file_override = false)
{
// get the template filename
$file = $file_override ?: $this->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, '<?php ' . LightnCandy::compile(
file_get_contents($file),
array(
'partialresolver' => 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;
}
}
38 changes: 27 additions & 11 deletions config/parser.php
Expand Up @@ -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',
),

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -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; },
),
),
);

0 comments on commit 8e4ffae

Please sign in to comment.