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

svn at limb-project.com svn at limb-project.com
Tue Mar 25 14:38:58 MSK 2008


Author: korchasa
Date: 2008-03-25 14:38:58 +0300 (Tue, 25 Mar 2008)
New Revision: 6855
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6855

Added:
   3.x/trunk/limb/macro/src/tags/tree/tree_empty.tag.php
Modified:
   3.x/trunk/limb/macro/src/tags/tree/tree.tag.php
   3.x/trunk/limb/macro/tests/cases/tags/tree/lmbMacroTreeTagTest.class.php
Log:
-- added {{tree:empty}} macro tag 

Modified: 3.x/trunk/limb/macro/src/tags/tree/tree.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/tree/tree.tag.php	2008-03-23 17:07:36 UTC (rev 6854)
+++ 3.x/trunk/limb/macro/src/tags/tree/tree.tag.php	2008-03-25 11:38:58 UTC (rev 6855)
@@ -33,19 +33,19 @@
 
     $items = $code->generateVar();
     $counter = $code->generateVar();
-    $extra_params = $code->generateVar(); 
+    $extra_params = $code->generateVar();
 
     $this->method = $code->beginMethod('_render_tree'. uniqid(), array($items, $level, $extra_params . '= array()'));
-    
-    $code->writePHP("if($extra_params) extract($extra_params);"); 
 
+    $code->writePHP("if($extra_params) extract($extra_params);");
+
     $code->writePHP($counter . "=0;\n");
 
     $code->writePHP('foreach(' . $items . ' as ' . $as . ") {\n");
 
     if($user_counter = $this->get('counter'))
       $code->writePHP($user_counter . ' = ' . $counter . "+1;\n");
-     
+
     //rendering tags before branch
     $code->writePHP('if(!' . $counter . ") {\n");
     foreach($before_node as $tag)
@@ -57,18 +57,21 @@
     $code->writePHP($counter . "++;\n");
     $code->writePHP("}\n");//foreach
 
+    $code = $this->_renderEmptyTag($code, $items);
+
     //rendering tags after branch
     $code->writePHP('if(' . $counter . ") {\n");
     foreach($after_node as $tag)
-      $tag->generate($code);
+      if(!$tag instanceof lmbMacroTreeEmptyTag)
+        $tag->generate($code);
     $code->writePHP("}\n");
-
     $code->endMethod();
 
     $arg_str = $this->extraAttributesIntoArrayString();
     $code->writePHP('$this->' . $this->method . '(' . $tree . ', 0' . ',' . $arg_str . ");\n");
+
   }
-  
+
   function getRecursionMethod()
   {
     return $this->method;
@@ -99,10 +102,21 @@
     }
     return $tags;
   }
-  
+
   function extraAttributesIntoArrayString()
   {
     return $this->attributesIntoArrayString($skip = array('as', 'using', 'counter', 'level'));
   }
+
+  protected function _renderEmptyTag($code, $items)
+  {
+    if($list_empty = $this->findImmediateChildByClass('lmbMacroTreeEmptyTag'))
+    {
+      $code->writePHP('if(count(' . $items . ') == 0) {');
+        $list_empty->generate($code);
+      $code->writePHP('}');
+    }
+
+    return $code;
+  }
 }
-

Added: 3.x/trunk/limb/macro/src/tags/tree/tree_empty.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/tree/tree_empty.tag.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/tree/tree_empty.tag.php	2008-03-25 11:38:58 UTC (rev 6855)
@@ -0,0 +1,17 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * @tag tree:empty
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroTreeEmptyTag extends lmbMacroTag
+{
+}

Modified: 3.x/trunk/limb/macro/tests/cases/tags/tree/lmbMacroTreeTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/tree/lmbMacroTreeTagTest.class.php	2008-03-23 17:07:36 UTC (rev 6854)
+++ 3.x/trunk/limb/macro/tests/cases/tags/tree/lmbMacroTreeTagTest.class.php	2008-03-25 11:38:58 UTC (rev 6855)
@@ -11,93 +11,113 @@
 {
   function testRenderTree()
   {
-    $content = '{{tree using="$#tree" as="$item" kids_prop="kids"}}' . 
-                  '<ul>' . 
-                  '{{tree:node}}' . 
-                    '<li>{$item.title}{{tree:nextlevel/}}</li>' . 
+    $content = '{{tree using="$#tree" as="$item" kids_prop="kids"}}' .
+                  '<ul>' .
+                  '{{tree:node}}' .
+                    '<li>{$item.title}{{tree:nextlevel/}}</li>' .
                   '{{/tree:node}}' .
-                  '</ul>' . 
+                  '</ul>' .
                 '{{/tree}}';
 
     $tpl = $this->_createTemplate($content, 'tree.html');
 
     $macro = $this->_createMacro($tpl);
-    $macro->set('tree', array(array('title' => 'foo'), 
+    $macro->set('tree', array(array('title' => 'foo'),
                               array('title' => 'bar', 'kids' => array(array('title' => 'bar1'),
-                                                                      array('title' => 'bar2'))), 
+                                                                      array('title' => 'bar2'))),
                               array('title' => 'hey')));
 
     $out = $macro->render();
     $this->assertEqual($out, '<ul><li>foo</li><li>bar<ul><li>bar1</li><li>bar2</li></ul></li><li>hey</li></ul>');
   }
-  
+
   function testCounter()
   {
-    $content = '{{tree using="$#tree" as="$item" kids_prop="kids" counter="$counter" prefix="1"}}' . 
-                  '<ul>' . 
-                  '{{tree:node}}' . 
+    $content = '{{tree using="$#tree" as="$item" kids_prop="kids" counter="$counter" prefix="1"}}' .
+                  '<ul>' .
+                  '{{tree:node}}' .
                   '<li>{$counter}){$item.title}'.
-                  '{{tree:nextlevel/}}</li>' . 
+                  '{{tree:nextlevel/}}</li>' .
                   '{{/tree:node}}' .
-                  '</ul>' . 
+                  '</ul>' .
                 '{{/tree}}';
 
     $tpl = $this->_createTemplate($content, 'tree.html');
 
     $macro = $this->_createMacro($tpl);
-    $macro->set('tree', array(array('title' => 'foo'), 
+    $macro->set('tree', array(array('title' => 'foo'),
                               array('title' => 'bar', 'kids' => array(array('title' => 'bar1'),
-                                                                      array('title' => 'bar2'))), 
+                                                                      array('title' => 'bar2'))),
                               array('title' => 'hey')));
 
     $out = $macro->render();
     $this->assertEqual($out, '<ul><li>1)foo</li><li>2)bar<ul><li>1)bar1</li><li>2)bar2</li></ul></li><li>3)hey</li></ul>');
   }
-  
+
   function testPassExtraParamsIntoTreeMethod()
   {
-    $content = '{{tree using="$#tree" as="$item" kids_prop="kids" counter="$counter" prefix="1"}}' . 
-                  '<ul>' . 
-                  '{{tree:node}}' . 
+    $content = '{{tree using="$#tree" as="$item" kids_prop="kids" counter="$counter" prefix="1"}}' .
+                  '<ul>' .
+                  '{{tree:node}}' .
                   '<li>{$prefix}.{$counter}){$item.title}'.
-                  '{{tree:nextlevel prefix="$new_prefix"}}<?php $new_prefix = $prefix . "." . $counter; ?>{{/tree:nextlevel}}</li>' . 
+                  '{{tree:nextlevel prefix="$new_prefix"}}<?php $new_prefix = $prefix . "." . $counter; ?>{{/tree:nextlevel}}</li>' .
                   '{{/tree:node}}' .
-                  '</ul>' . 
+                  '</ul>' .
                 '{{/tree}}';
 
     $tpl = $this->_createTemplate($content, 'tree.html');
 
     $macro = $this->_createMacro($tpl);
-    $macro->set('tree', array(array('title' => 'foo'), 
+    $macro->set('tree', array(array('title' => 'foo'),
                               array('title' => 'bar', 'kids' => array(array('title' => 'bar1'),
-                                                                      array('title' => 'bar2'))), 
+                                                                      array('title' => 'bar2'))),
                               array('title' => 'hey')));
 
     $out = $macro->render();
     $this->assertEqual($out, '<ul><li>1.1)foo</li><li>1.2)bar<ul><li>1.2.1)bar1</li><li>1.2.2)bar2</li></ul></li><li>1.3)hey</li></ul>');
   }
-  
+
   function testCheckBC()
   {
-    $content = '{{tree using="$#tree" as="$item" kids_prop="kids" counter="$counter" prefix="1"}}' . 
-                  '<ul>' . 
-                  '{{tree:branch}}' . 
+    $content = '{{tree using="$#tree" as="$item" kids_prop="kids" counter="$counter" prefix="1"}}' .
+                  '<ul>' .
+                  '{{tree:branch}}' .
                   '<li>{$prefix}.{$counter})'.
-                  '{{tree:item prefix="$new_prefix"}}{$item.title}<?php $new_prefix = $prefix . "." . $counter; ?>{{/tree:item}}</li>' . 
+                  '{{tree:item prefix="$new_prefix"}}{$item.title}<?php $new_prefix = $prefix . "." . $counter; ?>{{/tree:item}}</li>' .
                   '{{/tree:branch}}' .
-                  '</ul>' . 
+                  '</ul>' .
                 '{{/tree}}';
 
     $tpl = $this->_createTemplate($content, 'tree.html');
 
     $macro = $this->_createMacro($tpl);
-    $macro->set('tree', array(array('title' => 'foo'), 
+    $macro->set('tree', array(array('title' => 'foo'),
                               array('title' => 'bar', 'kids' => array(array('title' => 'bar1'),
-                                                                      array('title' => 'bar2'))), 
+                                                                      array('title' => 'bar2'))),
                               array('title' => 'hey')));
 
     $out = $macro->render();
     $this->assertEqual($out, '<ul><li>1.1)foo</li><li>1.2)bar<ul><li>1.2.1)bar1</li><li>1.2.2)bar2</li></ul></li><li>1.3)hey</li></ul>');
   }
+
+  function testTreeWithoutData()
+  {
+    $content = '{{tree using="$#tree" as="$item" kids_prop="kids" counter="$counter" prefix="1"}}' .
+                  '<ul>' .
+                  '{{tree:branch}}' .
+                  '<li>{$prefix}.{$counter})'.
+                  '{{tree:item prefix="$new_prefix"}}{$item.title}<?php $new_prefix = $prefix . "." . $counter; ?>{{/tree:item}}</li>' .
+                  '{{/tree:branch}}' .
+		  '{{tree:empty}}kids not found{{/tree:empty}}' .
+                  '</ul>' .
+                '{{/tree}}';
+
+    $tpl = $this->_createTemplate($content, 'tree.html');
+
+    $macro = $this->_createMacro($tpl);
+    $macro->set('tree', array());
+
+    $out = $macro->render();
+    $this->assertEqual($out, 'kids not found');
+  }
 }
-



More information about the limb-svn mailing list