[limb-svn] r6285 - in 3.x/trunk/limb/macro: src src/tags tests/cases/tags
svn at limb-project.com
svn at limb-project.com
Mon Sep 10 15:30:01 MSD 2007
Author: pachanga
Date: 2007-09-10 15:30:01 +0400 (Mon, 10 Sep 2007)
New Revision: 6285
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6285
Modified:
3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php
3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php
3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php
3.x/trunk/limb/macro/src/tags/include.tag.php
3.x/trunk/limb/macro/tests/cases/tags/lmbMacroIncludeTagTest.class.php
Log:
-- initial support for dynamic <%include%> tag added
Modified: 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php 2007-09-10 10:44:20 UTC (rev 6284)
+++ 3.x/trunk/limb/macro/src/lmbMacroCodeWriter.class.php 2007-09-10 11:30:01 UTC (rev 6285)
@@ -43,7 +43,8 @@
$this->parent = 'lmbMacroTemplateExecutor';
$this->registerInclude('limb/macro/src/lmbMacroTemplateExecutor.class.php');
- $this->beginMethod($render_func);
+ $this->beginMethod($render_func, array('$args = array()'));
+ $this->writePHP('extract($args);');
}
function getClass()
Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php 2007-09-10 10:44:20 UTC (rev 6284)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplate.class.php 2007-09-10 11:30:01 UTC (rev 6285)
@@ -23,21 +23,30 @@
protected $cache_dir;
protected $vars = array();
- function __construct($file, $cache_dir, $locator = null)
+ function __construct($file, $cache_dir = null, $locator = null)
{
$this->file = $file;
+
+ if(!$cache_dir)
+ $cache_dir = LIMB_VAR_DIR . '/compiled';
$this->cache_dir = $cache_dir;
+
+ if(!$locator)
+ $locator = new lmbMacroTemplateLocator();
$this->locator = $locator;
- if(!$this->locator)
- $this->locator = new lmbMacroTemplateLocator();
}
+ function setVars($vars)
+ {
+ $this->vars = $vars;
+ }
+
function set($name, $value)
{
$this->vars[$name] = $value;
}
- function render()
+ function render($vars = array())
{
if(!$source_file = $this->locator->locateSourceTemplate($this->file))
throw new lmbMacroException('Template source file not found', array('file_name' => $this->file));
@@ -50,10 +59,10 @@
$compiler->compile($source_file, $compiled_file, $class, 'render');
include($compiled_file);
- $executor = new $class($this->vars);
+ $executor = new $class($this->vars, $this->cache_dir, $this->locator);
ob_start();
- $executor->render();
+ $executor->render($vars);
$out = ob_get_contents();
ob_end_clean();
return $out;
Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php 2007-09-10 10:44:20 UTC (rev 6284)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php 2007-09-10 11:30:01 UTC (rev 6285)
@@ -15,10 +15,16 @@
*/
class lmbMacroTemplateExecutor
{
- function __construct($vars = array())
+ protected $cache_dir;
+ protected $locator;
+
+ function __construct($vars = array(), $cache_dir = null, $locator = null)
{
foreach($vars as $name => $value)
$this->$name = $value;
+
+ $this->cache_dir = $cache_dir;
+ $this->locator = $locator;
}
function set($name, $value)
@@ -32,6 +38,15 @@
return '';
}
- function render(){}
+ function render($args = array())
+ {
+ extract($args);
+ }
+
+ function includeTemplate($file, $vars = array())
+ {
+ $template = new lmbMacroTemplate($file, $this->cache_dir, $this->locator);
+ echo $template->render($vars);
+ }
}
Modified: 3.x/trunk/limb/macro/src/tags/include.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/include.tag.php 2007-09-10 10:44:20 UTC (rev 6284)
+++ 3.x/trunk/limb/macro/src/tags/include.tag.php 2007-09-10 11:30:01 UTC (rev 6285)
@@ -31,27 +31,55 @@
if(!$file = $this->get('file'))
$this->raiseRequiredAttributeError($file);
- $source_file = $locator->locateSourceTemplate($file);
- if(empty($source_file))
- $this->raise('Template source file not found', array('file_name' => $file));
+ if(!$this->_isDynamic())
+ {
+ $source_file = $locator->locateSourceTemplate($file);
+ if(empty($source_file))
+ $this->raise('Template source file not found', array('file_name' => $file));
- $compiler->parseTemplate($file, $this);
+ $compiler->parseTemplate($file, $this);
+ }
}
+ function _isDynamic()
+ {
+ return strpos($this->get('file'), '$') === 0;
+ }
+
function generateContents($code)
{
+ if($this->_isDynamic())
+ $this->_generateDynamicContents($code);
+ else
+ $this->_generateStaticContents($code);
+ }
+
+ function _generateDynamicContents($code)
+ {
+ $args = $this->_attributesIntoArray();
+
+ $arg_str = 'array(';
+ foreach($args as $key => $value)
+ $arg_str .= "'$key' => $value,";
+ $arg_str .= ')';
+
+ $code->writePHP('$this->includeTemplate(' . $this->get('file') . ',' . $arg_str . ');');
+ }
+
+ function _generateStaticContents($code)
+ {
static $counter = 1;
- list($keys, $vals) = $this->_getLocalVars();
+ list($keys, $vals) = $this->_attributesIntoArgs();
- $method = $code->beginMethod('__include' . ($counter++), $keys);
+ $method = $code->beginMethod('__staticInclude' . ($counter++), $keys);
parent :: generateContents($code);
$code->endMethod();
$code->writePHP('$this->' . $method . '(' . implode(', ', $vals) . ');');
}
- protected function _getLocalVars()
+ protected function _attributesIntoArgs()
{
$keys = array();
$vals = array();
@@ -63,6 +91,14 @@
return array($keys, $vals);
}
+ protected function _attributesIntoArray()
+ {
+ $arr = array();
+ foreach($this->attributes as $k => $v)
+ $arr[$k] = $this->_sanitizeValue($v);
+ return $arr;
+ }
+
protected function _sanitizeValue($v)
{
if(is_numeric($v) || $v{0} == '$')
Modified: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroIncludeTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroIncludeTagTest.class.php 2007-09-10 10:44:20 UTC (rev 6284)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroIncludeTagTest.class.php 2007-09-10 11:30:01 UTC (rev 6285)
@@ -19,7 +19,7 @@
lmbFs :: mkdir(LIMB_VAR_DIR . '/tpl/compiled');
}
- function testSimpleInclude()
+ function testSimpleStaticInclude()
{
$bar = '<body><%include file="foo.html"/%></body>';
$foo = '<p>Hello, Bob</p>';
@@ -33,7 +33,7 @@
$this->assertEqual($out, '<body><p>Hello, Bob</p></body>');
}
- function testNestedInclude()
+ function testNestedStaticInclude()
{
$bar = '<body><%include file="foo.html"/%></body>';
$foo = '<p>Hello, <%include file="name.html"/%></p>';
@@ -49,7 +49,7 @@
$this->assertEqual($out, '<body><p>Hello, Bob</p></body>');
}
- function testIncludePassVariables()
+ function testStaticIncludePassVariables()
{
$bar = '<body><?php $var2=2;?><%include file="foo.html" var1="1" var2="$var2"/%></body>';
$foo = '<p>Numbers: <?php echo $var1;?> <?php echo $var2;?></p>';
@@ -63,7 +63,7 @@
$this->assertEqual($out, '<body><p>Numbers: 1 2</p></body>');
}
- function testIncludeMixLocalAndTemplateVariables()
+ function testStaticIncludeMixLocalAndTemplateVariables()
{
$bar = '<body><?php $var2=2;?><%include file="foo.html" var1="1" var2="$var2"/%></body>';
$foo = '<p>Numbers: <?php echo $var1;?> <?php echo $var2;?> <?php echo $this->var3;?></p>';
@@ -78,6 +78,36 @@
$this->assertEqual($out, '<body><p>Numbers: 1 2 3</p></body>');
}
+ function testDynamicInclude()
+ {
+ $bar = '<body><%include file="$this->file"/%></body>';
+ $foo = '<p>Hello!</p>';
+
+ $bar_tpl = $this->_createTemplate($bar, 'bar.html');
+ $foo_tpl = $this->_createTemplate($foo, 'foo.html');
+
+ $macro = $this->_createMacro($bar_tpl);
+ $macro->set('file', 'foo.html');
+
+ $out = $macro->render();
+ $this->assertEqual($out, '<body><p>Hello!</p></body>');
+ }
+
+ function testDynamicIncludePassLocalVars()
+ {
+ $bar = '<body><?php $name = "Fred";?><%include file="$this->file" name="$name"/%></body>';
+ $foo = '<p>Hello, <?php echo $name;?>!</p>';
+
+ $bar_tpl = $this->_createTemplate($bar, 'bar.html');
+ $foo_tpl = $this->_createTemplate($foo, 'foo.html');
+
+ $macro = $this->_createMacro($bar_tpl);
+ $macro->set('file', 'foo.html');
+
+ $out = $macro->render();
+ $this->assertEqual($out, '<body><p>Hello, Fred!</p></body>');
+ }
+
protected function _createMacro($file)
{
$base_dir = LIMB_VAR_DIR . '/tpl';
More information about the limb-svn
mailing list