Add support for mustache like each/with and if

This commit is contained in:
fzerorubigd 2012-11-10 14:44:23 +03:30
parent f3fc076dfc
commit e3c2936694
2 changed files with 40 additions and 5 deletions

View File

@ -99,10 +99,12 @@ class Handlebars_Context
* variable , ../variable , variable.variable , . * variable , ../variable , variable.variable , .
* *
* @param string $variableName variavle name to get from current context * @param string $variableName variavle name to get from current context
* @param boolean $strict strict search? if not found then throw exception
* *
* @return mixed * @return mixed
* @throw InvalidArgumentException in strict mode and variable not found
*/ */
public function get($variableName) public function get($variableName, $strict = false)
{ {
//Need to clean up //Need to clean up
$variableName = trim($variableName); $variableName = trim($variableName);
@ -112,6 +114,9 @@ class Handlebars_Context
$level++; $level++;
} }
if (count($this->stack) < $level) { if (count($this->stack) < $level) {
if ($strict) {
throw new InvalidArgumentException('can not find variable in context');
}
return ''; return '';
} }
end($this->stack); end($this->stack);
@ -121,6 +126,9 @@ class Handlebars_Context
} }
$current = current($this->stack); $current = current($this->stack);
if (!$variableName) { if (!$variableName) {
if ($strict) {
throw new InvalidArgumentException('can not find variable in context');
}
return ''; return '';
} elseif ($variableName == '.') { } elseif ($variableName == '.') {
return $current; return $current;
@ -141,10 +149,12 @@ class Handlebars_Context
* *
* @param mixed $variable variable to check * @param mixed $variable variable to check
* @param string $inside property/method to check * @param string $inside property/method to check
* @param boolean $strict strict search? if not found then throw exception
* *
* @return boolean true if exist * @return boolean true if exist
* @throw InvalidArgumentException in strict mode and variable not found
*/ */
private function _findVariableInContext($variable, $inside) private function _findVariableInContext($variable, $inside, $strict = false)
{ {
$value = ''; $value = '';
if (is_array($variable)) { if (is_array($variable)) {
@ -159,6 +169,8 @@ class Handlebars_Context
} }
} elseif ($inside === '.') { } elseif ($inside === '.') {
$value = $variable; $value = $variable;
} elseif ($strict) {
throw new InvalidArgumentException('can not find variable in context');
} }
return $value; return $value;
} }

View File

@ -168,6 +168,29 @@ class Handlebars_Template
$source $source
); );
return call_user_func_array($helpers->$sectionName, $params); return call_user_func_array($helpers->$sectionName, $params);
} elseif (trim($current[Handlebars_Tokenizer::ARGS]) == '') {
//Fallback for mustache style each/with/for just if there is no argument at all.
try {
$sectionVar = $context->get($sectionName, true);
} catch (InvalidArgumentException $e) {
throw new RuntimeException($sectionName . ' is not registered as a helper');
}
$buffer = '';
if (is_array($sectionVar) || $sectionVar instanceof Traversable) {
foreach ($sectionVar as $d) {
$context->push($d);
$buffer .= $this->render($context);
$context->pop();
}
} elseif (is_object($sectionVar)) {
//Act like with
$context->push($sectionVar);
$buffer = $this->render($context);
$context->pop();
} elseif ($sectionVar) {
$buffer = $this->render($context);
}
return $buffer;
} else { } else {
throw new RuntimeException($sectionName . ' is not registered as a helper'); throw new RuntimeException($sectionName . ' is not registered as a helper');
} }