Commit 1d3732c8 by Seldaek

+ Plugins: Added {a} to build 'a' html tags

+ Plugins: Added {tif} that acts as a ternary if / allows you to use a ternary operator inside it * The compiler now allows \r, \n and \t to be parameter splitters as well as "," and " ". You can therefore make complex function calls on multiple lines git-svn-id: svn://dwoo.org/dwoo/trunk@84 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 4b6eace8
[2008--] 0.9.2 [2008--] 0.9.2
+ Plugins: Added {a} to build 'a' html tags
+ Plugins: Added {tif} that acts as a ternary if / allows you to use a ternary
operator inside it
* The compiler now allows \r, \n and \t to be parameter splitters as well as
"," and " ". You can therefore make complex function calls on multiple lines
* Converted most of the code to follow PEAR Coding Standards, hopefully this * Converted most of the code to follow PEAR Coding Standards, hopefully this
didn't break anything that the tests missed didn't break anything that the tests missed
......
...@@ -692,9 +692,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -692,9 +692,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler
if ($this->debug) { if ($this->debug) {
echo '<hr><pre>'; echo '<hr><pre>';
$lines = preg_split('{\r\n|\n}', htmlentities($output)); $lines = preg_split('{\r\n|\n}', highlight_string(($output), true));
foreach ($lines as $i=>$line) foreach ($lines as $i=>$line) {
echo ($i+1).'. '.$line."\r\n"; echo ($i+1).'. '.$line."\r\n";
}
} }
if ($this->debug) echo '<hr></pre></pre>'; if ($this->debug) echo '<hr></pre></pre>';
...@@ -1106,7 +1107,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1106,7 +1107,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
break 2; break 2;
} }
if (($paramstr[$ptr] === ' ' || $paramstr[$ptr] === ',')) { if ($paramstr[$ptr] === ' ' || $paramstr[$ptr] === ',' || $paramstr[$ptr] === "\r" || $paramstr[$ptr] === "\n" || $paramstr[$ptr] === "\t") {
$ptr++; $ptr++;
} else { } else {
break; break;
...@@ -1115,13 +1116,13 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1115,13 +1116,13 @@ class Dwoo_Compiler implements Dwoo_ICompiler
if ($this->debug) echo 'FUNC START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>'; if ($this->debug) echo 'FUNC START PARAM PARSING WITH POINTER AT '.$ptr.'<br/>';
if ($func === 'if' || $func === 'elseif') { if ($func === 'if' || $func === 'elseif' || $func === 'tif') {
$params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'condition', $ptr); $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'condition', $ptr);
} else { } else {
$params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'function', $ptr); $params = $this->parse($paramstr, $ptr, strlen($paramstr), $params, 'function', $ptr);
} }
if ($this->debug) echo 'PARAM PARSED, POINTER AT '.$ptr.'<br/>'; if ($this->debug) echo 'PARAM PARSED, POINTER AT '.$ptr.' ('.substr($paramstr, $ptr-1, 3).')<br/>';
} }
$paramstr = substr($paramstr, 0, $ptr); $paramstr = substr($paramstr, 0, $ptr);
$state = 0; $state = 0;
...@@ -1307,10 +1308,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1307,10 +1308,10 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$substr = substr($in, $from, $to-$from); $substr = substr($in, $from, $to-$from);
$first = $substr[0]; $first = $substr[0];
if ($this->debug) echo 'STRING FOUND<br />'; if ($this->debug) echo 'STRING FOUND ('.$substr.')<br />';
$strend = false; $strend = false;
$o = $from+1; $o = $from+1;
while ($strend===false) { while ($strend === false) {
$strend = strpos($in, $first, $o); $strend = strpos($in, $first, $o);
if ($strend === false) { if ($strend === false) {
throw new Dwoo_Compilation_Exception('Unfinished string in : <strong>'.substr($in, 0, $from).'<u>'.substr($in, $from, $to-$from).'</u>'.substr($in, $to).'</strong>'); throw new Dwoo_Compilation_Exception('Unfinished string in : <strong>'.substr($in, 0, $from).'<u>'.substr($in, $from, $to-$from).'</u>'.substr($in, $to).'</strong>');
...@@ -1324,10 +1325,20 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1324,10 +1325,20 @@ class Dwoo_Compiler implements Dwoo_ICompiler
if ($strend !== false) { if ($strend !== false) {
$realend = $strend-$from+1; $realend = $strend-$from+1;
} }
$strend = strpos($in, ' ', $strend+1); $newend = strlen($in)-1;
if ($strend===false) { if (($tmpend = strpos($in, " ", $strend+1)) && $tmpend < $newend) {
$strend = strlen($in)-1; $newend = $tmpend;
}
if (($tmpend = strpos($in, "\t", $strend+1)) && $tmpend < $newend) {
$newend = $tmpend;
} }
if (($tmpend = strpos($in, "\r", $strend+1)) && $tmpend < $newend) {
$newend = $tmpend;
}
if (($tmpend = strpos($in, "\n", $strend+1)) && $tmpend < $newend) {
$newend = $tmpend;
}
$strend = $newend;
} }
$srcOutput = substr($in, $from, $strend+1-$from); $srcOutput = substr($in, $from, $strend+1-$from);
...@@ -1606,7 +1617,9 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1606,7 +1617,9 @@ class Dwoo_Compiler implements Dwoo_ICompiler
} }
$parts = $this->mapParams($parts, array('Dwoo_Plugin_if', 'init'), 1); $parts = $this->mapParams($parts, array('Dwoo_Plugin_if', 'init'), 1);
$value = Dwoo_Plugin_if::replaceKeywords($parts, $this); $parts = $this->getCompiledParams($parts);
$value = Dwoo_Plugin_if::replaceKeywords($parts['*'], $this);
$output .= '='.implode(' ', $value); $output .= '='.implode(' ', $value);
$assign = true; $assign = true;
...@@ -1838,11 +1851,11 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1838,11 +1851,11 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$end = strlen($substr); $end = strlen($substr);
if ($curBlock === 'condition') { if ($curBlock === 'condition') {
$breakChars = array('(', ')', ' ', '||', '&&', '|', '&', '>=', '<=', '===', '==', '=', '!==', '!=', '<<', '<', '>>', '>', '^', '~', ',', '+', '-', '*', '/', '%', '!'); $breakChars = array('(', ')', ' ', '||', '&&', '|', '&', '>=', '<=', '===', '==', '=', '!==', '!=', '<<', '<', '>>', '>', '^', '~', ',', '+', '-', '*', '/', '%', '!', '?', ':');
} elseif ($curBlock === 'modifier') { } elseif ($curBlock === 'modifier') {
$breakChars = array(' ', ',', ')', ':', '|'); $breakChars = array(' ', ',', ')', ':', '|', "\r", "\n", "\t");
} else { } else {
$breakChars = array(' ', ',', ')'); $breakChars = array(' ', ',', ')', "\r", "\n", "\t");
} }
$breaker = false; $breaker = false;
...@@ -1887,7 +1900,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler ...@@ -1887,7 +1900,7 @@ class Dwoo_Compiler implements Dwoo_ICompiler
$substr = '('.$substr.')'; $substr = '('.$substr.')';
} elseif ($curBlock === 'condition' && array_search($substr, $breakChars, true) !== false) { } elseif ($curBlock === 'condition' && array_search($substr, $breakChars, true) !== false) {
if ($this->debug) echo 'BREAKCHAR PARSED<br />'; if ($this->debug) echo 'BREAKCHAR PARSED<br />';
$substr = '"'.$substr.'"'; //$substr = '"'.$substr.'"';
} else { } else {
if ($this->debug) echo 'BLABBER CASTED AS STRING<br />'; if ($this->debug) echo 'BLABBER CASTED AS STRING<br />';
......
...@@ -42,7 +42,9 @@ class Dwoo_Plugin_elseif extends Dwoo_Plugin_if implements Dwoo_ICompilable_Bloc ...@@ -42,7 +42,9 @@ class Dwoo_Plugin_elseif extends Dwoo_Plugin_if implements Dwoo_ICompilable_Bloc
$out = substr($out, 0, -strlen(Dwoo_Compiler::PHP_CLOSE)); $out = substr($out, 0, -strlen(Dwoo_Compiler::PHP_CLOSE));
} }
return $out . " elseif (".implode(' ', self::replaceKeywords($params, $compiler)).") {\n" . Dwoo_Compiler::PHP_CLOSE; $params = $compiler->getCompiledParams($params);
return $out . " elseif (".implode(' ', self::replaceKeywords($params['*'], $compiler)).") {\n" . Dwoo_Compiler::PHP_CLOSE;
} }
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend='', $append='') public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend='', $append='')
......
...@@ -38,14 +38,11 @@ class Dwoo_Plugin_if extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block ...@@ -38,14 +38,11 @@ class Dwoo_Plugin_if extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
{ {
} }
public static function replaceKeywords($params, Dwoo_Compiler $compiler) public static function replaceKeywords(array $params, Dwoo_Compiler $compiler)
{ {
$params = $compiler->getCompiledParams($params);
$p = array(); $p = array();
$params = $params['*']; reset($params);
while (list($k,$v) = each($params)) { while (list($k,$v) = each($params)) {
switch($v) { switch($v) {
...@@ -66,6 +63,12 @@ class Dwoo_Plugin_if extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block ...@@ -66,6 +63,12 @@ class Dwoo_Plugin_if extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
case '"/"': case '"/"':
$p[] = trim($v, '"'); $p[] = trim($v, '"');
break; break;
case '"and"':
$p[] = '&&';
break;
case '"or"':
$p[] = '||';
break;
case '"=="': case '"=="':
case '"eq"': case '"eq"':
$p[] = '=='; $p[] = '==';
...@@ -171,7 +174,9 @@ class Dwoo_Plugin_if extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block ...@@ -171,7 +174,9 @@ class Dwoo_Plugin_if extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
$currentBlock =& $compiler->getCurrentBlock(); $currentBlock =& $compiler->getCurrentBlock();
$currentBlock['params']['postOutput'] = Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE; $currentBlock['params']['postOutput'] = Dwoo_Compiler::PHP_OPEN."\n}".Dwoo_Compiler::PHP_CLOSE;
return Dwoo_Compiler::PHP_OPEN.'if ('.implode(' ', self::replaceKeywords($params, $compiler)).") {\n".Dwoo_Compiler::PHP_CLOSE; $params = $compiler->getCompiledParams($params);
return Dwoo_Compiler::PHP_OPEN.'if ('.implode(' ', self::replaceKeywords($params['*'], $compiler)).") {\n".Dwoo_Compiler::PHP_CLOSE;
} }
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend='', $append='') public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend='', $append='')
......
<?php
/**
* Outputs a html &lt;a&gt; tag
* <pre>
* * href : the target URI where the link must point
* * text : text to display, if not provided the href parameter will be used
* * rest : any other attributes you want to add to the tag can be added as named parameters
* </pre>
*
* Example :
*
* <code>
* {* Create a simple link out of an url variable and add a special class attribute: *}
*
* {$url|a:class="external"}
*
* {* Mark a link as active depending on some other variable : *}
*
* {a $link.url $link.title class=tif($link.active "active")} {* This is similar to: <a href="{$link.url}" class="{if $link.active}active{/if}">{$link.title}</a> *}
* </code>
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
*
* This file is released under the LGPL
* "GNU Lesser General Public License"
* More information can be found here:
* {@link http://www.gnu.org/copyleft/lesser.html}
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @link http://dwoo.org/
* @version 0.9.1
* @date 2008-05-30
* @package Dwoo
*/
function Dwoo_Plugin_a_compile(Dwoo_Compiler $compiler, $href, $text=null, array $rest=array())
{
if ($text=='null') {
$text = $href;
}
$out = '\'<a href="\'.'.$href.'.\'"';
foreach ($rest as $attr=>$val) {
if (trim($val, '"\'')=='' || $val=='null') {
continue;
}
$out .= ' '.$attr.'="\'.'.$val.'.\'"';
}
return $out . '>\'.'.$text.'.\'</a>\'';
}
<?php
/**
* Ternary if operation
*
* It evaluates the first argument and returns the second if it's true, or the third if it's false
* <pre>
* * rest : you can not use named parameters to call this, use it either with three arguments in the correct order (expression, true result, false result) or write it as in php (expression ? true result : false result)
* </pre>
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
*
* This file is released under the LGPL
* "GNU Lesser General Public License"
* More information can be found here:
* {@link http://www.gnu.org/copyleft/lesser.html}
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @link http://dwoo.org/
* @version 0.9.1
* @date 2008-05-30
* @package Dwoo
*/
function Dwoo_Plugin_tif_compile(Dwoo_Compiler $compiler, array $rest)
{
// load if plugin
if (!class_exists('Dwoo_Plugin_if', false)) {
try {
Dwoo_Loader::loadPlugin('if');
} catch (Exception $e) {
throw new Dwoo_Compilation_Exception('Tif: the if plugin is required to use Tif');
}
}
// fetch false result and remove the ":" if it was present
$falseResult = array_pop($rest);
if (trim(end($rest), '"\'') === ':') {
// remove the ':' if present
array_pop($rest);
} elseif (trim(end($rest), '"\'') === '?' || count($rest) === 1) {
// there was in fact no false result provided, so we move it to be the true result instead
$trueResult = $falseResult;
$falseResult = "''";
}
// fetch true result if needed
if (!isset($trueResult)) {
$trueResult = array_pop($rest);
// no true result provided so we use the expression arg
if ($trueResult === '?') {
$trueResult = true;
}
}
// remove the '?' if present
if (trim(end($rest), '"\'') === '?') {
array_pop($rest);
}
// check params were correctly provided
if (empty($rest) || empty($trueResult) || empty($falseResult)) {
throw new Dwoo_Compilation_Exception('Tif: you must provide three parameters serving as <expression> ? <true value> : <false value>');
}
// parse condition
$condition = Dwoo_Plugin_if::replaceKeywords($rest, $compiler);
return '(('.implode(' ', $condition).') ? '.($trueResult===true ? implode(' ', $condition) : $trueResult).' : '.$falseResult.')';
}
...@@ -136,6 +136,25 @@ class CompilerTests extends PHPUnit_Framework_TestCase ...@@ -136,6 +136,25 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->dwoo->get($tpl, array(), $this->compiler); $this->dwoo->get($tpl, array(), $this->compiler);
} }
public function testMixedParamsMultiline()
{
$tpl = new Dwoo_Template_String('{replace(
$foo search="BAR"|lower
replace="BAR"
)}');
$tpl->forceCompilation();
$this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
$tpl = new Dwoo_Template_String('{replace(
$foo search=$bar|lower
replace="BAR"
)}');
$tpl->forceCompilation();
$this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar','bar'=>'BAR'), $this->compiler));
}
public function testRecursiveCall() public function testRecursiveCall()
{ {
$tpl = new Dwoo_Template_String('{lower(reverse(upper($foo)))}'); $tpl = new Dwoo_Template_String('{lower(reverse(upper($foo)))}');
...@@ -462,13 +481,13 @@ class CompilerTests extends PHPUnit_Framework_TestCase ...@@ -462,13 +481,13 @@ class CompilerTests extends PHPUnit_Framework_TestCase
$this->dwoo->get($tpl, array('foo'=>0), $this->compiler); $this->dwoo->get($tpl, array('foo'=>0), $this->compiler);
} }
public function testAutoEscape() public function testAutoEscape()
{ {
$cmp = new Dwoo_Compiler(); $cmp = new Dwoo_Compiler();
$cmp->setAutoEscape(true); $cmp->setAutoEscape(true);
$this->assertEquals(true, $cmp->getAutoEscape()); $this->assertEquals(true, $cmp->getAutoEscape());
$tpl = new Dwoo_Template_String('{$foo}{$foo|safe}'); $tpl = new Dwoo_Template_String('{$foo}{$foo|safe}');
$tpl->forceCompilation(); $tpl->forceCompilation();
......
...@@ -14,6 +14,19 @@ class FuncTests extends PHPUnit_Framework_TestCase ...@@ -14,6 +14,19 @@ class FuncTests extends PHPUnit_Framework_TestCase
$this->dwoo = new Dwoo(); $this->dwoo = new Dwoo();
} }
public function testA()
{
$tpl = new Dwoo_Template_String('{a "http://foo/" "Foo!" test="foo" bar="bar"}');
$tpl->forceCompilation();
$this->assertEquals('<a href="http://foo/" test="foo" bar="bar">Foo!</a>', $this->dwoo->get($tpl, array(), $this->compiler));
$tpl = new Dwoo_Template_String('{a $url test="foo" bar="bar"}');
$tpl->forceCompilation();
$this->assertEquals('<a href="http://foo/" test="foo" bar="bar">http://foo/</a>', $this->dwoo->get($tpl, array('url'=>'http://foo/'), $this->compiler));
}
public function testAssign() public function testAssign()
{ {
// test simple assign // test simple assign
...@@ -404,6 +417,49 @@ a"}'); ...@@ -404,6 +417,49 @@ a"}');
$this->assertEquals("a b c d", $this->dwoo->get($tpl, array(), $this->compiler)); $this->assertEquals("a b c d", $this->dwoo->get($tpl, array(), $this->compiler));
} }
public function testTif()
{
$tpl = new Dwoo_Template_String('{tif $a && $b && 3==3 ? foo : bar}');
$tpl->forceCompilation();
$this->assertEquals("foo", $this->dwoo->get($tpl, array('a'=>true, 'b'=>3), $this->compiler));
$tpl = new Dwoo_Template_String('{tif $a && $b && 3==3 foo bar}');
$tpl->forceCompilation();
$this->assertEquals("bar", $this->dwoo->get($tpl, array('a'=>true, 'b'=>0), $this->compiler));
$tpl = new Dwoo_Template_String('{tif $a foo bar}');
$tpl->forceCompilation();
$this->assertEquals("foo", $this->dwoo->get($tpl, array('a'=>true, 'b'=>0), $this->compiler));
$tpl = new Dwoo_Template_String('{tif $a?"foo":"bar"}');
$tpl->forceCompilation();
$this->assertEquals("foo", $this->dwoo->get($tpl, array('a'=>true, 'b'=>0), $this->compiler));
$tpl = new Dwoo_Template_String('{tif $a?foo:bar}');
$tpl->forceCompilation();
$this->assertEquals("foo", $this->dwoo->get($tpl, array('a'=>true, 'b'=>0), $this->compiler));
$tpl = new Dwoo_Template_String('{tif $a ? foo}');
$tpl->forceCompilation();
$this->assertEquals("foo", $this->dwoo->get($tpl, array('a'=>true, 'b'=>0), $this->compiler));
$tpl = new Dwoo_Template_String('{tif $a foo}');
$tpl->forceCompilation();
$this->assertEquals("foo", $this->dwoo->get($tpl, array('a'=>true, 'b'=>0), $this->compiler));
$tpl = new Dwoo_Template_String('{tif $a ?: foo}');
$tpl->forceCompilation();
$this->assertEquals("1", $this->dwoo->get($tpl, array('a'=>true, 'b'=>0), $this->compiler));
}
public function testTruncate() public function testTruncate()
{ {
$tpl = new Dwoo_Template_String('{truncate "abcdefghijklmnopqrstuvwxyz" 20 "..." true}'); $tpl = new Dwoo_Template_String('{truncate "abcdefghijklmnopqrstuvwxyz" 20 "..." true}');
......
...@@ -28,5 +28,14 @@ class HelperTests extends PHPUnit_Framework_TestCase ...@@ -28,5 +28,14 @@ class HelperTests extends PHPUnit_Framework_TestCase
$tpl->forceCompilation(); $tpl->forceCompilation();
$this->assertEquals('true', $this->dwoo->get($tpl, array('test'=>array("hoy"=>3, 5=>"foo", "bar"=>"moo"), 'baz'=>'baz'), $this->compiler)); $this->assertEquals('true', $this->dwoo->get($tpl, array('test'=>array("hoy"=>3, 5=>"foo", "bar"=>"moo"), 'baz'=>'baz'), $this->compiler));
$tpl = new Dwoo_Template_String('{if array(hoy=3,5=array(
"foo"
frack
18
) bar=moo) === $test}true{/if}');
$tpl->forceCompilation();
$this->assertEquals('true', $this->dwoo->get($tpl, array('test'=>array("hoy"=>3, 5=>array("foo", "frack", 18), "bar"=>"moo"), 'baz'=>'baz'), $this->compiler));
} }
} }
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