Skip to content

Commit

Permalink
Merge branch 'master' of github.com:xp-forge/handlebars
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jun 13, 2015
2 parents f4f46c9 + c97bfa8 commit 1dbb6bf
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 23 deletions.
142 changes: 124 additions & 18 deletions src/main/php/com/handlebarsjs/HandlebarsEngine.class.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php namespace com\handlebarsjs;

use com\github\mustache\MustacheEngine;
use com\github\mustache\Template;
use com\github\mustache\TemplateLoader;
use util\log\LogCategory;
use util\log\LogLevel;
use lang\IllegalArgumentException;
new \import('com.handlebarsjs.LogCategoryExtensions');
new import('com.handlebarsjs.LogCategoryExtensions');

/**
* Handlebars implementation for the XP Framework.
Expand All @@ -24,14 +26,15 @@
* @test xp://com.handlebarsjs.unittest.WebsiteExamplesTest
* @see http://handlebarsjs.com/
*/
class HandlebarsEngine extends MustacheEngine {
class HandlebarsEngine extends \lang\Object {
protected $mustache;
protected $builtin= [];

/**
* Constructor. Initializes builtin helpers.
*/
public function __construct() {
parent::__construct();
$this->mustache= (new MustacheEngine())->withParser(new HandlebarsParser());

// This: Access the current value in the context
$this->setBuiltin('this', function($items, $context, $options) {
Expand All @@ -42,9 +45,22 @@ public function __construct() {
return $variable;
}
});
}

/** @return com.github.mustache.TemplateLoader */
public function templates() { return $this->mustache->getTemplates(); }

// Overwrite parser
$this->parser= new HandlebarsParser();
/** @return [:var] */
public function helpers() { return $this->mustache->helpers; }

/**
* Gets a given helper
*
* @param string $name
* @return var or NULL if no such helper exists.
*/
public function helper($name) {
return isset($this->mustache->helpers[$name]) ? $this->mustache->helpers[$name] : null;
}

/**
Expand All @@ -53,24 +69,14 @@ public function __construct() {
* @param string name
* @param var builtin
*/
protected function setBuiltin($name, $builtin) {
private function setBuiltin($name, $builtin) {
if (null === $builtin) {
unset($this->builtin[$name], $this->helpers[$name]);
unset($this->builtin[$name], $this->mustache->helpers[$name]);
} else {
$this->builtin[$name]= $this->helpers[$name]= $builtin;
$this->builtin[$name]= $this->mustache->helpers[$name]= $builtin;
}
}

/**
* Sets helpers
*
* @param [:var] $helpers
* @return self this
*/
public function withHelpers(array $helpers) {
return parent::withHelpers(array_merge($this->builtin, $helpers));
}

/**
* Sets a logger to use. Accepts either a closure, a util.log.LogCategory
* instance or NULL (to unset).
Expand Down Expand Up @@ -101,4 +107,104 @@ public function withLogger($logger) {
}
return $this;
}

/**
* Sets template loader to be used
*
* @param com.github.mustache.TemplateLoader $l
* @return self this
*/
public function withTemplates(TemplateLoader $l) {
$this->mustache->withTemplates($l);
return $this;
}

/**
* Adds a helper with a given name
*
* @param string $name
* @param var $helper
* @return self this
*/
public function withHelper($name, $helper) {
$this->mustache->withHelper($name, $helper);
return $this;
}

/**
* Sets helpers
*
* @param [:var] $helpers
* @return self this
*/
public function withHelpers(array $helpers) {
$this->mustache->withHelpers(array_merge($this->builtin, $helpers));
return $this;
}

/**
* Compile a template.
*
* @param string $template The template, as a string
* @param string $start Initial start tag, defaults to "{{"
* @param string $end Initial end tag, defaults to "}}"
* @param string $indent Indenting level, defaults to no indenting
* @return com.github.mustache.Template
*/
public function compile($template, $start= '{{', $end= '}}', $indent= '') {
return $this->mustache->compile($template, $start, $end, $indent);
}

/**
* Load a template.
*
* @param string $name The template name.
* @param string $start Initial start tag, defaults to "{{"
* @param string $end Initial end tag, defaults to "}}"
* @param string $indent Indenting level, defaults to no indenting
* @return com.github.mustache.Template
*/
public function load($name, $start= '{{', $end= '}}', $indent= '') {
return $this->mustache->load($name, $start, $end, $indent);
}

/**
* Evaluate a compiled template.
*
* @param com.github.mustache.Template $template The template
* @param var $arg Either a view context, or a Context instance
* @return string The rendered output
*/
public function evaluate(Template $template, $arg) {
return $this->mustache->evaluate($template, $arg);
}

/**
* Render a template - like evaluate(), but will compile if necessary.
*
* @param var $template The template, either as string or as compiled Template instance
* @param var $arg Either a view context, or a Context instance
* @param string $start Initial start tag, defaults to "{{"
* @param string $end Initial end tag, defaults to "}}"
* @param string $indent Indenting level, defaults to no indenting
* @return string The rendered output
*/
public function render($template, $arg, $start= '{{', $end= '}}', $indent= '') {
return $this->mustache->render($template, $arg, $start, $end, $indent);
}

/**
* Transform a template by its name, which is previously loaded from
* the template loader.
*
* @param string $name The template name.
* @param var $arg Either a view context, or a Context instance
* @param string $start Initial start tag, defaults to "{{"
* @param string $end Initial end tag, defaults to "}}"
* @param string $indent Indenting level, defaults to no indenting
* @return string The rendered output
*/
public function transform($name, $arg, $start= '{{', $end= '}}', $indent= '') {
return $this->mustache->transform($name, $arg, $start, $end, $indent);
}
}
11 changes: 6 additions & 5 deletions src/test/php/com/handlebarsjs/unittest/EngineTest.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace com\handlebarsjs\unittest;

use com\handlebarsjs\HandlebarsEngine;
use util\log\LogCategory;

class EngineTest extends \unittest\TestCase {

Expand All @@ -12,26 +13,26 @@ public function can_create() {
#[@test]
public function initially_no_logger_set() {
$engine= new HandlebarsEngine();
$this->assertFalse(isset($engine->helpers['log']));
$this->assertNull($engine->helper('log'));
}

#[@test]
public function withLogger_using_closure_sets_logger() {
$engine= create(new HandlebarsEngine())->withLogger(function($args) { });
$this->assertInstanceOf('Closure', $engine->helpers['log']);
$this->assertInstanceOf('Closure', $engine->helper('log'));
}

#[@test]
public function withLogger_using_LogCategory_sets_logger() {
$engine= create(new HandlebarsEngine())->withLogger(new \util\log\LogCategory('test'));
$this->assertInstanceOf('Closure', $engine->helpers['log']);
$engine= create(new HandlebarsEngine())->withLogger(new LogCategory('test'));
$this->assertInstanceOf('Closure', $engine->helper('log'));
}

#[@test]
public function withLogger_null_unsets_previously_set_logger() {
$engine= create(new HandlebarsEngine())->withLogger(function($args) { });
$engine->withLogger(null);
$this->assertFalse(isset($engine->helpers['log']));
$this->assertNull($engine->helper('log'));
}

#[@test, @expect('lang.IllegalArgumentException')]
Expand Down

0 comments on commit 1dbb6bf

Please sign in to comment.