mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-04-16 03:17:24 +03:00
Merge pull request #35 from cgray/master
Context::get will resolve @key/@index & added Template::parseArguments helper
This commit is contained in:
commit
58c33c1ba7
@ -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
|
||||
@ -183,6 +184,7 @@ class Context
|
||||
* @param boolean $strict strict search? if not found then throw exception
|
||||
*
|
||||
* @throws \InvalidArgumentException in strict mode and variable not found
|
||||
* @throws \RuntimeException if supplied argument is a malformed quoted string
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($variableName, $strict = false)
|
||||
@ -219,6 +221,16 @@ class Context
|
||||
return '';
|
||||
} elseif ($variableName == '.' || $variableName == 'this') {
|
||||
return $current;
|
||||
} elseif ($variableName == '@index') {
|
||||
$current = $this->lastIndex();
|
||||
} elseif ($variableName == '@key') {
|
||||
$current = $this->lastKey();
|
||||
} elseif ($variableName[0] == "'" || $variableName[0] == '"') {
|
||||
if ($variableName[0] == substr($variableName, -1) && strlen($variableName) > 2) {
|
||||
$current = substr($variableName, 1, strlen($variableName) -2);
|
||||
} else {
|
||||
throw new \RuntimeException("Malformed string: ".$variableName);
|
||||
}
|
||||
} else {
|
||||
$chunks = explode('.', $variableName);
|
||||
foreach ($chunks as $chunk) {
|
||||
@ -228,7 +240,6 @@ class Context
|
||||
$current = $this->_findVariableInContext($current, $chunk, $strict);
|
||||
}
|
||||
}
|
||||
|
||||
return $current;
|
||||
}
|
||||
|
||||
|
@ -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,20 @@ 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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user