Commit cb80c435 by seldaek

Added {return} plugin that allows any included template to return variables into…

Added {return} plugin that allows any included template to return variables into the one that included it, or to the main controller code via $dwoo->getReturnValues() Fixes #59 git-svn-id: http://svn.dwoo.org/trunk@346 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 06407917
[2010--] 1.2.0 [2010--] 1.2.0
! BC Break: Dwoo::isArray had to be fixed and it has been split up in 3 ! BC Break: Dwoo_Core::isArray had to be fixed and it has been split up
methods, isArray (for array access), isTraversable (for foreach) and in 3 methods, isArray (for array access), isTraversable (for foreach)
count (just a helper that counts anything). It won't affect you unless and count (just a helper that counts anything). It won't affect you
you built some plugin depending on isArray, in which case you should unless you built some plugin depending on isArray, in which case you
check all works fine still should check all works fine still
! BC Break: Dwoo_Core::get now returns a new Dwoo_Core instance if it's
evaluating a template already, this only affects you if you built your
own include-like plugin, see the changes to include plugin in revision
346 [http://bugs.dwoo.org/dwoo/browse_code/revision/345] for more infos
+ Added {return} plugin that allows any included template to return
variables into the one that included it, or to the main controller
code via $dwoo->getReturnValues()
+ Moved Dwoo code to Dwoo_Core that is extended by Dwoo, so you can use + Moved Dwoo code to Dwoo_Core that is extended by Dwoo, so you can use
the Dwoo directory as an svn:externals without problems now and just the Dwoo directory as an svn:externals without problems now and just
use Dwoo_Core in place of Dwoo in your code use Dwoo_Core in place of Dwoo in your code
......
...@@ -195,6 +195,13 @@ class Dwoo_Core ...@@ -195,6 +195,13 @@ class Dwoo_Core
protected $runtimePlugins; protected $runtimePlugins;
/** /**
* stores the returned values during template runtime
*
* @var array
*/
protected $returnData;
/**
* stores the data during template runtime * stores the data during template runtime
* *
* @var array * @var array
...@@ -271,6 +278,7 @@ class Dwoo_Core ...@@ -271,6 +278,7 @@ class Dwoo_Core
{ {
$this->template = null; $this->template = null;
unset($this->data); unset($this->data);
unset($this->returnData);
} }
/** /**
...@@ -308,8 +316,7 @@ class Dwoo_Core ...@@ -308,8 +316,7 @@ class Dwoo_Core
{ {
// a render call came from within a template, so we need a new dwoo instance in order to avoid breaking this one // a render call came from within a template, so we need a new dwoo instance in order to avoid breaking this one
if ($this->template instanceof Dwoo_ITemplate) { if ($this->template instanceof Dwoo_ITemplate) {
$proxy = clone $this; return clone $this;
return $proxy->get($_tpl, $data, $_compiler, $_output);
} }
// auto-create template if required // auto-create template if required
...@@ -448,6 +455,7 @@ class Dwoo_Core ...@@ -448,6 +455,7 @@ class Dwoo_Core
$this->stack = array(); $this->stack = array();
$this->curBlock = null; $this->curBlock = null;
$this->buffer = ''; $this->buffer = '';
$this->returnData = array();
} }
/* /*
...@@ -1541,6 +1549,27 @@ class Dwoo_Core ...@@ -1541,6 +1549,27 @@ class Dwoo_Core
} }
/** /**
* [runtime function] sets a return value for the currently running template
*
* @param string $name var name
* @param mixed $value var value
*/
public function setReturnValue($name, $value)
{
$this->returnData[$name] = $value;
}
/**
* [runtime function] retrieves the return values set by the template
*
* @return array
*/
public function getReturnValues()
{
return $this->returnData;
}
/**
* [runtime function] returns a reference to the current scope * [runtime function] returns a reference to the current scope
* *
* @return &mixed * @return &mixed
......
...@@ -31,7 +31,8 @@ function Dwoo_Plugin_eval(Dwoo_Core $dwoo, $var, $assign = null) ...@@ -31,7 +31,8 @@ function Dwoo_Plugin_eval(Dwoo_Core $dwoo, $var, $assign = null)
} }
$tpl = new Dwoo_Template_String($var); $tpl = new Dwoo_Template_String($var);
$out = $dwoo->get($tpl, $dwoo->readVar('_parent')); $clone = $dwoo->get(null);
$out = $clone->get($tpl, $dwoo->readVar('_parent'));
if ($assign !== null) { if ($assign !== null) {
$dwoo->assignInScope($out, $assign); $dwoo->assignInScope($out, $assign);
......
...@@ -62,11 +62,18 @@ function Dwoo_Plugin_include(Dwoo_Core $dwoo, $file, $cache_time = null, $cache_ ...@@ -62,11 +62,18 @@ function Dwoo_Plugin_include(Dwoo_Core $dwoo, $file, $cache_time = null, $cache_
$vars = $rest + $vars; $vars = $rest + $vars;
} }
$out = $dwoo->get($include, $vars); $clone = $dwoo->get(null);
$out = $clone->get($include, $vars);
if ($assign !== null) { if ($assign !== null) {
$dwoo->assignInScope($out, $assign); $dwoo->assignInScope($out, $assign);
} else { }
foreach ($clone->getReturnValues() as $name => $value) {
$dwoo->assignInScope($value, $name);
}
if ($assign === null) {
return $out; return $out;
} }
} }
<?php
/**
* Inserts another template into the current one
* <pre>
* * file : the resource name of the template
* * cache_time : cache length in seconds
* * cache_id : cache identifier for the included template
* * compile_id : compilation identifier for the included template
* * data : data to feed into the included template, it can be any array and will default to $_root (the current data)
* * assign : if set, the output of the included template will be saved in this variable instead of being output
* * rest : any additional parameter/value provided will be added to the data array
* </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.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://dwoo.org/LICENSE Modified BSD License
* @link http://dwoo.org/
* @version 1.1.0
* @date 2009-07-18
* @package Dwoo
*/
function Dwoo_Plugin_return_compile(Dwoo_Compiler $compiler, array $rest = array())
{
$out = array();
foreach ($rest as $var => $val) {
$out[] = '$this->setReturnValue('.var_export($var, true).', '.$val.')';
}
return '('.implode('.', $out).')';
}
\ No newline at end of file
...@@ -429,6 +429,23 @@ a"}'); ...@@ -429,6 +429,23 @@ a"}');
$this->assertEquals("FlMl", $this->dwoo->get($tpl, array(), $this->compiler)); $this->assertEquals("FlMl", $this->dwoo->get($tpl, array(), $this->compiler));
} }
public function testReturn()
{
$tpl = new Dwoo_Template_String('{return bar=$foo}');
$tpl->forceCompilation();
$this->dwoo->get($tpl, array('foo' => 'FOO'), $this->compiler);
$this->assertEquals(array('bar'=>'FOO'), $this->dwoo->getReturnValues());
}
public function testReturnViaInclude()
{
$tpl = new Dwoo_Template_String('{include cat("file:" %TEST_DIRECTORY "/resources/returnTest.html") val="foo"}{$bar}');
$tpl->forceCompilation();
$this->assertEquals('FOO', $this->dwoo->get($tpl, array(), $this->compiler));
}
public function testReverse() public function testReverse()
{ {
$tpl = new Dwoo_Template_String('{reverse "abc"}'); $tpl = new Dwoo_Template_String('{reverse "abc"}');
......
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