[limb-svn] r6420 - in 3.x/trunk/limb/macro: src/tags tests/cases/tags
svn at limb-project.com
svn at limb-project.com
Tue Oct 16 01:37:38 MSD 2007
Author: pachanga
Date: 2007-10-16 01:37:38 +0400 (Tue, 16 Oct 2007)
New Revision: 6420
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6420
Added:
3.x/trunk/limb/macro/src/tags/tree.tag.php
3.x/trunk/limb/macro/src/tags/tree_branch.tag.php
3.x/trunk/limb/macro/src/tags/tree_item.tag.php
3.x/trunk/limb/macro/tests/cases/tags/lmbMacroTreeTagTest.class.php
Log:
-- adding proof of concept {{tree}} macro
Added: 3.x/trunk/limb/macro/src/tags/tree.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/tree.tag.php (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/tree.tag.php 2007-10-15 21:37:38 UTC (rev 6420)
@@ -0,0 +1,93 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/macro/src/lmbMacroTag.class.php');
+
+/**
+ * @tag tree
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroTreeTag extends lmbMacroTag
+{
+ function generateContents($code)
+ {
+ if(!$level = $this->get('level'))
+ $level = '$level';
+
+ if(!$as = $this->get('as'))
+ $as = '$item';
+
+ if(!$kids_prop = $this->get('kids_prop'))
+ $kids_prop = 'kids';
+
+ $before_branch = $this->_getTagsBeforeBranch();
+ $after_branch = $this->_getTagsAfterBranch();
+ $branch = $this->findImmediateChildByClass('lmbMacroTreeBranchTag');
+
+ $tree = $this->get('using');
+
+ $items = $code->getTempVarRef();
+ $counter = $code->getTempVarRef();
+
+ $method = $code->beginMethod('_render_tree'. uniqid(), array($items, $level));
+ $code->writePHP($counter . '=0;');
+
+ $code->writePHP('foreach(' . $items . ' as ' . $as . ') {');
+
+ //rendering tags before branch
+ $code->writePHP('if(!' . $counter . ') {');
+ foreach($before_branch as $tag)
+ $tag->generateContents($code);
+ $code->writePHP('}');
+
+ $branch->setRecursionMethod($method);
+ $branch->generateContents($code);
+
+ $code->writePHP($counter . '++;');
+ $code->writePHP('}');//foreach
+
+ //rendering tags after branch
+ $code->writePHP('if(' . $counter . ') {');
+ foreach($after_branch as $tag)
+ $tag->generateContents($code);
+ $code->writePHP('}');
+
+ $code->endMethod();
+
+ $code->writePHP('$this->' . $method . '(' . $tree . ', 0);');
+ }
+
+ protected function _getTagsBeforeBranch()
+ {
+ $tags = array();
+ foreach($this->children as $child)
+ {
+ if(is_a($child, 'lmbMacroTreeBranchTag'))
+ break;
+ $tags[] = $child;
+ }
+ return $tags;
+ }
+
+ protected function _getTagsAfterBranch()
+ {
+ $tags = array();
+ $collect = false;
+ foreach($this->children as $child)
+ {
+ if($collect)
+ $tags[] = $child;
+ if(is_a($child, 'lmbMacroTreeBranchTag'))
+ $collect = true;
+ }
+ return $tags;
+ }
+}
+
Added: 3.x/trunk/limb/macro/src/tags/tree_branch.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/tree_branch.tag.php (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/tree_branch.tag.php 2007-10-15 21:37:38 UTC (rev 6420)
@@ -0,0 +1,86 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/macro/src/lmbMacroTag.class.php');
+
+/**
+ * @tag tree:branch
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroTreeBranchTag extends lmbMacroTag
+{
+ protected $method;
+
+ function setRecursionMethod($name)
+ {
+ $this->method = $name;
+ }
+
+ function generateContents($code)
+ {
+ if(!$level = $this->parent->get('level'))
+ $level = '$level';
+
+ if(!$as = $this->parent->get('as'))
+ $as = '$item';
+
+ if(!$kids_prop = $this->parent->get('kids_prop'))
+ $kids_prop = 'kids';
+
+ $before_item = $this->_getTagsBeforeItem();
+ $after_item = $this->_getTagsAfterItem();
+ $item = $this->findImmediateChildByClass('lmbMacroTreeItemTag');
+
+ $code->writePHP('if(isset(' . $as . '["' . $kids_prop . '"])) {');
+
+ foreach($before_item as $tag)
+ $tag->generateContents($code);
+
+ $item->generateContents($code);
+
+ $code->writePHP('$this->' . $this->method . '(' . $as . '["' . $kids_prop . '"], ' . $level . ' + 1);');
+
+ foreach($after_item as $tag)
+ $tag->generateContents($code);
+
+ $code->writePHP('} else {');
+
+ parent :: generateContents($code);
+
+ $code->writePHP('}');
+ }
+
+ protected function _getTagsBeforeItem()
+ {
+ $tags = array();
+ foreach($this->children as $child)
+ {
+ if(is_a($child, 'lmbMacroTreeItemTag'))
+ break;
+ $tags[] = $child;
+ }
+ return $tags;
+ }
+
+ protected function _getTagsAfterItem()
+ {
+ $tags = array();
+ $collect = false;
+ foreach($this->children as $child)
+ {
+ if($collect)
+ $tags[] = $child;
+ if(is_a($child, 'lmbMacroTreeItemTag'))
+ $collect = true;
+ }
+ return $tags;
+ }
+}
+
Added: 3.x/trunk/limb/macro/src/tags/tree_item.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/tree_item.tag.php (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/tree_item.tag.php 2007-10-15 21:37:38 UTC (rev 6420)
@@ -0,0 +1,20 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/macro/src/lmbMacroTag.class.php');
+
+/**
+ * @tag tree:item
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroTreeItemTag extends lmbMacroTag
+{
+}
+
Added: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroTreeTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroTreeTagTest.class.php (rev 0)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroTreeTagTest.class.php 2007-10-15 21:37:38 UTC (rev 6420)
@@ -0,0 +1,34 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+class lmbMacroTreeTagTest extends lmbBaseMacroTest
+{
+ function testRenderTree()
+ {
+ $content = '{{tree using="$#tree" as="$item" kids_prop="kids"}}' .
+ '<ul>' .
+ '{{tree:branch}}' .
+ '<li>{{tree:item}}{$item.title}{{/tree:item}}</li>' .
+ '{{/tree:branch}}' .
+ '</ul>' .
+ '{{/tree}}';
+
+ $tpl = $this->_createTemplate($content, 'tree.html');
+
+ $macro = $this->_createMacro($tpl);
+ $macro->set('tree', array(array('title' => 'foo'),
+ array('title' => 'bar', 'kids' => array(array('title' => 'bar1'),
+ 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>');
+ }
+}
+
More information about the limb-svn
mailing list