mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-05-03 18:43:07 +03:00
fix #71
This commit is contained in:
parent
a9178d5f01
commit
84c6e3006c
@ -52,6 +52,9 @@ class Parser
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper method for recursively building a parse tree.
|
* Helper method for recursively building a parse tree.
|
||||||
|
* Trim right and trim left is a bit tricky here.
|
||||||
|
* {{#begin~}}{{TOKEN}}, TOKEN.. {{LAST}}{{~/begin}} is translated to:
|
||||||
|
* {{#begin}}{{~TOKEN}}, TOKEN.. {{LAST~}}{{/begin}}
|
||||||
*
|
*
|
||||||
* @param \ArrayIterator $tokens Stream of tokens
|
* @param \ArrayIterator $tokens Stream of tokens
|
||||||
*
|
*
|
||||||
@ -71,7 +74,7 @@ class Parser
|
|||||||
if ($token !== null) {
|
if ($token !== null) {
|
||||||
switch ($token[Tokenizer::TYPE]) {
|
switch ($token[Tokenizer::TYPE]) {
|
||||||
case Tokenizer::T_END_SECTION:
|
case Tokenizer::T_END_SECTION:
|
||||||
$newNodes = array();
|
$newNodes = array($token);
|
||||||
do {
|
do {
|
||||||
$result = array_pop($stack);
|
$result = array_pop($stack);
|
||||||
if ($result === null) {
|
if ($result === null) {
|
||||||
@ -84,6 +87,17 @@ class Parser
|
|||||||
&& isset($result[Tokenizer::NAME])
|
&& isset($result[Tokenizer::NAME])
|
||||||
&& $result[Tokenizer::NAME] == $token[Tokenizer::NAME]
|
&& $result[Tokenizer::NAME] == $token[Tokenizer::NAME]
|
||||||
) {
|
) {
|
||||||
|
if (isset($result[Tokenizer::TRIM_RIGHT]) && $result[Tokenizer::TRIM_RIGHT]) {
|
||||||
|
// If the start node has trim right, then its equal with the first item in the loop with
|
||||||
|
// Trim left
|
||||||
|
$newNodes[0][Tokenizer::TRIM_LEFT] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($token[Tokenizer::TRIM_RIGHT]) && $token[Tokenizer::TRIM_RIGHT]) {
|
||||||
|
//OK, if we have trim right here, we should pass it to the upper level.
|
||||||
|
$result[Tokenizer::TRIM_RIGHT] = true;
|
||||||
|
}
|
||||||
|
|
||||||
$result[Tokenizer::NODES] = $newNodes;
|
$result[Tokenizer::NODES] = $newNodes;
|
||||||
$result[Tokenizer::END] = $token[Tokenizer::INDEX];
|
$result[Tokenizer::END] = $token[Tokenizer::INDEX];
|
||||||
array_push($stack, $result);
|
array_push($stack, $result);
|
||||||
@ -92,7 +106,7 @@ class Parser
|
|||||||
array_unshift($newNodes, $result);
|
array_unshift($newNodes, $result);
|
||||||
}
|
}
|
||||||
} while (true);
|
} while (true);
|
||||||
// There is no break here, since we need the end token to handle the whitespace trim
|
break;
|
||||||
default:
|
default:
|
||||||
array_push($stack, $token);
|
array_push($stack, $token);
|
||||||
}
|
}
|
||||||
|
@ -162,8 +162,12 @@ class Template
|
|||||||
|
|
||||||
$tmp = $this->_renderInternal($current, $context);
|
$tmp = $this->_renderInternal($current, $context);
|
||||||
|
|
||||||
if ($rTrim) {
|
if (isset($current[Tokenizer::TRIM_LEFT]) && $current[Tokenizer::TRIM_LEFT]) {
|
||||||
$tmp = ltrim($tmp);
|
$tmp = rtrim($tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($rTrim || (isset($current[Tokenizer::TRIM_RIGHT]) && $current[Tokenizer::TRIM_RIGHT])) {
|
||||||
|
$tmp = ltrim($tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
$buffer .= $tmp;
|
$buffer .= $tmp;
|
||||||
|
@ -95,7 +95,7 @@ class Tokenizer
|
|||||||
const VALUE = 'value';
|
const VALUE = 'value';
|
||||||
const ARGS = 'args';
|
const ARGS = 'args';
|
||||||
const TRIM_LEFT = 'tleft';
|
const TRIM_LEFT = 'tleft';
|
||||||
const TRIM_RIGHT = 'rleft';
|
const TRIM_RIGHT = 'tright';
|
||||||
|
|
||||||
protected $state;
|
protected $state;
|
||||||
protected $tagType;
|
protected $tagType;
|
||||||
|
@ -157,6 +157,11 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|||||||
array(),
|
array(),
|
||||||
'var jsVar = "A \"quoted\" text";',
|
'var jsVar = "A \"quoted\" text";',
|
||||||
),
|
),
|
||||||
|
array(
|
||||||
|
'{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}',
|
||||||
|
array('first' => false, 'second' => true),
|
||||||
|
'The second'
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,7 +289,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|||||||
'fail'
|
'fail'
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
' {{~#if 1}}OK {{~else~}} NO {{~/if~}} END',
|
' {{~#if 1}}OK {{~else~}} NO {{~/if~}} END',
|
||||||
array(),
|
array(),
|
||||||
'OKEND'
|
'OKEND'
|
||||||
),
|
),
|
||||||
@ -312,6 +317,25 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|||||||
'{{= (( )) =}}((#if 1))OK((else))NO((/if))',
|
'{{= (( )) =}}((#if 1))OK((else))NO((/if))',
|
||||||
array(),
|
array(),
|
||||||
'OK'
|
'OK'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'{{#each data~}} {{this}} {{~/each}}',
|
||||||
|
array('data' => array(1, 2, 3, 4)),
|
||||||
|
'1234'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'{{#each data}}{{this}} {{~/each}}',
|
||||||
|
array('data' => array(1, 2, 3, 4)),
|
||||||
|
'1234'
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'{{#each data~}} {{this}}{{/each}}',
|
||||||
|
array('data' => array(1, 2, 3, 4)),
|
||||||
|
'1234'
|
||||||
|
),
|
||||||
|
array( '{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}',
|
||||||
|
array('first' => false, 'second' => true),
|
||||||
|
'The second'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user