Merge pull request #17 from diosney/master

Helpers: Added PHP 5.2 support.
This commit is contained in:
Behrooz Shabani 2013-08-21 08:31:51 -07:00
commit dd7df5bf65
2 changed files with 123 additions and 56 deletions

View File

@ -1,4 +1,5 @@
language: php language: php
php: php:
- 5.2
- 5.3 - 5.3
- 5.4 - 5.4

View File

@ -59,8 +59,116 @@ class Handlebars_Helpers
} }
} }
} }
/**
* Create handler for the 'if' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous functions.
*
* @param $template
* @param $context
* @param $args
* @param $source
*
* @return mixed
*/
public static function _helper_if($template, $context, $args, $source) {
$tmp = $context->get($args);
$buffer = '';
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 $template
* @param $context
* @param $args
* @param $source
*
* @return mixed
*/
public static function _helper_each($template, $context, $args, $source) {
$tmp = $context->get($args);
$buffer = '';
if (is_array($tmp) || $tmp instanceof Traversable) {
foreach ($tmp as $var) {
$context->push($var);
$buffer .= $template->render($context);
$context->pop();
}
}
return $buffer;
}
/**
* Create handler for the 'unless' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous functions.
*
* @param $template
* @param $context
* @param $args
* @param $source
*
* @return mixed
*/
public static function _helper_unless($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 $template
* @param $context
* @param $args
* @param $source
*
* @return mixed
*/
public static function _helper_with($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 $template
* @param $context
* @param $args
* @param $source
*
* @return mixed
*/
public static function _helper_bindAttr($template, $context, $args, $source) {
return $args;
}
/** /**
* Add default helpers (if unless each with) * Add default helpers (if unless each with bindAttr)
* *
* @return void * @return void
*/ */
@ -68,70 +176,28 @@ class Handlebars_Helpers
{ {
$this->add( $this->add(
'if', 'if',
function ($template, $context, $args, $source) { array('Handlebars_Helpers', '_helper_if')
$tmp = $context->get($args);
$buffer = '';
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( $this->add(
'each', 'each',
function ($template, $context, $args, $source) { array('Handlebars_Helpers', '_helper_each')
$tmp = $context->get($args);
$buffer = '';
if (is_array($tmp) || $tmp instanceof Traversable) {
foreach ($tmp as $var) {
$context->push($var);
$buffer .= $template->render($context);
$context->pop();
}
}
return $buffer;
}
); );
$this->add( $this->add(
'unless', 'unless',
function ($template, $context, $args, $source) { array('Handlebars_Helpers', '_helper_unless')
$tmp = $context->get($args);
$buffer = '';
if (!$tmp) {
$buffer = $template->render($context);
}
return $buffer;
}
); );
$this->add( $this->add(
'with', 'with',
function ($template, $context, $args, $source) { array('Handlebars_Helpers', '_helper_with')
$tmp = $context->get($args);
$context->push($tmp);
$buffer = $template->render($context);
$context->pop();
return $buffer;
}
); );
//Just for compatibility with ember //Just for compatibility with ember
$this->add( $this->add(
'bindAttr', 'bindAttr',
function ($template, $context, $args, $source) { array('Handlebars_Helpers', '_helper_bindAttr')
return $args;
}
); );
} }