mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Done #83
This commit is contained in:
@@ -134,24 +134,23 @@ class Compiler
|
||||
*/
|
||||
public static function foreachOpen(Tokenizer $tokens, Tag $scope)
|
||||
{
|
||||
$p = array("index" => false, "first" => false, "last" => false);
|
||||
$key = null;
|
||||
$before = $body = array();
|
||||
$p = array("index" => false, "first" => false, "last" => false);
|
||||
$key = null;
|
||||
$before = $body = array();
|
||||
$prepend = "";
|
||||
if ($tokens->is(T_VARIABLE)) {
|
||||
$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);
|
||||
$scope["var"] = $scope->tpl->tmpVar();
|
||||
$prepend = $scope["var"].' = (array)('.$from.')';
|
||||
$from = $check = $scope["var"];
|
||||
$count = 0;
|
||||
$from = $scope->tpl->parseArray($tokens, $count);
|
||||
$check = $count;
|
||||
} else {
|
||||
throw new UnexpectedTokenException($tokens, null, "tag {foreach}");
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ class Template extends Render
|
||||
|
||||
/**
|
||||
* @param string $tag
|
||||
* @return bool|\Fenom\Scope
|
||||
* @return bool|\Fenom\Tag
|
||||
*/
|
||||
public function getParentScope($tag)
|
||||
{
|
||||
@@ -1281,35 +1281,28 @@ class Template extends Render
|
||||
public function parseArray(Tokenizer $tokens, &$count = 0)
|
||||
{
|
||||
if ($tokens->is("[")) {
|
||||
$_arr = "array(";
|
||||
$key = $val = false;
|
||||
$arr = array();
|
||||
$tokens->next();
|
||||
while ($tokens->valid()) {
|
||||
if ($tokens->is(',') && $val) {
|
||||
$key = true;
|
||||
$val = false;
|
||||
$_arr .= $tokens->getAndNext() . ' ';
|
||||
} elseif ($tokens->is(Tokenizer::MACRO_SCALAR, T_VARIABLE, T_STRING, T_EMPTY, T_ISSET, "(") && !$val) {
|
||||
$_arr .= $this->parseExpr($tokens);
|
||||
$key = false;
|
||||
$val = true;
|
||||
} elseif ($tokens->is('"') && !$val) {
|
||||
$_arr .= $this->parseQuote($tokens);
|
||||
$key = false;
|
||||
$val = true;
|
||||
} elseif ($tokens->is(T_DOUBLE_ARROW) && $val) {
|
||||
$_arr .= ' ' . $tokens->getAndNext() . ' ';
|
||||
$key = true;
|
||||
$val = false;
|
||||
} elseif (!$val && $tokens->is('[')) {
|
||||
$_arr .= $this->parseArray($tokens);
|
||||
$key = false;
|
||||
$val = true;
|
||||
} elseif ($tokens->is(']') && (!$key || $tokens->prev[0] === ',')) {
|
||||
if ($tokens->is(']')) {
|
||||
$tokens->next();
|
||||
return $_arr . ')';
|
||||
return 'array(' . implode(', ', $arr) . ')';
|
||||
}
|
||||
if ($tokens->is('[')) {
|
||||
$arr[] = $this->parseArray($tokens);
|
||||
$count++;
|
||||
} else {
|
||||
break;
|
||||
$expr = $this->parseExpr($tokens);
|
||||
if($tokens->is(T_DOUBLE_ARROW)) {
|
||||
$tokens->next();
|
||||
$arr[] = $expr.' => '.$this->parseExpr($tokens);
|
||||
} else {
|
||||
$arr[] = $expr;
|
||||
}
|
||||
$count++;
|
||||
}
|
||||
if($tokens->is(',')) {
|
||||
$tokens->next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user