Added string literal support in Context::get and added Template::parseArguments to ease plugin development

This commit is contained in:
Chris Gray 2014-01-16 23:28:48 -06:00
parent 1119784549
commit da1cf77c58
2 changed files with 19 additions and 7 deletions

View File

@ -9,6 +9,7 @@
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @author Chris Gray <chrisgray@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @copyright 2013 (c) f0ruD A
@ -223,6 +224,8 @@ class Context
$current = $this->lastIndex();
} elseif ($variableName == '@key') {
$current = $this->lastKey();
} elseif ($variableName[0] == "'" || $variableName[0] == '"') {
$current = trim($variableName, '"\'');
} else {
$chunks = explode('.', $variableName);
foreach ($chunks as $chunk) {
@ -232,7 +235,6 @@ class Context
$current = $this->_findVariableInContext($current, $chunk, $strict);
}
}
return $current;
}

View File

@ -9,6 +9,7 @@
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @author Chris Gray <chrisgray@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
@ -408,12 +409,6 @@ class Template
{
$name = $current[Tokenizer::NAME];
$value = $context->get($name);
if ($name == '@index') {
return $context->lastIndex();
}
if ($name == '@key') {
return $context->lastKey();
}
if ($escaped) {
$args = $this->handlebars->getEscapeArgs();
array_unshift($args, $value);
@ -425,4 +420,19 @@ class Template
return $value;
}
/**
* Break an argument string into an array of strings
*
* @param string $string Argument String as passed to a helper
*
* @return array the argument list as an array
*/
public function parseArguments($string){
$parts = array();
preg_match_all('#(?<!\\\\)("|\')(?:[^\\\\]|\\\\.)*?\1|\S+#s', $string, $parts);
$parts = isset($parts[0])?$parts[0]:array();
$parts = array_map("stripcslashes", $parts);
return $parts;
}
}