Use Arguments class instead of regex

This commit is contained in:
Pascal Thormeier 2015-06-17 23:57:12 +02:00
parent e1956be453
commit 9ebacc28ae

View File

@ -23,6 +23,7 @@
*/ */
namespace Handlebars; namespace Handlebars;
use Handlebars\Arguments;
/** /**
* Handlebars base template * Handlebars base template
@ -507,13 +508,9 @@ class Template
$partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]); $partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]);
if ($current[Tokenizer::ARGS]) { if ($current[Tokenizer::ARGS]) {
preg_match_all( $arguments = new Arguments($current[Tokenizer::ARGS]);
'/(\w+\=["|\'][^"\']+["|\']|\w+\=[a-zA-Z0-9\.\=]+|[a-zA-Z0-9\.]+)+/m',
$current[Tokenizer::ARGS],
$args
);
$context = new Context($this->_getPartialArgumentsValues($context, $args[0])); $context = new Context($this->_preparePartialArguments($context, $arguments));
} }
return $partial->render($context); return $partial->render($context);
@ -522,34 +519,26 @@ class Template
/** /**
* Prepare the arguments of a partial to actual array values to be used in a new context * Prepare the arguments of a partial to actual array values to be used in a new context
* *
* @param Context $context Current context * @param Context $context Current context
* @param array $args Arguments given by tokenizer, splitted to key=>value pairs * @param Arguments $arguments Arguments for partial
* *
* @return array * @return array
*/ */
private function _getPartialArgumentsValues(Context $context, array $args) private function _preparePartialArguments(Context $context, Arguments $arguments)
{ {
$partialArgs = array(); $positionalArgs = array();
foreach ($arguments->getPositionalArguments() as $positionalArg) {
foreach ($args as $arg) { $contextArg = $context->get($positionalArg);
$parts = explode('=', $arg, 2); if (is_array($contextArg)) {
foreach ($contextArg as $key => $value) {
$key = $parts[0]; $positionalArgs[$key] = $value;
if (false === isset($parts[1])) {
$value = $context->get($parts[0]);
if (is_array($value)) {
foreach ($value as $varKey => $varValue) {
$partialArgs[$varKey] = $varValue;
}
} else {
$partialArgs[$key] = $value;
} }
} else { } else {
$partialArgs[$key] = strpos($parts[1], '"') === false ? $context->get($parts[1]) : trim('"', $parts[1]); $positionalArgs[$positionalArg] = $contextArg;
} }
} }
return $partialArgs; return array_merge($positionalArgs, $arguments->getNamedArguments());
} }