[limb-svn] r6338 - in 3.x/trunk/limb/macro: src/tags tests/cases/tags

svn at limb-project.com svn at limb-project.com
Fri Sep 28 01:43:31 MSD 2007


Author: pachanga
Date: 2007-09-28 01:43:31 +0400 (Fri, 28 Sep 2007)
New Revision: 6338
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6338

Modified:
   3.x/trunk/limb/macro/src/tags/output.tag.php
   3.x/trunk/limb/macro/tests/cases/tags/lmbMacroOutputTagTest.class.php
Log:
-- adding basic chained operations support for arrays and objects in output tag


Modified: 3.x/trunk/limb/macro/src/tags/output.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/output.tag.php	2007-09-27 11:16:16 UTC (rev 6337)
+++ 3.x/trunk/limb/macro/src/tags/output.tag.php	2007-09-27 21:43:31 UTC (rev 6338)
@@ -21,7 +21,37 @@
 {
   function generateContents($code)
   {
-    $code->writePHP('echo ' . $this->tag . ';');
+    $code->writePHP($this->_compileExpression($code));
   }
+
+  protected function _compileExpression($code)
+  {
+    if(strpos($this->tag, '.') === false)
+      return 'echo ' . $this->tag . ';';
+
+    $result = '';
+
+    $tmp = $code->getTempVarRef();
+    $code->writePHP($tmp . "='';");
+
+    $items = explode('.', $this->tag);
+    //first item is variable itself
+    $prev = $items[0];
+    for($i=1; $i<sizeof($items); $i++)
+    {
+      $item = $items[$i];
+      $result .= 'if((is_array(' . $prev . ') && isset(' . $prev . '["' . $item . '"])) || ' . 
+                 '(is_object(' . $prev . ') && ' . $tmp . '=' . $prev . '->get("' . $item . '")))' .  
+                 '{ if(is_array(' . $prev . '))' . $tmp . ' = ' . $prev . '["' . $item . '"];';
+      $prev = $tmp;
+    }
+
+    //closing brackets
+    for($i=1; $i<sizeof($items); $i++)
+      $result .= '}';
+
+    $result .= "echo $tmp;";
+    return $result;
+  }
 }
 

Modified: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroOutputTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroOutputTagTest.class.php	2007-09-27 11:16:16 UTC (rev 6337)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroOutputTagTest.class.php	2007-09-27 21:43:31 UTC (rev 6338)
@@ -8,6 +8,7 @@
  */
 
 lmb_require('limb/macro/src/lmbMacroTemplate.class.php');
+lmb_require('limb/core/src/lmbObject.class.php');
 lmb_require('limb/fs/src/lmbFs.class.php');
 lmb_require('limb/macro/src/lmbMacroTagDictionary.class.php');
 
@@ -34,6 +35,45 @@
     $this->assertEqual($out, 'Foo');
   }
 
+  function testSimpleChainedOutputForArray()
+  {
+    $content = '<%$#var.foo.bar%>';
+
+    $tpl = $this->_createTemplate($content, 'tpl.html');
+
+    $macro = $this->_createMacro($tpl);
+    $macro->set('var', array('foo' => array('bar' => 'Hey')));
+
+    $out = $macro->render();
+    $this->assertEqual($out, 'Hey');
+  }
+
+  function testSimpleChainedOutputForObject()
+  {
+    $content = '<%$#var.foo.bar%>';
+
+    $tpl = $this->_createTemplate($content, 'tpl.html');
+
+    $macro = $this->_createMacro($tpl);
+    $macro->set('var', new lmbObject(array('foo' => new lmbObject(array('bar' => 'Hey')))));
+
+    $out = $macro->render();
+    $this->assertEqual($out, 'Hey');
+  }
+
+  function testChainedOutputForMixedArrayAndObjects()
+  {
+    $content = '<%$#var.foo.bar%>';
+
+    $tpl = $this->_createTemplate($content, 'tpl.html');
+
+    $macro = $this->_createMacro($tpl);
+    $macro->set('var', new lmbObject(array('foo' => array('bar' => 'Hey'))));
+
+    $out = $macro->render();
+    $this->assertEqual($out, 'Hey'); 
+  }
+
   protected function _createMacro($file)
   {
     $base_dir = LIMB_VAR_DIR . '/tpl';



More information about the limb-svn mailing list