mirror of
https://github.com/Mibew/handlebars.php.git
synced 2024-11-15 08:44:12 +03:00
Fix and add tests
Also use var_export in exceptions in case array/object/etc get passed
This commit is contained in:
parent
a4b6f9627a
commit
0111689f47
@ -144,7 +144,7 @@ class Arguments
|
||||
} else {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Malformed arguments string: "%s"',
|
||||
$args_string
|
||||
var_export($args_String, true)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -198,7 +198,7 @@ class Context
|
||||
if ($strict) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Can not find variable in context: "%s"',
|
||||
$variableName
|
||||
var_export($variableName, true)
|
||||
));
|
||||
}
|
||||
|
||||
@ -218,7 +218,7 @@ class Context
|
||||
if ($strict) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Can not find variable in context: "%s"',
|
||||
$variableName
|
||||
var_export($variableName, true)
|
||||
));
|
||||
}
|
||||
|
||||
@ -232,7 +232,7 @@ class Context
|
||||
} elseif ($strict) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Can not find variable in context: "%s"',
|
||||
$variableName
|
||||
var_export($variableName, true)
|
||||
));
|
||||
} else {
|
||||
return '';
|
||||
@ -281,7 +281,7 @@ class Context
|
||||
if ($strict) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Can not find variable in context: "%s"',
|
||||
$variable
|
||||
var_export($variable, true)
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -459,10 +459,10 @@ class Handlebars
|
||||
public function setTemplateClass($class)
|
||||
{
|
||||
if (!is_a($class, 'Handlebars\\Template', true)) {
|
||||
throw new \InvalidArgumentException(
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Custom template class "%s" must extend Template',
|
||||
$class
|
||||
);
|
||||
var_export($class, true)
|
||||
));
|
||||
}
|
||||
|
||||
$this->_templateClass = $class;
|
||||
|
@ -101,7 +101,7 @@ class Helpers
|
||||
if (!is_callable($helper) && ! $helper instanceof Helper) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
"%s Helper is not a callable or doesn't implement the Helper interface.",
|
||||
$name
|
||||
var_export($name, true)
|
||||
));
|
||||
}
|
||||
$this->helpers[$name] = $helper;
|
||||
@ -139,7 +139,7 @@ class Helpers
|
||||
if (!$this->has($name)) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Unknown helper: "%s"',
|
||||
$name
|
||||
var_export($name, true)
|
||||
));
|
||||
}
|
||||
|
||||
@ -177,7 +177,7 @@ class Helpers
|
||||
if (!$this->has($name)) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Unknown helper: "%s"',
|
||||
$name
|
||||
var_export($name, true)
|
||||
));
|
||||
}
|
||||
|
||||
@ -235,7 +235,7 @@ class Helpers
|
||||
if (!$this->has($name)) {
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
'Unknown helper: "%s"',
|
||||
$name
|
||||
var_export($name, true)
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,7 @@ class Parser
|
||||
if ($result === null) {
|
||||
throw new \LogicException(sprintf(
|
||||
'Unexpected closing tag: /%s',
|
||||
$token[Tokenizer::NAME]
|
||||
var_export($token[Tokenizer::NAME], true)
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -406,7 +406,7 @@ class Template
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
throw new \RuntimeException(sprintf(
|
||||
'"%s" is not registered as a helper',
|
||||
$sectionName
|
||||
var_export($sectionName, true)
|
||||
));
|
||||
}
|
||||
$buffer = '';
|
||||
@ -464,7 +464,7 @@ class Template
|
||||
} else {
|
||||
throw new \RuntimeException(sprintf(
|
||||
'"%s"" is not registered as a helper',
|
||||
$sectionName
|
||||
var_export($sectionName, true)
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -503,23 +503,50 @@ class Template
|
||||
|
||||
if ($current[Tokenizer::ARGS]) {
|
||||
preg_match_all(
|
||||
'/(\w+\=["|\'][^"\']+["|\']|\w+\=[a-zA-Z0-9\.\=]+)+/m',
|
||||
'/(\w+\=["|\'][^"\']+["|\']|\w+\=[a-zA-Z0-9\.\=]+|[a-zA-Z0-9\.]+)+/m',
|
||||
$current[Tokenizer::ARGS],
|
||||
$args
|
||||
);
|
||||
|
||||
$partialArgs = array();
|
||||
foreach ($args[0] as $arg) {
|
||||
list($key, $value) = explode('=', $arg, 2);
|
||||
$partialArgs[$key] = strpos($value, '"') === false ? $context->get($value) : trim('"', $value);
|
||||
}
|
||||
|
||||
$context = new Context($partialArgs);
|
||||
$context = new Context($this->_getPartialArguments($context, $args[0]));
|
||||
}
|
||||
|
||||
return $partial->render($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the arguments of a partial to actual array values to be used in a new context
|
||||
*
|
||||
* @param Context $context
|
||||
* @param array $args
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function _getPartialArguments(Context $context, array $args)
|
||||
{
|
||||
$partialArgs = array();
|
||||
|
||||
foreach ($args as $arg) {
|
||||
$parts = explode('=', $arg, 2);
|
||||
|
||||
$key = $parts[0];
|
||||
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 {
|
||||
$partialArgs[$key] = strpos($parts[1], '"') === false ? $context->get($parts[1]) : trim('"', $parts[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return $partialArgs;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if there is a helper with this variable name available or not.
|
||||
|
@ -715,7 +715,11 @@ EOM;
|
||||
public function testPartial()
|
||||
{
|
||||
$loader = new \Handlebars\Loader\StringLoader();
|
||||
$partialLoader = new \Handlebars\Loader\ArrayLoader(array('test' => '{{key}}', 'bar' => 'its foo'));
|
||||
$partialLoader = new \Handlebars\Loader\ArrayLoader(array(
|
||||
'test' => '{{key}}',
|
||||
'bar' => 'its foo',
|
||||
'presetVariables' => '{{myVar}}',
|
||||
));
|
||||
$partialAliasses = array('foo' => 'bar');
|
||||
$engine = new \Handlebars\Handlebars(
|
||||
array(
|
||||
@ -733,6 +737,9 @@ EOM;
|
||||
|
||||
$this->setExpectedException('RuntimeException');
|
||||
$engine->render('{{>foo-again}}', array());
|
||||
|
||||
$this->assertEquals('foobar', $engine->render("{{>presetVariables myVar='foobar'}}", array()));
|
||||
$this->assertEquals('foobar=barbaz', $engine->render("{{>presetVariables myVar='foobar=barbaz'}}", array()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user