From 296ea8926760e738cda83d61b56ea62b87dd9dbe Mon Sep 17 00:00:00 2001 From: Zordius Chen Date: Tue, 20 Jan 2015 19:08:39 +0800 Subject: [PATCH] support dynamic partial just like https://github.com/wycats/handlebars.js/pull/941 --- src/lightncandy.php | 14 ++++++++++---- tests/regressionTest.php | 7 ++++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/lightncandy.php b/src/lightncandy.php index b53ce076..591d14ea 100644 --- a/src/lightncandy.php +++ b/src/lightncandy.php @@ -92,6 +92,7 @@ class LightnCandy { // RegExps const VARNAME_SEARCH = '/(\\[[^\\]]+\\]|[^\\[\\]\\.]+)/'; const EXTENDED_COMMENT_SEARCH = '/{{!--.*?--}}/s'; + const IS_SUBEXP_SEARCH = '/^\(.+\)$/'; // Positions of matched token const POS_LOTHER = 1; @@ -506,7 +507,7 @@ protected static function readPartial($name, &$context) { return static::compilePartial($name, $context, $cnt); } - if (preg_match('/^\(.+\)$/', $name)) { + if (preg_match(static::IS_SUBEXP_SEARCH, $name)) { if ($context['flags']['runpart']) { $context['usedFeature']['dynpartial']++; return; @@ -991,7 +992,7 @@ protected static function compileSubExpression($subExpression, &$context) { * @return array variable names */ protected static function getVariableNameOrSubExpression($var, &$context, $ishelper = false) { - if (isset($var[0]) && preg_match('/^\(.+\)$/', $var[0])) { + if (isset($var[0]) && preg_match(static::IS_SUBEXP_SEARCH, $var[0])) { return static::compileSubExpression($var[0], $context); } return static::getVariableName($var, $context, $ishelper); @@ -1706,7 +1707,7 @@ protected static function compileSection(&$token, &$context, &$vars, $named) { switch ($token[self::POS_OP]) { case '>': // mustache spec: ignore missing partial - if (!isset($context['usedPartial'][$vars[0][0]])) { + if (($context['usedFeature']['dynpartial'] === 0) && !isset($context['usedPartial'][$vars[0][0]])) { return $context['ops']['seperator']; } $p = array_shift($vars); @@ -1716,8 +1717,13 @@ protected static function compileSection(&$token, &$context, &$vars, $named) { $v = static::getVariableNames($vars, $context, true); $tag = ">$p[0] " .implode(' ', $v[1]); if ($context['flags']['runpart']) { + if (preg_match(static::IS_SUBEXP_SEARCH, $p[0])) { + $p = static::compileSubExpression($p[0], $context)[0]; + } else { + $p = "'$p[0]'"; + } $sp = $context['tokens']['partialind'] ? ", '{$context['tokens']['partialind']}'" : ''; - return $context['ops']['seperator'] . static::getFuncName($context, 'p', $tag) . "\$cx, '$p[0]', $v[0]$sp){$context['ops']['seperator']}"; + return $context['ops']['seperator'] . static::getFuncName($context, 'p', $tag) . "\$cx, $p, $v[0]$sp){$context['ops']['seperator']}"; } else { if ($named || $v[0] !== 'array(array($in),array())') { $context['error'][] = "Do not support {{{$tag}}}, you should do compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag"; diff --git a/tests/regressionTest.php b/tests/regressionTest.php index e5804bcc..ce9fac3f 100644 --- a/tests/regressionTest.php +++ b/tests/regressionTest.php @@ -443,9 +443,14 @@ public function issueProvider() ), Array( - 'template' => '{{> (foo) bar}}', + 'template' => '{{> (pname foo) bar}}', 'data' => Array('bar' => 'OK! SUBEXP+PARTIAL!', 'foo' => Array('test/test3')), 'options' => Array( + 'helpers' => Array( + 'pname' => function($arg) { + return $arg[0]; + } + ), 'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_RUNTIMEPARTIAL, 'partials' => Array('test/test3' => '{{.}}'), ),