change default helpers to anonymus function again

This commit is contained in:
fzerorubigd 2013-11-21 23:35:30 +03:30
parent f13c6a3cef
commit 000bdd6e74
No known key found for this signature in database
GPG Key ID: D6EE858AF9D2999A
4 changed files with 112 additions and 152 deletions

View File

@ -85,7 +85,11 @@ class Autoloader
return;
}
$file = sprintf('%s/%s.php', $this->_baseDir, str_replace('\\', '/', $class));
$file = sprintf(
'%s/%s.php',
$this->_baseDir,
str_replace('\\', '/', $class)
);
if (is_file($file)) {
include $file;

View File

@ -47,7 +47,8 @@ class Helpers
* Create new helper container class
*
* @param array $helpers array of name=>$value helpers
* @param array|bool $defaults add defaults helper (if, unless, each,with, bindAttr)
* @param array|bool $defaults add defaults helper
* (if, unless, each,with, bindAttr)
*
* @throws \InvalidArgumentException when $helpers is not an array
* (or traversable) or helper is not a callable
@ -69,144 +70,6 @@ class Helpers
}
}
/**
* Create handler for the 'if' helper.
*
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions.
*
* @param \Handlebars\Template $template template that is being rendered
* @param \Handlebars\Context $context context object
* @param array $args passed arguments to helper
* @param string $source part of template that is wrapped
* within helper
*
* @return mixed
*/
public static function helperIf($template, $context, $args, $source)
{
$tmp = $context->get($args);
if ($tmp) {
$template->setStopToken('else');
$buffer = $template->render($context);
$template->setStopToken(false);
$template->discard($context);
} else {
$template->setStopToken('else');
$template->discard($context);
$template->setStopToken(false);
$buffer = $template->render($context);
}
return $buffer;
}
/**
* Create handler for the 'each' helper.
*
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions.
*
* @param \Handlebars\Template $template template that is being rendered
* @param \Handlebars\Context $context context object
* @param array $args passed arguments to helper
* @param string $source part of template that is wrapped
* within helper
*
* @return mixed
*/
public static function helperEach($template, $context, $args, $source)
{
$tmp = $context->get($args);
$buffer = '';
if (is_array($tmp) || $tmp instanceof \Traversable) {
$islist = (array_keys($tmp) == range(0, count($tmp) - 1));
foreach ($tmp as $key => $var) {
if ($islist) {
$context->pushIndex($key);
} else {
$context->pushKey($key);
}
$context->push($var);
$buffer .= $template->render($context);
$context->pop();
if ($islist) {
$context->popIndex();
} else {
$context->popKey();
}
}
}
return $buffer;
}
/**
* Create handler for the 'unless' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions.
*
* @param \Handlebars\Template $template template that is being rendered
* @param \Handlebars\Context $context context object
* @param array $args passed arguments to helper
* @param string $source part of template that is wrapped
* within helper
*
* @return mixed
*/
public static function helperUnless($template, $context, $args, $source)
{
$tmp = $context->get($args);
$buffer = '';
if (!$tmp) {
$buffer = $template->render($context);
}
return $buffer;
}
/**
* Create handler for the 'with' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions.
*
* @param \Handlebars\Template $template template that is being rendered
* @param \Handlebars\Context $context context object
* @param array $args passed arguments to helper
* @param string $source part of template that is wrapped
* within helper
*
* @return mixed
*/
public static function helperWith($template, $context, $args, $source)
{
$tmp = $context->get($args);
$context->push($tmp);
$buffer = $template->render($context);
$context->pop();
return $buffer;
}
/**
* Create handler for the 'bindAttr' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions.
*
* @param \Handlebars\Template $template template that is being rendered
* @param \Handlebars\Context $context context object
* @param array $args passed arguments to helper
* @param string $source part of template that is wrapped
* within helper
*
* @return mixed
*/
public static function helperBindAttr($template, $context, $args, $source)
{
return $args;
}
/**
* Add default helpers (if unless each with bindAttr)
@ -217,28 +80,115 @@ class Helpers
{
$this->add(
'if',
array('Handlebars\Helpers', 'helperIf')
function ($template, $context, $args, $source) {
/**
* @var $template \Handlebars\Template
* @var $context \Handlebars\Context
* @var $args array
* @var $source string
*/
$tmp = $context->get($args);
if ($tmp) {
$template->setStopToken('else');
$buffer = $template->render($context);
$template->setStopToken(false);
$template->discard($context);
} else {
$template->setStopToken('else');
$template->discard($context);
$template->setStopToken(false);
$buffer = $template->render($context);
}
return $buffer;
}
);
$this->add(
'each',
array('Handlebars\Helpers', 'helperEach')
function ($template, $context, $args, $source) {
/**
* @var $template \Handlebars\Template
* @var $context \Handlebars\Context
* @var $args array
* @var $source string
*/
$tmp = $context->get($args);
$buffer = '';
if (is_array($tmp) || $tmp instanceof \Traversable) {
$islist = (array_keys($tmp) == range(0, count($tmp) - 1));
foreach ($tmp as $key => $var) {
if ($islist) {
$context->pushIndex($key);
} else {
$context->pushKey($key);
}
$context->push($var);
$buffer .= $template->render($context);
$context->pop();
if ($islist) {
$context->popIndex();
} else {
$context->popKey();
}
}
}
return $buffer;
}
);
$this->add(
'unless',
array('Handlebars\Helpers', 'helperUnless')
function ($template, $context, $args, $source) {
/**
* @var $template \Handlebars\Template
* @var $context \Handlebars\Context
* @var $args array
* @var $source string
*/
$tmp = $context->get($args);
$buffer = '';
if (!$tmp) {
$buffer = $template->render($context);
}
return $buffer;
}
);
$this->add(
'with',
array('Handlebars\Helpers', 'helperWith')
function ($template, $context, $args, $source) {
/**
* @var $template \Handlebars\Template
* @var $context \Handlebars\Context
* @var $args array
* @var $source string
*/
$tmp = $context->get($args);
$context->push($tmp);
$buffer = $template->render($context);
$context->pop();
return $buffer;
}
);
//Just for compatibility with ember
$this->add(
'bindAttr',
array('Handlebars\Helpers', 'helperBindAttr')
function ($template, $context, $args, $source) {
/**
* @var $template \Handlebars\Template
* @var $context \Handlebars\Context
* @var $args array
* @var $source string
*/
return $args;
}
);
}

View File

@ -200,15 +200,10 @@ class Template
/**
* Discard top tree
*
* @param mixed $context current context
*
* @return string
*/
public function discard($context)
public function discard()
{
if (!$context instanceof Context) {
$context = new Context($context);
}
$topTree = end($this->_stack); //This method never pop a value from stack
list($index, $tree, $stop) = $topTree;
while (array_key_exists($index, $tree)) {

View File

@ -111,6 +111,17 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
array('data' => array(1, 2, 3, 4)),
'1234'
),
array(
'{{#unless data}}ok{{/unless}}',
array('data' => true),
''
),
array(
'{{#unless data}}ok{{/unless}}',
array('data' => false),
'ok'
)
);
}
@ -203,7 +214,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
public function testVariableAccess()
{
$loader = new \Handlebars\Loader\StringLoader();
$engine = new \Handlebars\Handlebars();
$engine = \Handlebars\Handlebars::factory();
$engine->setLoader($loader);
$var = new \StdClass();