mirror of
https://github.com/Mibew/handlebars.php.git
synced 2024-11-15 08:44:12 +03:00
Merge pull request #124 from thormeier/partial-support-and-error-reporting
Partial support and error reporting
This commit is contained in:
commit
071793118a
@ -142,7 +142,12 @@ class Arguments
|
|||||||
// Remove found argument from arguments string.
|
// Remove found argument from arguments string.
|
||||||
$current_str = ltrim(substr($current_str, strlen($matches[0])));
|
$current_str = ltrim(substr($current_str, strlen($matches[0])));
|
||||||
} else {
|
} else {
|
||||||
throw new \InvalidArgumentException('Malformed arguments string');
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'Malformed arguments string: "%s"',
|
||||||
|
$args_string
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,7 +197,10 @@ class Context
|
|||||||
if (count($this->stack) < $level) {
|
if (count($this->stack) < $level) {
|
||||||
if ($strict) {
|
if ($strict) {
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'can not find variable in context'
|
sprintf(
|
||||||
|
'Can not find variable in context: "%s"',
|
||||||
|
$variableName
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,9 +219,13 @@ class Context
|
|||||||
if (!$variableName) {
|
if (!$variableName) {
|
||||||
if ($strict) {
|
if ($strict) {
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'can not find variable in context'
|
sprintf(
|
||||||
|
'Can not find variable in context: "%s"',
|
||||||
|
$variableName
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
} elseif ($variableName == '.' || $variableName == 'this') {
|
} elseif ($variableName == '.' || $variableName == 'this') {
|
||||||
return $current;
|
return $current;
|
||||||
@ -228,7 +235,10 @@ class Context
|
|||||||
return $specialVariables[$variableName];
|
return $specialVariables[$variableName];
|
||||||
} elseif ($strict) {
|
} elseif ($strict) {
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'can not find variable in context'
|
sprintf(
|
||||||
|
'Can not find variable in context: "%s"',
|
||||||
|
$variableName
|
||||||
|
)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
return '';
|
return '';
|
||||||
@ -275,7 +285,12 @@ class Context
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($strict) {
|
if ($strict) {
|
||||||
throw new \InvalidArgumentException('can not find variable in context');
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'Can not find variable in context: "%s"',
|
||||||
|
var_export($variable, true)
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
@ -299,7 +314,12 @@ class Context
|
|||||||
$get_pattern = "/(?:" . $name_pattern . ")/";
|
$get_pattern = "/(?:" . $name_pattern . ")/";
|
||||||
|
|
||||||
if (!preg_match($check_pattern, $variableName)) {
|
if (!preg_match($check_pattern, $variableName)) {
|
||||||
throw new \InvalidArgumentException('variable name is invalid');
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'Variable name is invalid: "%s"',
|
||||||
|
$variableName
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
preg_match_all($get_pattern, $variableName, $matches);
|
preg_match_all($get_pattern, $variableName, $matches);
|
||||||
|
@ -460,7 +460,10 @@ class Handlebars
|
|||||||
{
|
{
|
||||||
if (!is_a($class, 'Handlebars\\Template', true)) {
|
if (!is_a($class, 'Handlebars\\Template', true)) {
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
'Custom template class must extend Template'
|
sprintf(
|
||||||
|
'Custom template class "%s" must extend Template',
|
||||||
|
$class
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +100,10 @@ class Helpers
|
|||||||
{
|
{
|
||||||
if (!is_callable($helper) && ! $helper instanceof Helper) {
|
if (!is_callable($helper) && ! $helper instanceof Helper) {
|
||||||
throw new \InvalidArgumentException(
|
throw new \InvalidArgumentException(
|
||||||
"$name Helper is not a callable or doesn't implement the Helper interface."
|
sprintf(
|
||||||
|
"%s Helper is not a callable or doesn't implement the Helper interface.",
|
||||||
|
$name
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$this->helpers[$name] = $helper;
|
$this->helpers[$name] = $helper;
|
||||||
@ -136,7 +139,12 @@ class Helpers
|
|||||||
public function call($name, Template $template, Context $context, $args, $source)
|
public function call($name, Template $template, Context $context, $args, $source)
|
||||||
{
|
{
|
||||||
if (!$this->has($name)) {
|
if (!$this->has($name)) {
|
||||||
throw new \InvalidArgumentException('Unknown helper: ' . $name);
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'Unknown helper: "%s"',
|
||||||
|
$name
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->helpers[$name] instanceof Helper) {
|
if ($this->helpers[$name] instanceof Helper) {
|
||||||
@ -171,7 +179,12 @@ class Helpers
|
|||||||
public function __get($name)
|
public function __get($name)
|
||||||
{
|
{
|
||||||
if (!$this->has($name)) {
|
if (!$this->has($name)) {
|
||||||
throw new \InvalidArgumentException('Unknown helper :' . $name);
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'Unknown helper: "%s"',
|
||||||
|
$name
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->helpers[$name];
|
return $this->helpers[$name];
|
||||||
@ -226,7 +239,12 @@ class Helpers
|
|||||||
public function remove($name)
|
public function remove($name)
|
||||||
{
|
{
|
||||||
if (!$this->has($name)) {
|
if (!$this->has($name)) {
|
||||||
throw new \InvalidArgumentException('Unknown helper: ' . $name);
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'Unknown helper: "%s"',
|
||||||
|
$name
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
unset($this->helpers[$name]);
|
unset($this->helpers[$name]);
|
||||||
|
@ -75,11 +75,21 @@ class InlineLoader implements Loader
|
|||||||
public function __construct($fileName, $offset)
|
public function __construct($fileName, $offset)
|
||||||
{
|
{
|
||||||
if (!is_file($fileName)) {
|
if (!is_file($fileName)) {
|
||||||
throw new \InvalidArgumentException('InlineLoader expects a valid filename.');
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'InlineLoader expects a valid filename, "%s" given.',
|
||||||
|
$fileName
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_int($offset) || $offset < 0) {
|
if (!is_int($offset) || $offset < 0) {
|
||||||
throw new \InvalidArgumentException('InlineLoader expects a valid file offset.');
|
throw new \InvalidArgumentException(
|
||||||
|
sprintf(
|
||||||
|
'InlineLoader expects a valid file offset, "%s" given.',
|
||||||
|
$offset
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->fileName = $fileName;
|
$this->fileName = $fileName;
|
||||||
@ -98,7 +108,7 @@ class InlineLoader implements Loader
|
|||||||
$this->loadTemplates();
|
$this->loadTemplates();
|
||||||
|
|
||||||
if (!array_key_exists($name, $this->templates)) {
|
if (!array_key_exists($name, $this->templates)) {
|
||||||
throw new \InvalidArgumentException("Template {$name} not found.");
|
throw new \InvalidArgumentException("Template $name not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->templates[$name];
|
return $this->templates[$name];
|
||||||
|
@ -79,7 +79,10 @@ class Parser
|
|||||||
$result = array_pop($stack);
|
$result = array_pop($stack);
|
||||||
if ($result === null) {
|
if ($result === null) {
|
||||||
throw new \LogicException(
|
throw new \LogicException(
|
||||||
'Unexpected closing tag: /' . $token[Tokenizer::NAME]
|
sprintf(
|
||||||
|
'Unexpected closing tag: /%s',
|
||||||
|
$token[Tokenizer::NAME]
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Handlebars;
|
namespace Handlebars;
|
||||||
|
use Handlebars\Arguments;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handlebars base template
|
* Handlebars base template
|
||||||
@ -31,6 +32,7 @@ namespace Handlebars;
|
|||||||
* @category Xamin
|
* @category Xamin
|
||||||
* @package Handlebars
|
* @package Handlebars
|
||||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||||
|
* @author Pascal Thormeier <pascal.thormeier@gmail.com>
|
||||||
* @copyright 2010-2012 (c) Justin Hileman
|
* @copyright 2010-2012 (c) Justin Hileman
|
||||||
* @copyright 2012 (c) ParsPooyesh Co
|
* @copyright 2012 (c) ParsPooyesh Co
|
||||||
* @license MIT <http://opensource.org/licenses/MIT>
|
* @license MIT <http://opensource.org/licenses/MIT>
|
||||||
@ -405,7 +407,10 @@ class Template
|
|||||||
$sectionVar = $context->get($sectionName, true);
|
$sectionVar = $context->get($sectionName, true);
|
||||||
} catch (\InvalidArgumentException $e) {
|
} catch (\InvalidArgumentException $e) {
|
||||||
throw new \RuntimeException(
|
throw new \RuntimeException(
|
||||||
$sectionName . ' is not registered as a helper'
|
sprintf(
|
||||||
|
'"%s" is not registered as a helper',
|
||||||
|
$sectionName
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
$buffer = '';
|
$buffer = '';
|
||||||
@ -462,7 +467,10 @@ class Template
|
|||||||
return $this->_mustacheStyleSection($context, $current);
|
return $this->_mustacheStyleSection($context, $current);
|
||||||
} else {
|
} else {
|
||||||
throw new \RuntimeException(
|
throw new \RuntimeException(
|
||||||
$sectionName . ' is not registered as a helper'
|
sprintf(
|
||||||
|
'"%s"" is not registered as a helper',
|
||||||
|
$sectionName
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -500,12 +508,39 @@ class Template
|
|||||||
$partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]);
|
$partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]);
|
||||||
|
|
||||||
if ($current[Tokenizer::ARGS]) {
|
if ($current[Tokenizer::ARGS]) {
|
||||||
$context = $context->get($current[Tokenizer::ARGS]);
|
$arguments = new Arguments($current[Tokenizer::ARGS]);
|
||||||
|
|
||||||
|
$context = new Context($this->_preparePartialArguments($context, $arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $partial->render($context);
|
return $partial->render($context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare the arguments of a partial to actual array values to be used in a new context
|
||||||
|
*
|
||||||
|
* @param Context $context Current context
|
||||||
|
* @param Arguments $arguments Arguments for partial
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function _preparePartialArguments(Context $context, Arguments $arguments)
|
||||||
|
{
|
||||||
|
$positionalArgs = array();
|
||||||
|
foreach ($arguments->getPositionalArguments() as $positionalArg) {
|
||||||
|
$contextArg = $context->get($positionalArg);
|
||||||
|
if (is_array($contextArg)) {
|
||||||
|
foreach ($contextArg as $key => $value) {
|
||||||
|
$positionalArgs[$key] = $value;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$positionalArgs[$positionalArg] = $contextArg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_merge($positionalArgs, $arguments->getNamedArguments());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if there is a helper with this variable name available or not.
|
* Check if there is a helper with this variable name available or not.
|
||||||
|
@ -176,7 +176,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|||||||
'{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}',
|
'{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}',
|
||||||
array('first' => false, 'second' => true),
|
array('first' => false, 'second' => true),
|
||||||
'The second'
|
'The second'
|
||||||
)
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -715,7 +715,11 @@ EOM;
|
|||||||
public function testPartial()
|
public function testPartial()
|
||||||
{
|
{
|
||||||
$loader = new \Handlebars\Loader\StringLoader();
|
$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');
|
$partialAliasses = array('foo' => 'bar');
|
||||||
$engine = new \Handlebars\Handlebars(
|
$engine = new \Handlebars\Handlebars(
|
||||||
array(
|
array(
|
||||||
@ -733,6 +737,9 @@ EOM;
|
|||||||
|
|
||||||
$this->setExpectedException('RuntimeException');
|
$this->setExpectedException('RuntimeException');
|
||||||
$engine->render('{{>foo-again}}', array());
|
$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