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

svn at limb-project.com svn at limb-project.com
Thu Sep 20 14:09:07 MSD 2007


Author: pachanga
Date: 2007-09-20 14:09:07 +0400 (Thu, 20 Sep 2007)
New Revision: 6321
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6321

Modified:
   3.x/trunk/limb/macro/src/tags/list.tag.php
   3.x/trunk/limb/macro/tests/cases/tags/lmbMacroListTagTest.class.php
Log:
-- adding better implementation of rendering other nodes inside of <%list%> tag

Modified: 3.x/trunk/limb/macro/src/tags/list.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/list.tag.php	2007-09-20 05:54:27 UTC (rev 6320)
+++ 3.x/trunk/limb/macro/src/tags/list.tag.php	2007-09-20 10:09:07 UTC (rev 6321)
@@ -19,25 +19,63 @@
 {
   function generateContents($code)
   {
-    $counter = $code->getTempVarRef();
     $using = $this->get('using');
     $as = $this->get('as');
 
-    $list_item = $this->findImmediateChildByClass('lmbMacroListItemTag');
-    $list_empty = $this->findImmediateChildByClass('lmbMacroListEmptyTag');
-
+    $counter = $code->getTempVarRef();
     $code->writePHP($counter . ' = 0;');
     $code->writePHP('foreach(' . $using . ' as ' . $as . ') {');
-    $list_item->generateContents($code);
+
+    $found_item_tag = false;
+    $postponed_nodes = array();
+
+    //we need to render all nodes before and after <%list:*%> tags
+    foreach($this->children as $child)
+    {
+      //we want to skip all <%list:*%> tags, since they are rendered manually
+      if(!$this->_isOneOfListTags($child))
+      {
+        //tags before <%list:item%> should be rendered only once when counter is 0
+        if(!$found_item_tag)
+        {
+          $code->writePHP('if(' . $counter . ' == 0) {');
+          $child->generateContents($code);
+          $code->writePHP('}');
+        }
+        //otherwise we collect them to display later 
+        else
+          $postponed_nodes[] = $child;
+      }
+      elseif(is_a($child, 'lmbMacroListItemTag'))
+      {
+        $found_item_tag = true;
+        $child->generateContents($code);
+      }
+    }
+
     $code->writePHP($counter . '++;');
     $code->writePHP('}');
 
-    if($list_empty)
+    //tags after <%list:item%> should be rendered only if there were any items
+    foreach($postponed_nodes as $node)
     {
+      $code->writePHP('if(' . $counter . ' > 0) {');
+      $node->generateContents($code);
+      $code->writePHP('}');
+    }
+
+    if($list_empty = $this->findImmediateChildByClass('lmbMacroListEmptyTag'))
+    {
       $code->writePHP('if(' . $counter . ' == 0) {');
       $list_empty->generateContents($code);
       $code->writePHP('}');
     }
   }
+
+  protected function _isOneOfListTags($node)
+  {
+    return is_a($node, 'lmbMacroListEmptyTag') || 
+           is_a($node, 'lmbMacroListItemTag');
+  }
 }
 

Modified: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroListTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroListTagTest.class.php	2007-09-20 05:54:27 UTC (rev 6320)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroListTagTest.class.php	2007-09-20 10:09:07 UTC (rev 6321)
@@ -50,6 +50,32 @@
     $this->assertEqual($out, 'Nothing');
   }
 
+  function testTextNodesInsideListTag()
+  {
+    $list = '<%list using="$#list" as="$item"%>List: <%list:item%><?=$item?> <%/list:item%> !<%/list%>';
+
+    $list_tpl = $this->_createTemplate($list, 'list.html');
+
+    $macro = $this->_createMacro($list_tpl);
+    $macro->set('list', array('Bob', 'Todd'));
+
+    $out = $macro->render();
+    $this->assertEqual($out, 'List: Bob Todd  !');
+  }
+
+  function testTextNodesInsideListTagWithEmptyListTag()
+  {
+    $list = '<%list using="$#list" as="$item"%>List: <%list:item%><?=$item?> <%/list:item%> !<%list:empty%>Nothing<%/list:empty%><%/list%>';
+
+    $list_tpl = $this->_createTemplate($list, 'list.html');
+
+    $macro = $this->_createMacro($list_tpl);
+    $macro->set('list', array());
+
+    $out = $macro->render();
+    $this->assertEqual($out, 'Nothing');
+  }
+
   protected function _createMacro($file)
   {
     $base_dir = LIMB_VAR_DIR . '/tpl';



More information about the limb-svn mailing list