Skip to content

Commit

Permalink
try to use different partial block id to prevent different partialblo…
Browse files Browse the repository at this point in the history
…ck issue
  • Loading branch information
Zordius Chen committed Feb 1, 2016
1 parent eea05ef commit 9546854
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public static function composePHPRender($context, $code) {
'scopes' => array(),
'sp_vars' => isset(\$options['data']) ? array_merge(array('root' => \$in), \$options['data']) : array('root' => \$in),
'blparam' => array(),
'partialid' => 0,
'runtime' => '{$context['runtime']}',
);
{$context['renderex']}
Expand Down Expand Up @@ -341,6 +342,7 @@ protected static function compileToken(&$context, $info) {
*/
public static function partial(&$context, $vars) {
Parser::getBlockParams($vars);
$pid = Parser::getPartialBlock($vars);
$p = array_shift($vars);
if ($context['flags']['runpart']) {
if (!isset($vars[0])) {
Expand All @@ -354,7 +356,7 @@ public static function partial(&$context, $vars) {
$p = "'$p[0]'";
}
$sp = $context['tokens']['partialind'] ? ", '{$context['tokens']['partialind']}'" : '';
return $context['ops']['seperator'] . static::getFuncName($context, 'p', $tag) . "\$cx, $p, $v[0]$sp){$context['ops']['seperator']}";
return $context['ops']['seperator'] . static::getFuncName($context, 'p', $tag) . "\$cx, $p, $v[0],$pid$sp){$context['ops']['seperator']}";
}
return isset($context['usedPartial'][$p[0]]) ? "{$context['ops']['seperator']}'" . Partial::compileStatic($context, $p[0]) . "'{$context['ops']['seperator']}" : $context['ops']['seperator'];
}
Expand Down
2 changes: 2 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public static function create($options) {
'delimiter' => 0,
'subexp' => 0,
'rawblock' => 0,
'pblock' => 0,
'lookup' => 0,
'log' => 0,
),
Expand Down Expand Up @@ -217,6 +218,7 @@ protected static function updateHelperTable(&$context, $options, $tname = 'helpe
*/
public static function merge(&$context, $tmp) {
$context['error'] = $tmp['error'];
$context['partials'] = $tmp['partials'];
$context['partialCode'] = $tmp['partialCode'];
$context['partialStack'] = $tmp['partialStack'];
$context['usedCount'] = $tmp['usedCount'];
Expand Down
18 changes: 18 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,27 @@ class Parser extends Token
{
// Compile time error handling flags
const BLOCKPARAM = 9999;
const PARTIALBLOCK = 9998;
const LITERAL = -1;
const SUBEXP = -2;

/**
* Get partial block id and fix the variable list
*
* @param array<boolean|integer|array> $vars parsed token
*
* @return integer Return partial block id
*
*/
public static function getPartialBlock(&$vars) {
if (isset($vars[static::PARTIALBLOCK])) {
$id = $vars[static::PARTIALBLOCK];
unset($vars[static::PARTIALBLOCK]);
return $id;
}
return 0;
}

/**
* Get block params and fix the variable list
*
Expand Down
3 changes: 3 additions & 0 deletions src/Partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ protected static function prePartial(&$context, $tmpl, &$name) {
* @return string|null $content partial content
*/
public static function resolve(&$context, &$name) {
if ($name === '@partial-block') {
$name = "@partial-block{$context['usedFeature']['pblock']}";
}
if (isset($context['partials'][$name])) {
return static::prePartial($context, $context['partials'][$name], $name);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,12 +480,18 @@ public static function m($cx, $a, $b) {
* @return string The rendered string of the partial
*
*/
public static function p($cx, $p, $v, $sp = '') {
public static function p($cx, $p, $v, $pid, $sp = '') {
if ($p === '@partial-block') {
$p = "$p" . ($pid || $cx['partialid']);
}

if (!isset($cx['partials'][$p])) {
static::err($cx, "Can not find partial named as '$p' !!");
return '';
}

$cx['partialid'] = $pid;

return call_user_func($cx['partials'][$p], $cx, static::m($cx, $v[0][0], $v[1]), $sp);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ protected static function operator($operator, &$context, &$vars) {
case '#>':
static::pushLeft($context);
$context['stack'][] = count($context['parsed'][0]);
$vars[Parser::PARTIALBLOCK] = ++$context['usedFeature']['pblock'];
static::pushStack($context, '#>', $vars);
array_unshift($context['partialblock'], '');
return static::partial($context, $vars);
Expand Down Expand Up @@ -277,7 +278,10 @@ protected static function partialBlock(&$context, $vars) {
if (static::blockEnd($context, $vars, '#>') !== null) {
$c = $context['stack'][count($context['stack']) - 4];
$found = Partial::resolve($context, $vars[0][0]) !== null;
$v = $found ? '@partial-block' : $vars[0][0];
$v = $found ? "@partial-block{$context['usedFeature']['pblock']}" : "{$vars[0][0]}";
if ($found) {
$context['partials'][$v] = $context['partialblock'][0];
}
$context['usedPartial'][$v] = $context['partialblock'][0];
Partial::compileDynamic($context, $v);
if ($found) {
Expand Down
1 change: 1 addition & 0 deletions tests/usageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function compileProvider()
'delimiter' => 0,
'subexp' => 0,
'rawblock' => 0,
'pblock' => 0,
'lookup' => 0,
'log' => 0,
);
Expand Down

0 comments on commit 9546854

Please sign in to comment.