Skip to content

Commit 296ea89

Browse files
author
Zordius Chen
committed
support dynamic partial just like handlebars-lang/handlebars.js#941
1 parent 0e66a83 commit 296ea89

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/lightncandy.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class LightnCandy {
9292
// RegExps
9393
const VARNAME_SEARCH = '/(\\[[^\\]]+\\]|[^\\[\\]\\.]+)/';
9494
const EXTENDED_COMMENT_SEARCH = '/{{!--.*?--}}/s';
95+
const IS_SUBEXP_SEARCH = '/^\(.+\)$/';
9596

9697
// Positions of matched token
9798
const POS_LOTHER = 1;
@@ -506,7 +507,7 @@ protected static function readPartial($name, &$context) {
506507
return static::compilePartial($name, $context, $cnt);
507508
}
508509

509-
if (preg_match('/^\(.+\)$/', $name)) {
510+
if (preg_match(static::IS_SUBEXP_SEARCH, $name)) {
510511
if ($context['flags']['runpart']) {
511512
$context['usedFeature']['dynpartial']++;
512513
return;
@@ -991,7 +992,7 @@ protected static function compileSubExpression($subExpression, &$context) {
991992
* @return array<string> variable names
992993
*/
993994
protected static function getVariableNameOrSubExpression($var, &$context, $ishelper = false) {
994-
if (isset($var[0]) && preg_match('/^\(.+\)$/', $var[0])) {
995+
if (isset($var[0]) && preg_match(static::IS_SUBEXP_SEARCH, $var[0])) {
995996
return static::compileSubExpression($var[0], $context);
996997
}
997998
return static::getVariableName($var, $context, $ishelper);
@@ -1706,7 +1707,7 @@ protected static function compileSection(&$token, &$context, &$vars, $named) {
17061707
switch ($token[self::POS_OP]) {
17071708
case '>':
17081709
// mustache spec: ignore missing partial
1709-
if (!isset($context['usedPartial'][$vars[0][0]])) {
1710+
if (($context['usedFeature']['dynpartial'] === 0) && !isset($context['usedPartial'][$vars[0][0]])) {
17101711
return $context['ops']['seperator'];
17111712
}
17121713
$p = array_shift($vars);
@@ -1716,8 +1717,13 @@ protected static function compileSection(&$token, &$context, &$vars, $named) {
17161717
$v = static::getVariableNames($vars, $context, true);
17171718
$tag = ">$p[0] " .implode(' ', $v[1]);
17181719
if ($context['flags']['runpart']) {
1720+
if (preg_match(static::IS_SUBEXP_SEARCH, $p[0])) {
1721+
$p = static::compileSubExpression($p[0], $context)[0];
1722+
} else {
1723+
$p = "'$p[0]'";
1724+
}
17191725
$sp = $context['tokens']['partialind'] ? ", '{$context['tokens']['partialind']}'" : '';
1720-
return $context['ops']['seperator'] . static::getFuncName($context, 'p', $tag) . "\$cx, '$p[0]', $v[0]$sp){$context['ops']['seperator']}";
1726+
return $context['ops']['seperator'] . static::getFuncName($context, 'p', $tag) . "\$cx, $p, $v[0]$sp){$context['ops']['seperator']}";
17211727
} else {
17221728
if ($named || $v[0] !== 'array(array($in),array())') {
17231729
$context['error'][] = "Do not support {{{$tag}}}, you should do compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag";

tests/regressionTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,14 @@ public function issueProvider()
443443
),
444444

445445
Array(
446-
'template' => '{{> (foo) bar}}',
446+
'template' => '{{> (pname foo) bar}}',
447447
'data' => Array('bar' => 'OK! SUBEXP+PARTIAL!', 'foo' => Array('test/test3')),
448448
'options' => Array(
449+
'helpers' => Array(
450+
'pname' => function($arg) {
451+
return $arg[0];
452+
}
453+
),
449454
'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_RUNTIMEPARTIAL,
450455
'partials' => Array('test/test3' => '{{.}}'),
451456
),

0 commit comments

Comments
 (0)