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

svn at limb-project.com svn at limb-project.com
Tue Oct 2 18:39:46 MSD 2007


Author: serega
Date: 2007-10-02 18:39:45 +0400 (Tue, 02 Oct 2007)
New Revision: 6363
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6363

Added:
   3.x/trunk/limb/macro/src/tags/list_even.tag.php
   3.x/trunk/limb/macro/src/tags/list_fill.tag.php
   3.x/trunk/limb/macro/src/tags/list_odd.tag.php
   3.x/trunk/limb/macro/src/tags/lmbMacroListGlueHelper.class.php
   3.x/trunk/limb/macro/tests/cases/tags/.setup.php
   3.x/trunk/limb/macro/tests/cases/tags/lmbBaseMacroTagTest.class.php
Modified:
   3.x/trunk/limb/macro/src/tags/list.tag.php
   3.x/trunk/limb/macro/src/tags/list_empty.tag.php
   3.x/trunk/limb/macro/src/tags/list_glue.tag.php
   3.x/trunk/limb/macro/src/tags/list_item.tag.php
   3.x/trunk/limb/macro/tests/cases/tags/lmbMacroListTagTest.class.php
Log:
-- more advanced version of MACRO list tags (step attribute for glue tag, new fill tag, ability to use several glue tags etc.)

Modified: 3.x/trunk/limb/macro/src/tags/list.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/list.tag.php	2007-10-02 06:37:22 UTC (rev 6362)
+++ 3.x/trunk/limb/macro/src/tags/list.tag.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -2,9 +2,9 @@
 /*
  * Limb PHP Framework
  *
- * @link http://limb-project.com 
+ * @link http://limb-project.com
  * @copyright  Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
  */
 
 lmb_require('limb/macro/src/lmbMacroTag.class.php');
@@ -17,80 +17,87 @@
  */
 class lmbMacroListTag extends lmbMacroTag
 {
+  protected $counter_var_var;
+  protected $count_source = false;
+
+  function countSource()
+  {
+    $this->count_source = true;
+  }
+
   function generateContents($code)
   {
-    $using = $this->get('using');
-
     if(!$as = $this->get('as'))
       $as = '$item';
 
     //internal list counter
-    $counter = $code->getTempVarRef();
-    $code->writePHP($counter . ' = 0;');
-    $code->writePHP('foreach(' . $using . ' as ' . $as . ') {');
+    $this->counter_var = $code->getTempVarRef();
+    $code->writePHP($this->counter_var . ' = 0;');
 
+    $this->_prepareSourceVar($code);
+
+    $code->writePHP('foreach(' . $this->source_var . ' as ' . $as . ') {');
+
     if($user_counter = $this->get('counter'))
-      $code->writePHP($user_counter . ' = ' . $counter . '+1;');
+      $code->writePHP($user_counter . ' = ' . $this->counter_var . '+1;');
 
-    //glue tag
-    $glue = $this->findImmediateChildByClass('lmbMacroListGlueTag');
+    if($parity = $this->get('parity'))
+      $code->writePHP($parity . ' = (( (' . $this->counter_var . ' + 1) % 2) ? "odd" : "even");');
 
     $found_item_tag = false;
     $postponed_nodes = array();
 
     foreach($this->children as $child)
     {
-      //we want to skip all {{list:*}} tags, since they are rendered manually
+      //we want to skip some of  {{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) {');
+          $code->writePHP('if(' . $this->counter_var . ' == 0) {');
           $child->generateContents($code);
           $code->writePHP('}');
         }
-        //otherwise we collect them to display later 
+        //otherwise we collect them to display later
         else
           $postponed_nodes[] = $child;
       }
       elseif(is_a($child, 'lmbMacroListItemTag'))
       {
         $found_item_tag = true;
-        if($glue)
-        {
-          $code->writePHP('if('. $counter . ' > 0) {');
-          $glue->generateContents($code);
-          $code->writePHP('}');
-        }
         $child->generateContents($code);
       }
     }
 
-    $code->writePHP($counter . '++;');
+    $code->writePHP($this->counter_var . '++;');
     $code->writePHP('}');
 
     //tags after {{list:item}} should be rendered only if there were any items
     foreach($postponed_nodes as $node)
     {
-      $code->writePHP('if(' . $counter . ' > 0) {');
+      $code->writePHP('if(' . $this->counter_var . ' > 0) {');
       $node->generateContents($code);
       $code->writePHP('}');
     }
 
-    if($list_empty = $this->findImmediateChildByClass('lmbMacroListEmptyTag'))
-    {
-      $code->writePHP('if(' . $counter . ' == 0) {');
-      $list_empty->generateContents($code);
-      $code->writePHP('}');
-    }
+    $this->_renderEmptyTag($code);
   }
 
+  function getCounterVar()
+  {
+    return $this->counter_var;
+  }
+
+  function getSourceVar()
+  {
+    return $this->source_var;
+  }
+
   protected function _isOneOfListTags($node)
   {
     $classes = array('lmbMacroListEmptyTag',
-                   'lmbMacroListItemTag',
-                   'lmbMacroListGlueTag');
+                     'lmbMacroListItemTag');
 
     foreach($classes as $class)
     {
@@ -99,5 +106,33 @@
     }
     return false;
   }
+
+  protected function _prepareSourceVar($code)
+  {
+    $using = $this->get('using');
+
+    $this->source_var = $code->getTempVarRef();
+    $item_var = $code->getTempVarRef();
+
+    if($this->count_source)
+    {
+      $code->writePHP($this->source_var . " = array();\n");
+      $code->writePHP('foreach(' . $using . " as $item_var) {\n");
+        $code->writePHP($this->source_var . "[] = $item_var;\n");
+      $code->writePHP("}\n;");
+    }
+    else
+      $code->writePHP($this->source_var . " = {$using};\n");
+  }
+
+  protected function _renderEmptyTag($code)
+  {
+    if($list_empty = $this->findImmediateChildByClass('lmbMacroListEmptyTag'))
+    {
+      $code->writePHP('if(' . $this->counter_var . ' == 0) {');
+      $list_empty->generateContents($code);
+      $code->writePHP('}');
+    }
+  }
 }
 

Modified: 3.x/trunk/limb/macro/src/tags/list_empty.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/list_empty.tag.php	2007-10-02 06:37:22 UTC (rev 6362)
+++ 3.x/trunk/limb/macro/src/tags/list_empty.tag.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -2,9 +2,9 @@
 /*
  * Limb PHP Framework
  *
- * @link http://limb-project.com 
+ * @link http://limb-project.com
  * @copyright  Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
  */
 
 lmb_require('limb/macro/src/lmbMacroTag.class.php');

Added: 3.x/trunk/limb/macro/src/tags/list_even.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/list_even.tag.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/list_even.tag.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -0,0 +1,30 @@
+<?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
+ */
+
+lmb_require('limb/macro/src/lmbMacroTag.class.php');
+
+/**
+ * Renders a portion of the template if the current list row is even
+ * @tag list:even
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroListRowEvenTag extends lmbMacroTag
+{
+  function generateContents($code)
+  {
+    $list = $this->findParentByClass('lmbMacroListTag');
+    $counter_var = $list->getCounterVar();
+
+    $code->writePHP('if(('. $counter_var . '+1) % 2 == 0) {');
+    parent :: generateContents($code);
+    $code->writePHP('}');
+  }
+}
+

Added: 3.x/trunk/limb/macro/src/tags/list_fill.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/list_fill.tag.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/list_fill.tag.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -0,0 +1,50 @@
+<?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
+ */
+
+lmb_require('limb/macro/src/lmbMacroTag.class.php');
+
+/**
+ * Compile time component for output finalizers in a list
+ * Allows to generate valid layout while output multicolumn lists
+ * Default ratio attribute is 1  * @tag list:fill
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroListFillTag extends lmbMacroTag
+{
+  function generateContents($code)
+  {
+    $ratio_var = $code->getTempVarRef();
+    if($ratio = $this->get('upto'))
+      $code->writePHP($ratio_var . " = $ratio;\n");
+    else
+      $code->writePHP($ratio_var . " = 1;\n");
+
+    $list = $this->findParentByClass('lmbMacroListTag');
+
+    $count_var = $code->getTempVarRef();
+    $items_left_var = $code->getTempVarRef();
+    $code->writePhp($count_var .' = count('. $list->getSourceVar() . ');');
+
+    $code->writePhp("if ({$count_var}/{$ratio_var} > 1) \n");
+    $code->writePhp($items_left_var . " = ceil({$count_var}/{$ratio_var})*{$ratio_var} - {$count_var}; \n");
+    $code->writePhp("else \n");
+    $code->writePhp($items_left_var . " = 0;\n");
+
+    $code->writePhp("if ({$items_left_var}){\n");
+
+    if($items_left = $this->get('items_left'))
+      $code->writePhp($items_left . " = {$items_left_var};");
+
+    parent :: generateContents($code);
+
+    $code->writePhp('}'. "\n");
+  }
+}
+

Modified: 3.x/trunk/limb/macro/src/tags/list_glue.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/list_glue.tag.php	2007-10-02 06:37:22 UTC (rev 6362)
+++ 3.x/trunk/limb/macro/src/tags/list_glue.tag.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -17,5 +17,70 @@
  */
 class lmbMacroListGlueTag extends lmbMacroTag
 {
+  protected $step_var;
+  protected $helper_var;
+
+  function preParse($compiler)
+  {
+    $list = $this->findParentByClass('lmbMacroListTag');
+    $list->countSource();
+  }
+
+  function generateContents($code)
+  {
+    $step_var = $this->getStepVar($code);
+    $helper_var = $this->getHelperVar($code);
+
+    $code->writePHP("if (!isset({$helper_var})){\n");
+    $code->registerInclude('limb/macro/src/tags/lmbMacroListGlueHelper.class.php');
+    $code->writePHP($helper_var . " = new lmbMacroListGlueHelper();\n");
+
+    if($step = $this->get('step'))
+      $code->writePHP($step_var . " = {$step};\n");
+    else
+      $code->writePHP($step_var . " = 1;\n");
+
+    $code->writePhp($helper_var . "->setStep({$step_var});\n");
+    $list = $this->findParentByClass('lmbMacroListTag');
+    $source_var = $list->getSourceVar();
+    $code->writePhp($helper_var . "->setTotalItems(count($source_var));\n");
+
+    $code->writePHP("}\n");
+
+    $code->writePhp($helper_var . "->next();\n");
+
+    $code->writePhp("if ( " . $helper_var  . "->shouldDisplay()){\n");
+
+    $code->writePhp($helper_var . "->reset();\n");
+
+    $separators = $this->parent->findImmediateChildrenByClass('lmbMacroListGlueTag');
+    if(array($separators) && count($separators))
+    {
+      foreach($separators as $separator)
+      {
+        $code->writePhp('if (' . $separator->getStepVar($code) . ' < ' . $step_var . ') ');
+        $code->writePhp($separator->getHelperVar($code) . "->skipNext();\n");
+      }
+    }
+
+    parent :: generateContents($code);
+
+    $code->writePhp("}\n");
+  }
+
+  function getStepVar($code)
+  {
+    if(!$this->step_var)
+      $this->step_var = $code->getTempVarRef();
+
+    return $this->step_var;
+  }
+
+  function getHelperVar($code)
+  {
+    if(!$this->helper_var)
+      $this->helper_var = $code->getTempVarRef();
+    return $this->helper_var;
+  }
 }
 

Modified: 3.x/trunk/limb/macro/src/tags/list_item.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/list_item.tag.php	2007-10-02 06:37:22 UTC (rev 6362)
+++ 3.x/trunk/limb/macro/src/tags/list_item.tag.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -2,9 +2,9 @@
 /*
  * Limb PHP Framework
  *
- * @link http://limb-project.com 
+ * @link http://limb-project.com
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
  */
 
 lmb_require('limb/macro/src/lmbMacroTag.class.php');

Added: 3.x/trunk/limb/macro/src/tags/list_odd.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/list_odd.tag.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/list_odd.tag.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -0,0 +1,30 @@
+<?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
+ */
+
+lmb_require('limb/macro/src/lmbMacroTag.class.php');
+
+/**
+ * Renders a portion of the template if the current list row is odd
+ * @tag list:odd
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroListRowOddTag extends lmbMacroTag
+{
+  function generateContents($code)
+  {
+    $list = $this->findParentByClass('lmbMacroListTag');
+    $counter_var = $list->getCounterVar();
+
+    $code->writePHP('if(('. $counter_var . ' + 1) % 2 != 0) {');
+    parent :: generateContents($code);
+    $code->writePHP('}');
+  }
+}
+

Added: 3.x/trunk/limb/macro/src/tags/lmbMacroListGlueHelper.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/lmbMacroListGlueHelper.class.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/lmbMacroListGlueHelper.class.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -0,0 +1,74 @@
+<?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
+ */
+
+/**
+ * Created just to simplify list:glue tag code
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroListGlueHelper
+{
+  protected $step = 1;
+  protected $step_counter = 0;
+  protected $total = 0;
+  protected $total_counter = 0;
+  protected $list_component = null;
+  protected $skip_next = false;
+
+  function setStep($step)
+  {
+    if($step < 1)
+      $step = 1;
+
+    $this->step = $step;
+  }
+
+  function next()
+  {
+    if($this->skip_next)
+      $this->step_counter = 0;
+    else
+      $this->step_counter++;
+
+    $this->total_counter++;
+  }
+
+  function reset()
+  {
+    $this->step_counter = 0;
+  }
+
+  function setTotalItems($total_items)
+  {
+    $this->total = $total_items;
+  }
+
+  function shouldDisplay()
+  {
+    if($this->skip_next ||
+       ($this->step_counter != $this->step) ||
+       ($this->total_counter >= $this->total))
+    {
+      $result = false;
+    }
+    else
+      $result = true;
+
+    $this->skip_next = false;
+
+    return $result;
+  }
+
+  function skipNext()
+  {
+    $this->reset();
+    $this->skip_next = true;
+  }
+}
+

Added: 3.x/trunk/limb/macro/tests/cases/tags/.setup.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/.setup.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/tests/cases/tags/.setup.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -0,0 +1,3 @@
+<?php
+require_once(dirname(__FILE__) . '/lmbBaseMacroTagTest.class.php');
+

Added: 3.x/trunk/limb/macro/tests/cases/tags/lmbBaseMacroTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbBaseMacroTagTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbBaseMacroTagTest.class.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -0,0 +1,37 @@
+<?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
+ */
+
+lmb_require('limb/fs/src/lmbFs.class.php');
+lmb_require('limb/macro/src/lmbMacroTemplate.class.php');
+lmb_require('limb/macro/src/lmbMacroTagDictionary.class.php');
+
+class lmbBaseMacroTagTest extends UnitTestCase
+{
+  function setUp()
+  {
+    lmbFs :: rm(LIMB_VAR_DIR . '/tpl');
+    lmbFs :: mkdir(LIMB_VAR_DIR . '/tpl/compiled');
+  }
+
+  protected function _createMacro($file)
+  {
+    $base_dir = LIMB_VAR_DIR . '/tpl';
+    $cache_dir = LIMB_VAR_DIR . '/tpl/compiled';
+    $macro = new lmbMacroTemplate($file, new lmbMacroConfig($cache_dir, true, true, array($base_dir)));
+    return $macro;
+  }
+
+  protected function _createTemplate($code, $name)
+  {
+    $file = LIMB_VAR_DIR . '/tpl/' . $name;
+    file_put_contents($file, $code);
+    return $file;
+  }
+}
+

Modified: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroListTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroListTagTest.class.php	2007-10-02 06:37:22 UTC (rev 6362)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroListTagTest.class.php	2007-10-02 14:39:45 UTC (rev 6363)
@@ -2,23 +2,13 @@
 /*
  * Limb PHP Framework
  *
- * @link http://limb-project.com 
+ * @link http://limb-project.com
  * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
  */
 
-lmb_require('limb/fs/src/lmbFs.class.php');
-lmb_require('limb/macro/src/lmbMacroTemplate.class.php');
-lmb_require('limb/macro/src/lmbMacroTagDictionary.class.php');
-
-class lmbMacroListTagTest extends UnitTestCase
+class lmbMacroListTagTest extends lmbBaseMacroTagTest
 {
-  function setUp()
-  {
-    lmbFs :: rm(LIMB_VAR_DIR . '/tpl');
-    lmbFs :: mkdir(LIMB_VAR_DIR . '/tpl/compiled');
-  }
-
   function testSimpleList()
   {
     $list = '{{list using="$#list" as="$item"}}{{list:item}}<?=$item?> {{/list:item}}{{/list}}';
@@ -47,7 +37,7 @@
 
   function testEmptyList()
   {
-    $list = '{{list using="$#list" as="$item"}}{{list:item}}<?=$item?>{{/list:item}}' . 
+    $list = '{{list using="$#list" as="$item"}}{{list:item}}<?=$item?>{{/list:item}}' .
             '{{list:empty}}Nothing{{/list:empty}}{{/list}}';
 
     $list_tpl = $this->_createTemplate($list, 'list.html');
@@ -87,7 +77,7 @@
 
   function testTextNodesInsideListTagWithEmptyListTag()
   {
-    $list = '{{list using="$#list" as="$item"}}List: {{list:item}}<?=$item?> {{/list:item}} !' . 
+    $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');
@@ -99,10 +89,39 @@
     $this->assertEqual($out, 'Nothing');
   }
 
+  function testParity()
+  {
+    $list = '{{list using="$#list" as="$item" parity="$parity"}}{{list:item}}{{$parity}}-{{$item}} {{/list:item}} !{{/list}}';
+
+    $list_tpl = $this->_createTemplate($list, 'list.html');
+
+    $macro = $this->_createMacro($list_tpl);
+    $macro->set('list', array('Bob', 'Todd', 'Jeff'));
+
+    $out = $macro->render();
+    $this->assertEqual($out, 'odd-Bob even-Todd odd-Jeff  !');
+  }
+
+  function testEvenAndOddTags()
+  {
+    $list = '{{list using="$#list" as="$item" parity="$parity"}}{{list:item}}'.
+              '{{list:odd}}Odd{{/list:odd}}{{list:even}}Even{{/list:even}}-{{$item}} {{/list:item}} !{{/list}}';
+
+    $list_tpl = $this->_createTemplate($list, 'list.html');
+
+    $macro = $this->_createMacro($list_tpl);
+    $macro->set('list', array('Bob', 'Todd', 'Jeff'));
+
+    $out = $macro->render();
+    $this->assertEqual($out, 'Odd-Bob Even-Todd Odd-Jeff  !');
+  }
+
   function testListWithGlue()
   {
-    $list = '{{list using="$#list" as="$item"}}List:{{list:item}}<?=$item?>{{/list:item}}!' . 
-            '{{list:glue}}||{{/list:glue}}{{/list}}';
+    $list = '{{list using="$#list" as="$item"}}List:'.
+            '{{list:item}}<?=$item?>{{list:glue}}||{{/list:glue}}'.
+            '{{/list:item}}!' .
+            '{{/list}}';
 
     $list_tpl = $this->_createTemplate($list, 'list.html');
 
@@ -113,19 +132,73 @@
     $this->assertEqual($out, 'List:Bob||Todd||Marry!');
   }
 
-  protected function _createMacro($file)
+  function testListWithGlueWithStep()
   {
-    $base_dir = LIMB_VAR_DIR . '/tpl';
-    $cache_dir = LIMB_VAR_DIR . '/tpl/compiled';
-    $macro = new lmbMacroTemplate($file, new lmbMacroConfig($cache_dir, true, true, array($base_dir)));
-    return $macro;
+    $list = '{{list using="$#list" as="$item"}}List:'.
+            '{{list:item}}<?=$item?>{{list:glue step="2"}}||{{/list:glue}}'.
+            '{{/list:item}}!' .
+            '{{/list}}';
+
+    $list_tpl = $this->_createTemplate($list, 'list.html');
+
+    $macro = $this->_createMacro($list_tpl);
+    $macro->set('list', array('Bob', 'Todd', 'Marry'));
+
+    $out = $macro->render();
+    $this->assertEqual($out, 'List:BobTodd||Marry!');
   }
 
-  protected function _createTemplate($code, $name)
+  function testTwoDependentGlues()
   {
-    $file = LIMB_VAR_DIR . '/tpl/' . $name;
-    file_put_contents($file, $code);
-    return $file;
+    $list = '{{list using="$#list" as="$item"}}List#'.
+            '{{list:item}}<?=$item?>' .
+            '{{list:glue step="2"}}|{{/list:glue}}'.
+            '{{list:glue}}:{{/list:glue}}'.
+            '{{/list:item}}!'.
+            '{{/list}}';
+
+    $list_tpl = $this->_createTemplate($list, 'list.html');
+
+    $macro = $this->_createMacro($list_tpl);
+    $macro->set('list', array('John', 'Pavel', 'Peter', 'Harry', 'Roman', 'Sergey'));
+
+    $this->assertEqual($macro->render(), 'List#John:Pavel|Peter:Harry|Roman:Sergey!');
   }
+
+  function testListFillTagWithRatio()
+  {
+    $list = '{{list using="$#list" as="$item"}}List#'.
+                '{{list:item}}{{$item}}'.
+                '{{list:glue step="3"}}++{{/list:glue}}'.
+                '{{list:glue}}:{{/list:glue}}'.
+                '{{/list:item}}'.
+                '{{list:fill upto="3" items_left="$items_left"}}{{$items_left}}{{/list:fill}}'.
+                '{{/list}}';
+
+    $list_tpl = $this->_createTemplate($list, 'list.html');
+
+    $macro = $this->_createMacro($list_tpl);
+    $macro->set('list', array('John', 'Pavel', 'Peter', 'Harry'));
+
+    $this->assertEqual($macro->render(), 'List#John:Pavel:Peter++Harry2');
+  }
+
+  function testListFillTagWithTotalElementsLessThanRatio()
+  {
+    $list = '{{list using="$#list" as="$item"}}List#'.
+                '{{list:item}}{{$item}}'.
+                '{{list:glue step="3"}}++{{/list:glue}}'.
+                '{{list:glue}}:{{/list:glue}}'.
+                '{{/list:item}}'.
+                '{{list:fill upto="3" items_left="$items_left"}}{{$items_left}}{{/list:fill}}'.
+                '{{/list}}';
+
+    $list_tpl = $this->_createTemplate($list, 'list.html');
+
+    $macro = $this->_createMacro($list_tpl);
+    $macro->set('list', array('John', 'Pavel'));
+
+    $this->assertEqual($macro->render(), 'List#John:Pavel');
+  }
 }
 



More information about the limb-svn mailing list