Commit f9676b0d by Seldaek

+ Added {$_key} var inside {loop} representing the current array key

+ Added new shortcuts : $ for current scope, $_ for $_parent and $__ for $_root + Added a 'data' argument to {include} to be able to feed data directly into it git-svn-id: svn://dwoo.org/dwoo/trunk@49 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 8207c1b0
......@@ -767,8 +767,10 @@ class Dwoo
*
* @param mixed $value the variable to check
* @param bool $checkIsEmpty if true, the function will also check if the array is empty
* @param bool $allowNonCountable if true, the function will return true if an object is not empty but does not
* implement Countable, by default a non-countable object is considered empty
* @param bool $allowNonCountable if true, the function will return true if
* an object is not empty but does not implement Countable, by default a
* non- countable object is considered empty, this setting is only used if
* $checkIsEmpty is true
* @return bool true if it's an array (and not empty) or false if it's not an array (or if it's empty)
*/
public function isArray($value, $checkIsEmpty=false, $allowNonCountable=false)
......@@ -1084,7 +1086,7 @@ class Dwoo
{
if($sep === '.' || $sep === '[' || $sep === '')
{
if((is_array($data) || ($data instanceof Iterator && $data instanceof ArrayAccess)) && isset($data[$m[2][$k]]))
if((is_array($data) || $data instanceof ArrayAccess) && isset($data[$m[2][$k]]))
$data = $data[$m[2][$k]];
else
return null;
......@@ -1153,12 +1155,12 @@ class Dwoo
{
return $this->globals;
}
elseif($varstr === '_root')
elseif($varstr === '_root' || $varstr === '__')
{
return $this->data;
$varstr = substr($varstr, 6);
}
elseif($varstr === '_parent')
elseif($varstr === '_parent' || $varstr === '_')
{
$varstr = '.'.$varstr;
$tree = $this->scopeTree;
......@@ -1233,13 +1235,13 @@ class Dwoo
array_shift($m[1]);
}
}
elseif($i === '_root')
elseif($i === '_root' || $i === '__')
{
$cur = $this->data;
array_shift($m[2]);
array_shift($m[1]);
}
elseif($i === '_parent')
elseif($i === '_parent' || $i === '_')
{
$tree = $this->scopeTree;
$cur = $this->data;
......@@ -1249,7 +1251,7 @@ class Dwoo
array_pop($tree);
array_shift($m[2]);
array_shift($m[1]);
if(current($m[2]) === '_parent')
if(current($m[2]) === '_parent' || current($m[2]) === '_')
continue;
while(($i = array_shift($tree)) !== null)
......@@ -1269,7 +1271,7 @@ class Dwoo
{
if($sep === '.' || $sep === '[' || $sep === '')
{
if((is_array($cur) || ($cur instanceof Iterator && $cur instanceof ArrayAccess)) && isset($cur[$m[2][$k]]))
if((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]]))
$cur = $cur[$m[2][$k]];
else
return null;
......@@ -1365,7 +1367,7 @@ class Dwoo
while(($bit = array_shift($scope)) !== null)
{
if($bit === '_parent')
if($bit === '_parent' || $bit === '_')
{
array_pop($this->scopeTree);
reset($this->scopeTree);
......@@ -1374,7 +1376,7 @@ class Dwoo
for($i=0;$i<$cnt;$i++)
$this->scope =& $this->scope[$this->scopeTree[$i]];
}
elseif($bit === '_root')
elseif($bit === '_root' || $bit === '__')
{
$this->scope =& $this->data;
$this->scopeTree = array();
......
......@@ -1441,7 +1441,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
{
$substr = substr($in, $from, $to-$from);
if(preg_match('#(\$?\.?[a-z0-9_:]+(?:(?:(?:\.|->)(?:[a-z0-9_:]+|(?R))|\[(?:[a-z0-9_:]+|(?R))\]))*)' . // var key
if(preg_match('#(\$?\.?[a-z0-9_:]*(?:(?:(?:\.|->)(?:[a-z0-9_:]+|(?R))|\[(?:[a-z0-9_:]+|(?R))\]))*)' . // var key
($curBlock==='root' || $curBlock==='function' || $curBlock==='condition' || $curBlock==='variable' || $curBlock==='expression' ? '(\([^)]*?\)(?:->[a-z0-9_]+(?:\([^)]*?\))?)*)?' : '()') . // method call
($curBlock==='root' || $curBlock==='function' || $curBlock==='condition' || $curBlock==='variable' || $curBlock==='string' ? '((?:(?:[+/*%=-])(?:(?<!=)=?-?[$%][a-z0-9.[\]>_:-]+(?:\([^)]*\))?|(?<!=)=?-?[0-9.,]*|[+-]))*)':'()') . // simple math expressions
($curBlock!=='modifier'? '((?:\|(?:@?[a-z0-9_]+(?:(?::("|\').+?\5|:[^\s`"\']*))*))+)?':'(())') . // modifiers
......@@ -1656,6 +1656,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler
*/
protected function parseVarKey($key, $curBlock)
{
if($key === '')
{
return '$this->scope';
}
if(substr($key, 0, 1) === '.')
$key = 'dwoo'.$key;
if(preg_match('#dwoo\.(get|post|server|cookies|session|env|request)((?:\.[a-z0-9_-]+)+)#i', $key, $m))
......@@ -1683,14 +1687,18 @@ class Dwoo_Compiler implements Dwoo_ICompiler
{
$output = '$this->globals';
}
elseif($key === '_root')
elseif($key === '_root' || $key === '__')
{
$output = '$this->data';
}
elseif($key === '_parent')
elseif($key === '_parent' || $key === '_')
{
$output = '$this->readParentVar(1)';
}
elseif($key === '_key')
{
$output = '$tmp_key';
}
else
{
if($curBlock === 'root')
......@@ -1704,7 +1712,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
preg_match_all('#(\[|->|\.)?([a-z0-9_]+)\]?#i', $key, $m);
$i = $m[2][0];
if($i === '_parent')
if($i === '_parent' || $i === '_')
{
$parentCnt = 0;
......@@ -1728,12 +1736,16 @@ class Dwoo_Compiler implements Dwoo_ICompiler
array_shift($m[2]);
array_shift($m[1]);
}
elseif($i === '_root')
elseif($i === '_root' || $i === '__')
{
$output = '$this->data';
array_shift($m[2]);
array_shift($m[1]);
}
elseif($i === '_key')
{
$output = '$tmp_key';
}
else
{
$output = '$this->scope';
......
......@@ -67,13 +67,13 @@ class Dwoo_Plugin_loop extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Blo
// checks if foreach must be looped
$out .= "\n".'if($this->isArray($_loop'.$cnt.'_data, true, true) === true)'."\n{";
// iterates over keys
$out .= "\n\t".'foreach($_loop'.$cnt.'_data as $this->scope["-loop-"])'."\n\t{";
$out .= "\n\t".'foreach($_loop'.$cnt.'_data as $tmp_key => $this->scope["-loop-"])'."\n\t{";
// updates properties
if($usesFirst)
$out .= "\n\t\t".'$_loop'.$cnt.'_glob["first"] = (string) ($_loop'.$cnt.'_glob["index"] === 0);';
if($usesLast)
$out .= "\n\t\t".'$_loop'.$cnt.'_glob["last"] = (string) ($_loop'.$cnt.'_glob["iteration"] === $_loop'.$cnt.'_glob["total"]);';
$out .= "\n\t\t".'$_loop'.$cnt.'_scope = $this->setScope("-loop-");'."\n// -- loop start output\n".Dwoo_Compiler::PHP_CLOSE;
$out .= "\n\t\t".'$_loop'.$cnt.'_scope = $this->setScope("-loop-");' . "\n// -- loop start output\n".Dwoo_Compiler::PHP_CLOSE;
// build post processing output and cache it
$postOut = Dwoo_Compiler::PHP_OPEN . "\n".'// -- loop end output'."\n\t\t".'$this->forceScope($_loop'.$cnt.'_scope);';
......
......@@ -19,7 +19,7 @@
* @date 2008-04-09
* @package Dwoo
*/
function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id = null, $compile_id = null, $assign = null, array $rest = array())
function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id = null, $compile_id = null, $data = '_root', $assign = null, array $rest = array())
{
if($file === '')
return;
......@@ -65,13 +65,16 @@ function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id =
elseif($include === false)
$dwoo->triggerError('Include : Including "'.$resource.':'.$identifier.'" was not allowed for an unknown reason.', E_USER_WARNING);
if(count($rest))
{
$vars = $rest;
}
if($dwoo->isArray($data))
$vars = $data;
elseif($dwoo->isArray($cache_time))
$vars = $cache_time;
else
$vars = $dwoo->readVar($data);
if(count($rest))
{
$vars = $dwoo->readVar('_parent');
$vars = $rest + $vars;
}
$out = $dwoo->get($include, $vars);
......
......@@ -70,7 +70,7 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->assertEquals('barbazzab', $this->dwoo->get($tpl, array('foo'=>array('bar','baz')), $this->compiler));
}
public function testDwoo_Func()
public function testDwooFunc()
{
$tpl = new Dwoo_Template_String('{upper($foo)}');
$tpl->forceCompilation();
......@@ -78,7 +78,7 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
}
public function testDwoo_Loose()
public function testDwooLoose()
{
$tpl = new Dwoo_Template_String('{upper $foo}');
$tpl->forceCompilation();
......@@ -271,7 +271,7 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->assertEquals('{ $a }moo{ $a}moo', $this->dwoo->get($tpl, array('a'=>'moo'), $this->compiler));
}
public function testDwoo_DotShortcut()
public function testDwooDotShortcut()
{
$tpl = new Dwoo_Template_String('{$.server.SCRIPT_NAME}{foreach $a item}{$.foreach.default.iteration}{$item}{$.foreach.$b.$c}{/foreach}');
$tpl->forceCompilation();
......@@ -279,6 +279,27 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->assertEquals($_SERVER['SCRIPT_NAME'].'1a12b2', $this->dwoo->get($tpl, array('a'=>array('a','b'), 'b'=>'default', 'c'=>'iteration'), $this->compiler));
}
public function testRootAndParentShortcut()
{
$tpl = new Dwoo_Template_String('{with $a}{$__.b}{$_.b}{$0}{/with}{$__.b}');
$tpl->forceCompilation();
$this->assertEquals('defaultdefaultadefault', $this->dwoo->get($tpl, array('a'=>array('a','b'), 'b'=>'default'), $this->compiler));
}
public function testCurrentScopeShortcut()
{
$tpl = new Dwoo_Template_String('{loop $}{$_key} > {loop $}{$}{/loop}{/loop}');
$tpl->forceCompilation();
$this->assertEquals('1 > ab2 > cd', $this->dwoo->get($tpl, array('1'=>array('a','b'), '2'=>array('c','d')), $this->compiler));
$tpl = new Dwoo_Template_String('{with $a}{$1}{/with}{loop $a}{$}{/loop}');
$tpl->forceCompilation();
$this->assertEquals('bab', $this->dwoo->get($tpl, array('a'=>array('a','b'), 'b'=>'default', 'c'=>'iteration'), $this->compiler));
}
public function testAssignAndIncrement()
{
$tpl = new Dwoo_Template_String('{$foo}{$foo+=3}{$foo}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment