Commit 6dd36bca by seldaek

+ SmartyCompat: Added a {section} plugin but I strongly discourage using it, it…

+ SmartyCompat: Added a {section} plugin but I strongly discourage using it, it was really made to support legacy templates git-svn-id: svn://dwoo.org/dwoo/trunk@204 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 0ec75b61
......@@ -5,6 +5,8 @@
IPluginProxy interface, that's it
+ Compiler: the modifier syntax (|foo) can now be applied on functions and on
complex variables i.e. {$obj->getStuff()|upper} or {lower('foo')|upper}
+ SmartyCompat: Added a {section} plugin but I strongly discourage using it,
it was really made to support legacy templates
* The core Dwoo class doesn't need writable compile/cache dirs in the
constructor anymore so you can provide custom ones later through
->setCompile(/Cache)Dir - thanks to Denis Arh for the patch
......@@ -16,7 +18,7 @@
and optionally public methods (if the new show_methods arg is set to true)
- thanks to Stephan Wentz for the patch
* Adapters: Zend: Added parameters to provide a custom engine (extends Dwoo)
or a custom data class (extends Dwoo_Data) - thanks to Maxime Mérian for
or a custom data class (extends Dwoo_Data) - thanks to Maxime Mrian for
the patch
* Compiler: added Dwoo_Compiler->setNestedCommentsHandling(true) to enable
parsing of nested comments (i.e. {* {* *} *} becomes a valid comment, useful
......
......@@ -86,7 +86,7 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
$out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];';
}
// checks if foreach must be looped
$out .= "\n".'if ($this->isArray($_for'.$cnt.'_from, true) || (is_numeric($_for'.$cnt.'_from) && abs(($_for'.$cnt.'_from - $_for'.$cnt.'_to)/$_for'.$cnt.'_step) !== 0))'."\n{";
$out .= "\n".'if ($this->isArray($_for'.$cnt.'_from, true) || (is_numeric($_for'.$cnt.'_from) && (abs(($_for'.$cnt.'_from - $_for'.$cnt.'_to)/$_for'.$cnt.'_step) !== 0 || $_for'.$cnt.'_from == $_for'.$cnt.'_to)))'."\n{";
// iterates over keys
$out .= "\n\t".'if ($this->isArray($_for'.$cnt.'_from, true)) {
$_for'.$cnt.'_from = 0;
......
<?php
/**
* Compatibility plugin for smarty templates, do not use otherwise, this is deprecated.
*
* 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
*/
class Dwoo_Plugin_section extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block, Dwoo_IElseable
{
public static $cnt=0;
public function init($name, $loop, $start = null, $step = null, $max = null, $show = true)
{
}
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
{
return '';
}
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
{
$output = Dwoo_Compiler::PHP_OPEN;
$params = $compiler->getCompiledParams($params);
// assigns params
$loop = $params['loop'];
$start = $params['start'];
$max = $params['max'];
$name = $params['name'];
$step = $params['step'];
$show = $params['show'];
// gets unique id
$cnt = self::$cnt++;
$output .= '$this->globals[\'section\']['.$name.'] = array();'."\n".
'$_section'.$cnt.' =& $this->globals[\'section\']['.$name.'];'."\n";
if ($loop !== 'null') {
$output .= '$_section'.$cnt.'[\'loop\'] = is_array($tmp = '.$loop.') ? count($tmp) : max(0, (int) $tmp);'."\n";
} else {
$output .= '$_section'.$cnt.'[\'loop\'] = 1;'."\n";
}
if ($show !== 'null') {
$output .= '$_section'.$cnt.'[\'show\'] = '.$show.";\n";
} else {
$output .= '$_section'.$cnt.'[\'show\'] = true;'."\n";
}
if ($name !== 'null') {
$output .= '$_section'.$cnt.'[\'name\'] = '.$name.";\n";
} else {
$output .= '$_section'.$cnt.'[\'name\'] = true;'."\n";
}
if ($max !== 'null') {
$output .= '$_section'.$cnt.'[\'max\'] = (int)'.$max.";\n".
'if($_section'.$cnt.'[\'max\'] < 0) { $_section'.$cnt.'[\'max\'] = $_section'.$cnt.'[\'loop\']; }'."\n";
} else {
$output .= '$_section'.$cnt.'[\'max\'] = $_section'.$cnt.'[\'loop\'];'."\n";
}
if ($step !== 'null') {
$output .= '$_section'.$cnt.'[\'step\'] = (int)'.$step.' == 0 ? 1 : (int) '.$step.";\n";
} else {
$output .= '$_section'.$cnt.'[\'step\'] = 1;'."\n";
}
if ($start !== 'null') {
$output .= '$_section'.$cnt.'[\'start\'] = (int)'.$start.";\n";
} else {
$output .= '$_section'.$cnt.'[\'start\'] = $_section'.$cnt.'[\'step\'] > 0 ? 0 : $_section'.$cnt.'[\'loop\'] - 1;'."\n".
'if ($_section'.$cnt.'[\'start\'] < 0) { $_section'.$cnt.'[\'start\'] = max($_section'.$cnt.'[\'step\'] > 0 ? 0 : -1, $_section'.$cnt.'[\'loop\'] + $_section'.$cnt.'[\'start\']); } '."\n".
'else { $_section'.$cnt.'[\'start\'] = min($_section'.$cnt.'[\'start\'], $_section'.$cnt.'[\'step\'] > 0 ? $_section'.$cnt.'[\'loop\'] : $_section'.$cnt.'[\'loop\'] -1); }'."\n";
}
/* if ($usesAny) {
$output .= "\n".'$this->globals["section"]['.$name.'] = array'."\n(";
if ($usesIndex) $output .="\n\t".'"index" => 0,';
if ($usesIteration) $output .="\n\t".'"iteration" => 1,';
if ($usesFirst) $output .="\n\t".'"first" => null,';
if ($usesLast) $output .="\n\t".'"last" => null,';
if ($usesShow) $output .="\n\t".'"show" => ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),';
if ($usesTotal) $output .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
$out.="\n);\n".'$_section'.$cnt.'[\'glob\'] =& $this->globals["section"]['.$name.'];'."\n\n";
}
*/
$output .= 'if ($_section'.$cnt.'[\'show\']) {'."\n";
if ($start === 'null' && $step === 'null' && $max === 'null') {
$output .= ' $_section'.$cnt.'[\'total\'] = $_section'.$cnt.'[\'loop\'];'."\n";
} else {
$output .= ' $_section'.$cnt.'[\'total\'] = min(ceil(($_section'.$cnt.'[\'step\'] > 0 ? $_section'.$cnt.'[\'loop\'] - $_section'.$cnt.'[\'start\'] : $_section'.$cnt.'[\'start\'] + 1) / abs($_section'.$cnt.'[\'step\'])), $_section'.$cnt.'[\'max\']);'."\n";
}
$output .= ' if ($_section'.$cnt.'[\'total\'] == 0) {'."\n".
' $_section'.$cnt.'[\'show\'] = false;'."\n".
' }'."\n".
'} else {'."\n".
' $_section'.$cnt.'[\'total\'] = 0;'."\n}\n";
$output .= 'if ($_section'.$cnt.'[\'show\']) {'."\n";
$output .= "\t".'for ($this->scope['.$name.'] = $_section'.$cnt.'[\'start\'], $_section'.$cnt.'[\'iteration\'] = 1; '.
'$_section'.$cnt.'[\'iteration\'] <= $_section'.$cnt.'[\'total\']; '.
'$this->scope['.$name.'] += $_section'.$cnt.'[\'step\'], $_section'.$cnt.'[\'iteration\']++) {'."\n";
$output .= "\t\t".'$_section'.$cnt.'[\'rownum\'] = $_section'.$cnt.'[\'iteration\'];'."\n";
$output .= "\t\t".'$_section'.$cnt.'[\'index_prev\'] = $this->scope['.$name.'] - $_section'.$cnt.'[\'step\'];'."\n";
$output .= "\t\t".'$_section'.$cnt.'[\'index_next\'] = $this->scope['.$name.'] + $_section'.$cnt.'[\'step\'];'."\n";
$output .= "\t\t".'$_section'.$cnt.'[\'first\'] = ($_section'.$cnt.'[\'iteration\'] == 1);'."\n";
$output .= "\t\t".'$_section'.$cnt.'[\'last\'] = ($_section'.$cnt.'[\'iteration\'] == $_section'.$cnt.'[\'total\']);'."\n";
$output .= Dwoo_Compiler::PHP_CLOSE . $content . Dwoo_Compiler::PHP_OPEN;
$output .= "\n\t}\n} " . Dwoo_Compiler::PHP_CLOSE;
if (isset($params['hasElse'])) {
$output .= $params['hasElse'];
}
return $output;
}
}
......@@ -71,71 +71,6 @@ class Dwoo_Processor_smarty_compat extends Dwoo_Processor
$params[$matches[$index]] = $matches[$index+1];
$index += 2;
}
$params['content'] = $matches[13];
if (isset($matches[14]) && !empty($matches[14])) {
$params['altcontent'] = $matches[14];
}
if (empty($params['name'])) {
throw new Dwoo_Compilation_Exception($this->compiler, 'Missing parameter <em>name</em> for section tag');
}
$name = $params['name'];
if (isset($params['loop'])) {
$loops = $params['loop'];
}
if (isset($params['max'])) {
$max = $params['max'];
}
if (isset($params['start'])) {
$start = $params['start'];
}
if (isset($params['step'])) {
$step = $params['step'];
}
if (!isset($loops))
$loops = null;
if (!isset($max) || $max < 0) {
if (is_numeric($loops)) {
$max = $loops;
} else {
$max = 'null';
}
}
if (!isset($step))
$step = 1;
if (!isset($start))
$start = $loops - 1;
elseif (!is_numeric($loops)) {
$start = 0;
}
list($l, $r) = $this->compiler->getDelimiters();
if (is_numeric($loops)) {
if (isset($params['start']) && isset($params['loop']) && !isset($params['max'])) {
$output = $l.'for '.$name.' '.$start.' '.($loops-$step).' '.$step.$r;
} else {
$output = $l.'for '.$name.' '.$start.' '.($start+floor($step*$max+($step>0?-1:1))).' '.$step.$r;
}
} else {
$output = $l.'for '.$name.' '.$loops.' '.($start+floor($max/$step)).' '.$step.' '.$start.$r;
}
$output .= str_replace('['.trim($name, '"\'').']', '[$'.trim($name, '"\'').']', $params['content']);
if (isset($params['altcontent'])) {
$output .= $l.'forelse'.$r.$params['altcontent'];
}
$output .= $l.'/for'.$r;
return $output;
return str_replace('['.trim($params['name'], '"\'').']', '[$'.trim($params['name'], '"\'').']', $matches[0]);
}
}
......@@ -264,21 +264,34 @@ baz"));
$this->assertEquals('true', $this->dwoo->get($tpl, array('moo'=>'i'), $this->compiler));
}
public function testFor ()
public function testFor()
{
$tpl = new Dwoo_Template_String('{for name=i from=$sub}{$i}.{$sub[$i]}{/for}');
$tpl->forceCompilation();
$this->assertEquals('0.foo1.bar2.baz3.qux', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler));
$tpl = new Dwoo_Template_String('{for name=i from=$sub to=2}{$i}.{$sub[$i]}{/for}');
$tpl->forceCompilation();
$this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler));
// fixes the init call not being called (which is normal)
$fixCall = new Dwoo_Plugin_for ($this->dwoo);
$fixCall->init(null,null);
}
public function testForVariations()
{
$tpl = new Dwoo_Template_String('{for i 1 1}-{$i}{/for}|{for i 1 2}-{$i}{/for}|{for i 1 3}-{$i}{/for}');
$tpl->forceCompilation();
$this->assertEquals('-1|-1-2|-1-2-3', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler));
}
public function testForElse()
{
$tpl = new Dwoo_Template_String('{for name=i from=0 to=0}{$i}{else}Narp!{/for}');
$tpl = new Dwoo_Template_String('{for name=i from=array()}{$i}{else}Narp!{/for}');
$tpl->forceCompilation();
$this->assertEquals('Narp!', $this->dwoo->get($tpl, array(), $this->compiler));
......@@ -286,7 +299,7 @@ baz"));
$tpl = new Dwoo_Template_String('{for name=i from=0 to=0}{$i}{forelse}Narp!{/for}');
$tpl->forceCompilation();
$this->assertEquals('Narp!', $this->dwoo->get($tpl, array(), $this->compiler));
$this->assertEquals('0', $this->dwoo->get($tpl, array(), $this->compiler));
$tpl = new Dwoo_Template_String('{for name=i from=0 to=10 step=3}{$i}{else}Narp!{/for}');
$tpl->forceCompilation();
......
......@@ -21,6 +21,7 @@ class SmartyTests extends PHPUnit_Framework_TestCase
public function testSmartyCompat()
{
$this->assertEquals('{'.Dwoo::VERSION.'}', $this->dwoo->fetch('smartytest.html'));
$this->dwoo->assign('arr', array('ab','cd','ef'));
$this->assertEquals('{'.Dwoo::VERSION.'} ab cd ef', $this->dwoo->fetch('smartytest.html'));
}
}
{ldelim}{$smarty.version}{rdelim}
\ No newline at end of file
{ldelim}{$smarty.version}{rdelim}{section name=i loop=$arr}
{$arr[i]}{/section}
\ No newline at end of file
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