Skip to content

Commit

Permalink
support dynamic partial just like handlebars-lang/handlebars.js#941
Browse files Browse the repository at this point in the history
  • Loading branch information
Zordius Chen committed Jan 20, 2015
1 parent 0e66a83 commit 296ea89
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/lightncandy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -991,7 +992,7 @@ protected static function compileSubExpression($subExpression, &$context) {
* @return array<string> 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);
Expand Down Expand Up @@ -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);
Expand All @@ -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";
Expand Down
7 changes: 6 additions & 1 deletion tests/regressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '{{.}}'),
),
Expand Down

0 comments on commit 296ea89

Please sign in to comment.