This commit is contained in:
bzick 2014-06-28 16:38:50 +04:00
parent 7985c34c9c
commit 4773d77611
3 changed files with 24 additions and 22 deletions

View File

@ -138,13 +138,20 @@ class Compiler
$key = null;
$before = $body = array();
if ($tokens->is(T_VARIABLE)) {
$from = $scope->tpl->parseTerm($tokens);
$prepend = "";
$from = $scope->tpl->parseTerm($tokens, $is_var);
if($is_var) {
$check = '!empty('.$from.')';
$prepend = "";
} else {
$scope["var"] = $scope->tpl->tmpVar();
$prepend = $scope["var"].' = (array)('.$from.')';
$from = $check = $scope["var"];
}
} elseif ($tokens->is('[')) {
$from = $scope->tpl->parseArray($tokens);
$uid = '$v' . $scope->tpl->i++;
$prepend = $uid . ' = ' . $from . ';';
$from = $uid;
$scope["var"] = $scope->tpl->tmpVar();
$prepend = $scope["var"].' = (array)('.$from.')';
$from = $check = $scope["var"];
} else {
throw new UnexpectedTokenException($tokens, null, "tag {foreach}");
}
@ -189,9 +196,9 @@ class Compiler
$body = $body ? implode("; ", $body) . ";" : "";
$scope["after"] = $scope["after"] ? implode("; ", $scope["after"]) . ";" : "";
if ($key) {
return "$prepend if($from) { $before foreach($from as $key => $value) { $body";
return "$prepend if($check) { $before foreach($from as $key => $value) { $body";
} else {
return "$prepend if($from) { $before foreach($from as $value) { $body";
return "$prepend if($check) { $before foreach($from as $value) { $body";
}
}

View File

@ -1263,10 +1263,11 @@ class Template extends Render
* [1, 2.3, 5+7/$var, 'string', "str {$var+3} ing", $var2, []]
*
* @param Tokenizer $tokens
* @throws UnexpectedTokenException
* @param int $count amount of elements
* @throws Error\UnexpectedTokenException
* @return string
*/
public function parseArray(Tokenizer $tokens)
public function parseArray(Tokenizer $tokens, &$count = 0)
{
if ($tokens->is("[")) {
$_arr = "array(";
@ -1277,16 +1278,7 @@ class Template extends Render
$key = true;
$val = false;
$_arr .= $tokens->getAndNext() . ' ';
} elseif ($tokens->is(
Tokenizer::MACRO_SCALAR,
T_VARIABLE,
T_STRING,
T_EMPTY,
T_ISSET,
"(",
"#"
) && !$val
) {
} elseif ($tokens->is(Tokenizer::MACRO_SCALAR, T_VARIABLE, T_STRING, T_EMPTY, T_ISSET, "(") && !$val) {
$_arr .= $this->parseExpr($tokens);
$key = false;
$val = true;

View File

@ -1267,12 +1267,15 @@ class TemplateTest extends TestCase
);
}
public function _testSandbox()
/**
* @group sb
*/
public function testSandbox()
{
try {
var_dump(
$this->fenom->setOptions(0)->compileCode(
"{autoescape true}{test_block_function:raw}{\$html}{/test_block_function}{/autoescape}"
$this->fenom->compileCode(
'{var $a = [3, 5,6]}'
)->getBody()
);
} catch (\Exception $e) {