Commit 1fd82315 by Seldaek

+ Syntax: Added support for {$foo+=5}, {$foo="a"}, {$foo++} and {$foo--}

+ Syntax: Added shortcut for $dwoo.*, you can now use {$.const.FOO} instead of {$dwoo.const.FOO} for example, applies to all $dwoo.* vars * Fixes some broken tests and adds a bunch (100 tests reached, hurray) git-svn-id: svn://dwoo.org/dwoo/trunk@27 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 04c8b0ec
...@@ -4,6 +4,11 @@ ...@@ -4,6 +4,11 @@
directly to emulate get(). This was done to reduce some overhead directly to emulate get(). This was done to reduce some overhead
+ Plugins: Added {extends} and {block} to handle template inheritance, read + Plugins: Added {extends} and {block} to handle template inheritance, read
more about it at http://wiki.dwoo.org/index.php/TemplateInheritance more about it at http://wiki.dwoo.org/index.php/TemplateInheritance
+ Plugins: Added {do} that executes whatever you feed it whitout echoing the
result, used internally for extends but you can use it if required
+ Syntax: Added support for {$foo+=5}, {$foo="a"}, {$foo++} and {$foo--}
+ Syntax: Added shortcut for $dwoo.*, you can now use {$.const.FOO} instead of
{$dwoo.const.FOO} for example, applies to all $dwoo.* vars
+ API: Added getSource(), getUid() and getResourceIdentifier() to DwooITemplate + API: Added getSource(), getUid() and getResourceIdentifier() to DwooITemplate
+ API: Added setSecurityPolicy() too DwooICompiler and modified the arguments + API: Added setSecurityPolicy() too DwooICompiler and modified the arguments
of its compile() method of its compile() method
......
...@@ -329,15 +329,15 @@ class Dwoo ...@@ -329,15 +329,15 @@ class Dwoo
{ {
if($_output === true) if($_output === true)
{ {
include $file; readfile($file);
$this->template = null; $this->template = null;
} }
else else
{ {
ob_start(); //ob_start();
include $file;
$this->template = null; $this->template = null;
return ob_get_clean(); return file_get_contents($file);
//return ob_get_clean();
} }
} }
// no cache present // no cache present
...@@ -1214,7 +1214,10 @@ class Dwoo ...@@ -1214,7 +1214,10 @@ class Dwoo
return $cur[$varstr]; return $cur[$varstr];
else else
return null; return null;
} }
if(substr($varstr, 0, 1) === '.')
$varstr = 'dwoo'.$varstr;
preg_match_all('#(\[|->|\.)?([a-z0-9_]+)\]?#i', $varstr, $m); preg_match_all('#(\[|->|\.)?([a-z0-9_]+)\]?#i', $varstr, $m);
} }
......
...@@ -981,7 +981,7 @@ class DwooCompiler implements DwooICompiler ...@@ -981,7 +981,7 @@ class DwooCompiler implements DwooICompiler
$parsingParams[] = $output; $parsingParams[] = $output;
return $parsingParams; return $parsingParams;
} }
elseif($substr!=='' && (is_array($parsingParams) || $curBlock === 'namedparam')) // unquoted string, bool or number elseif($substr!=='' && (is_array($parsingParams) || $curBlock === 'namedparam' || $curBlock === 'condition')) // unquoted string, bool or number
{ {
return $this->parseOthers($in, $from, $to, $parsingParams, $curBlock, $pointer); return $this->parseOthers($in, $from, $to, $parsingParams, $curBlock, $pointer);
} }
...@@ -1366,9 +1366,9 @@ class DwooCompiler implements DwooICompiler ...@@ -1366,9 +1366,9 @@ class DwooCompiler implements DwooICompiler
{ {
$substr = substr($in, $from, $to-$from); $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==='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==='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 ($curBlock!=='modifier'? '((?:\|(?:@?[a-z0-9_]+(?:(?::("|\').+?\5|:[^\s`"\']*))*))+)?':'(())') . // modifiers
'#i', $substr, $match)) '#i', $substr, $match))
{ {
...@@ -1453,6 +1453,7 @@ class DwooCompiler implements DwooICompiler ...@@ -1453,6 +1453,7 @@ class DwooCompiler implements DwooICompiler
$output = $this->parseVarKey($key, $curBlock); $output = $this->parseVarKey($key, $curBlock);
} }
// methods
if($hasMethodCall) if($hasMethodCall)
{ {
preg_match_all('{->([a-z0-9_]+)(\([^)]*\))?}i', $methodCall, $calls); preg_match_all('{->([a-z0-9_]+)(\([^)]*\))?}i', $methodCall, $calls);
...@@ -1474,13 +1475,35 @@ class DwooCompiler implements DwooICompiler ...@@ -1474,13 +1475,35 @@ class DwooCompiler implements DwooICompiler
} }
} }
// expressions
if($hasExpression) if($hasExpression)
{ {
preg_match_all('#(?:([+/*%-])(\$[a-z0-9.[\]>_:-]+(?:\([^)]*\))?|[0-9.,]*))#i', $match[3], $expMatch); preg_match_all('#(?:([+/*%=-])(=?-?\$[a-z0-9.[\]>_:-]+(?:\([^)]*\))?|=?-?[0-9.,]+|\1))#i', $match[3], $expMatch);
foreach($expMatch[1] as $k=>$operator) foreach($expMatch[1] as $k=>$operator)
{ {
if(substr($expMatch[2][$k], 0, 1) === '$') if(substr($expMatch[2][$k],0,1)==='=')
{
$assign = true;
if($operator === '=')
$this->triggerError('Invalid expression, <em>'.$substr.'</em>, can not use "==" in expressions', E_USER_ERROR);
if($curBlock !== 'root')
$this->triggerError('Invalid expression, <em>'.$substr.'</em>, "=" can only be used in pure expressions like {$foo+=3}, {$foo="bar"}', E_USER_ERROR);
$operator .= '=';
$expMatch[2][$k] = substr($expMatch[2][$k], 1);
}
if(substr($expMatch[2][$k],0,1)==='-' && strlen($expMatch[2][$k]) > 1)
{
$operator .= '-';
$expMatch[2][$k] = substr($expMatch[2][$k], 1);
}
if(($operator==='+'||$operator==='-') && $expMatch[2][$k]===$operator)
{
$output = '('.$output.$operator.$operator.')';
break;
}
elseif(substr($expMatch[2][$k], 0, 1) === '$')
{ {
$output = '('.$output.' '.$operator.' '.$this->parseVar($expMatch[2][$k], 0, strlen($expMatch[2][$k]), false, 'expression').')'; $output = '('.$output.' '.$operator.' '.$this->parseVar($expMatch[2][$k], 0, strlen($expMatch[2][$k]), false, 'expression').')';
} }
...@@ -1490,6 +1513,27 @@ class DwooCompiler implements DwooICompiler ...@@ -1490,6 +1513,27 @@ class DwooCompiler implements DwooICompiler
$this->triggerError('Unfinished expression, <em>'.$substr.'</em>, missing var or number after math operator', E_USER_ERROR); $this->triggerError('Unfinished expression, <em>'.$substr.'</em>, missing var or number after math operator', E_USER_ERROR);
} }
} }
// var assignment
elseif($curBlock === 'root' && substr(trim(substr($substr, $matchedLength)), 0, 1) === '=')
{
$value = trim(substr(trim(substr($substr, $matchedLength)), 1));
$parts = array();
$parts = $this->parse($value, 0, strlen($value), $parts, 'condition');
// load if plugin
try {
$this->getPluginType('if');
} catch (DwooException $e) {
$this->triggerError('Assignments require the "if" plugin to be accessible', E_USER_ERROR);
}
$parts = $this->mapParams($parts, array('DwooPlugin_if', 'init'), 1);
$value = DwooPlugin_if::replaceKeywords($parts, $this);
$output .= '='.implode(' ',$value);
$assign = true;
}
// handle modifiers // handle modifiers
if($curBlock !== 'modifier' && $hasModifiers) if($curBlock !== 'modifier' && $hasModifiers)
...@@ -1510,6 +1554,8 @@ class DwooCompiler implements DwooICompiler ...@@ -1510,6 +1554,8 @@ class DwooCompiler implements DwooICompiler
return $output; return $output;
elseif(substr($output, 0, strlen(self::PHP_OPEN)) === self::PHP_OPEN) elseif(substr($output, 0, strlen(self::PHP_OPEN)) === self::PHP_OPEN)
return $output; return $output;
elseif(isset($assign))
return self::PHP_OPEN.$output.';'.self::PHP_CLOSE;
else else
return self::PHP_OPEN.'echo '.$output.';'.self::PHP_CLOSE; return self::PHP_OPEN.'echo '.$output.';'.self::PHP_CLOSE;
} }
...@@ -1533,6 +1579,8 @@ class DwooCompiler implements DwooICompiler ...@@ -1533,6 +1579,8 @@ class DwooCompiler implements DwooICompiler
*/ */
protected function parseVarKey($key, $curBlock) protected function parseVarKey($key, $curBlock)
{ {
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)) if(preg_match('#dwoo\.(get|post|server|cookies|session|env|request)((?:\.[a-z0-9_-]+)+)#i', $key, $m))
{ {
$global = strtoupper($m[1]); $global = strtoupper($m[1]);
......
...@@ -40,13 +40,14 @@ class DwooPlugin_for extends DwooBlockPlugin implements DwooICompilableBlock ...@@ -40,13 +40,14 @@ class DwooPlugin_for extends DwooBlockPlugin implements DwooICompilableBlock
// evaluates which global variables have to be computed // evaluates which global variables have to be computed
$varName = '$dwoo.for.'.trim($name, '"\'').'.'; $varName = '$dwoo.for.'.trim($name, '"\'').'.';
$usesAny = strpos($tpl, $varName) !== false; $shortVarName = '$.for.'.trim($name, '"\'').'.';
$usesFirst = strpos($tpl, $varName.'first') !== false; $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false;
$usesLast = strpos($tpl, $varName.'last') !== false; $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false;
$usesIndex = strpos($tpl, $varName.'index') !== false; $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false;
$usesIteration = $usesFirst || $usesLast || strpos($tpl, $varName.'iteration') !== false; $usesIndex = strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false;
$usesShow = strpos($tpl, $varName.'show') !== false; $usesIteration = $usesFirst || $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false;
$usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false; $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false;
$usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false;
// gets foreach id // gets foreach id
$cnt = self::$cnt++; $cnt = self::$cnt++;
......
...@@ -56,13 +56,14 @@ class DwooPlugin_foreach extends DwooBlockPlugin implements DwooICompilableBlock ...@@ -56,13 +56,14 @@ class DwooPlugin_foreach extends DwooBlockPlugin implements DwooICompilableBlock
// evaluates which global variables have to be computed // evaluates which global variables have to be computed
$varName = '$dwoo.foreach.'.trim($name, '"\'').'.'; $varName = '$dwoo.foreach.'.trim($name, '"\'').'.';
$usesAny = strpos($tpl, $varName) !== false; $shortVarName = '$.foreach.'.trim($name, '"\'').'.';
$usesFirst = strpos($tpl, $varName.'first') !== false; $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false;
$usesLast = strpos($tpl, $varName.'last') !== false; $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false;
$usesIndex = $usesFirst || strpos($tpl, $varName.'index') !== false; $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false;
$usesIteration = $usesLast || strpos($tpl, $varName.'iteration') !== false; $usesIndex = $usesFirst || strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false;
$usesShow = strpos($tpl, $varName.'show') !== false; $usesIteration = $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false;
$usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false; $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false;
$usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false;
// gets foreach id // gets foreach id
$cnt = self::$cnt++; $cnt = self::$cnt++;
......
...@@ -182,7 +182,7 @@ class BlockTests extends PHPUnit_Framework_TestCase ...@@ -182,7 +182,7 @@ class BlockTests extends PHPUnit_Framework_TestCase
public function testForeachWithGlobalVarsPreceding() public function testForeachWithGlobalVarsPreceding()
{ {
$tpl = new DwooTemplateString('{$dwoo.foreach.foo.total}{foreach $sub key item foo}{/foreach}'); $tpl = new DwooTemplateString('{if isset($dwoo.foreach.foo.total)}fail{/if}{foreach $sub key item foo}{/foreach}');
$tpl->forceCompilation(); $tpl->forceCompilation();
$this->assertEquals('', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler)); $this->assertEquals('', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler));
...@@ -263,12 +263,12 @@ works.bb', $this->dwoo->get($tpl, array(), $this->compiler)); ...@@ -263,12 +263,12 @@ works.bb', $this->dwoo->get($tpl, array(), $this->compiler));
public function testWith() public function testWith()
{ {
$tpl = new DwooTemplateString('{with $foo}{$a}{/with}-{$a}-{with $foo.b}mlsk{/with}'); $tpl = new DwooTemplateString('{with $foo}{$a}{/with}-{if $a}FAIL{/if}-{with $foo.b}mlsk{/with}');
$tpl->forceCompilation(); $tpl->forceCompilation();
$this->assertEquals('bar--', $this->dwoo->get($tpl, array('foo'=>array('a'=>'bar')), $this->compiler)); $this->assertEquals('bar--', $this->dwoo->get($tpl, array('foo'=>array('a'=>'bar')), $this->compiler));
$tpl = new DwooTemplateString('{with $foo}{$a.0}{with $a}{$0}{/with}{with $b}B{else}NOB{/with}{/with}-{$a}-{with $foo.b}mlsk{/with}{with $fooo}a{withelse}b{/with}'); $tpl = new DwooTemplateString('{with $foo}{$a.0}{with $a}{$0}{/with}{with $b}B{else}NOB{/with}{/with}-{if $a}FAIL{/if}-{with $foo.b}mlsk{/with}{with $fooo}a{withelse}b{/with}');
$tpl->forceCompilation(); $tpl->forceCompilation();
$this->assertEquals('barbarNOB--b', $this->dwoo->get($tpl, array('foo'=>array('a'=>array('bar'))), $this->compiler)); $this->assertEquals('barbarNOB--b', $this->dwoo->get($tpl, array('foo'=>array('a'=>array('bar'))), $this->compiler));
......
...@@ -197,7 +197,7 @@ class CompilerTests extends PHPUnit_Framework_TestCase ...@@ -197,7 +197,7 @@ class CompilerTests extends PHPUnit_Framework_TestCase
public function testNumberedIndexes() public function testNumberedIndexes()
{ {
$tpl = new DwooTemplateString('{$100}-{$150}-{$0}'); $tpl = new DwooTemplateString('{$100}-{$150}-{if $0}FAIL{/if}');
$tpl->forceCompilation(); $tpl->forceCompilation();
$this->assertEquals('bar-foo-', $this->dwoo->get($tpl, array('100'=>'bar', 150=>'foo'), $this->compiler)); $this->assertEquals('bar-foo-', $this->dwoo->get($tpl, array('100'=>'bar', 150=>'foo'), $this->compiler));
...@@ -236,6 +236,25 @@ class CompilerTests extends PHPUnit_Framework_TestCase ...@@ -236,6 +236,25 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->assertEquals('{ $a }moo{ $a}moo', $this->dwoo->get($tpl, array('a'=>'moo'), $this->compiler)); $this->assertEquals('{ $a }moo{ $a}moo', $this->dwoo->get($tpl, array('a'=>'moo'), $this->compiler));
} }
public function testDwooDotShortcut()
{
$tpl = new DwooTemplateString('{$.server.SCRIPT_NAME}{foreach $a item}{$.foreach.default.iteration}{$item}{$.foreach.$b.$c}{/foreach}');
$tpl->forceCompilation();
$this->assertEquals($_SERVER['SCRIPT_NAME'].'1a12b2', $this->dwoo->get($tpl, array('a'=>array('a','b'), 'b'=>'default', 'c'=>'iteration'), $this->compiler));
}
public function testAssignAndIncrement()
{
$tpl = new DwooTemplateString('{$foo}{$foo+=3}{$foo}
{$foo}{$foo-=3}{$foo}
{$foo++}{$foo++}{$foo}{$foo*=$foo}{$foo}
{$foo--}{$foo=5}{$foo}');
$tpl->forceCompilation();
$this->assertEquals("03\n30\n0124\n45", $this->dwoo->get($tpl, array('foo'=>0), $this->compiler));
}
} }
class MethodCallsHelper { class MethodCallsHelper {
......
<?php <?php
error_reporting(E_ALL|E_STRICT);
if(!ini_get('date.timezone'))
date_default_timezone_set('CET');
define('DWOO_CACHE_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'cache'); define('DWOO_CACHE_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'cache');
define('DWOO_COMPILE_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'compiled'); define('DWOO_COMPILE_DIRECTORY', dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR.'compiled');
require dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'Dwoo.php'; require dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'Dwoo.php';
......
...@@ -127,6 +127,14 @@ class FuncTests extends PHPUnit_Framework_TestCase ...@@ -127,6 +127,14 @@ class FuncTests extends PHPUnit_Framework_TestCase
$this->assertEquals("barbarfoo3", $this->dwoo->get($tpl, array('foo2'=>"", 'foo3'=>"foo3"), $this->compiler)); $this->assertEquals("barbarfoo3", $this->dwoo->get($tpl, array('foo2'=>"", 'foo3'=>"foo3"), $this->compiler));
} }
public function testDo()
{
$tpl = new DwooTemplateString('{do assign($foo bar)}{do $bar}{$bar}');
$tpl->forceCompilation();
$this->assertEquals("moo", $this->dwoo->get($tpl, array('foo'=>"moo"), $this->compiler));
}
public function testEscape() public function testEscape()
{ {
$tpl = new DwooTemplateString('{escape $foo}'); $tpl = new DwooTemplateString('{escape $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