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

svn at limb-project.com svn at limb-project.com
Sun Sep 16 13:42:02 MSD 2007


Author: pachanga
Date: 2007-09-16 13:42:02 +0400 (Sun, 16 Sep 2007)
New Revision: 6304
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6304

Modified:
   3.x/trunk/limb/macro/src/lmbMacroTag.class.php
   3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php
   3.x/trunk/limb/macro/src/tags/include.tag.php
   3.x/trunk/limb/macro/src/tags/into.tag.php
   3.x/trunk/limb/macro/src/tags/slot.tag.php
   3.x/trunk/limb/macro/src/tags/wrap.tag.php
   3.x/trunk/limb/macro/tests/cases/tags/lmbMacroWrapTagTest.class.php
Log:
-- initial simple implementation of dynamic <%wrap%> seems to be working

Modified: 3.x/trunk/limb/macro/src/lmbMacroTag.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTag.class.php	2007-09-14 18:22:19 UTC (rev 6303)
+++ 3.x/trunk/limb/macro/src/lmbMacroTag.class.php	2007-09-16 09:42:02 UTC (rev 6304)
@@ -75,6 +75,11 @@
     return array_key_exists(strtolower($name), $this->attributes);
   }
 
+  function isVariable($name)
+  {
+    return $this->has($name) && strpos($this->get($name), '$') === 0;
+  }
+
   /**
   * Return the value of a boolean attribute as a boolean.
   * ATTRIBUTE=ANYTHING  (true)

Modified: 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php	2007-09-14 18:22:19 UTC (rev 6303)
+++ 3.x/trunk/limb/macro/src/lmbMacroTemplateExecutor.class.php	2007-09-16 09:42:02 UTC (rev 6304)
@@ -49,5 +49,13 @@
     $template->setVars(get_object_vars($this));//global template vars
     echo $template->render($vars);//local template vars
   }
+
+  function wrapTemplate($file, $slots_handlers)
+  {
+    $template = new lmbMacroTemplate($file, $this->cache_dir, $this->locator);
+    foreach($slots_handlers as $name => $handler)
+      $template->set('__slot_handler_' . $name, $handler);
+    echo $template->render();
+  }
 }
 

Modified: 3.x/trunk/limb/macro/src/tags/include.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/include.tag.php	2007-09-14 18:22:19 UTC (rev 6303)
+++ 3.x/trunk/limb/macro/src/tags/include.tag.php	2007-09-16 09:42:02 UTC (rev 6304)
@@ -43,7 +43,7 @@
 
   function _isDynamic()
   {
-    return strpos($this->get('file'), '$') === 0;
+    return $this->isVariable('file');
   }
 
   function generateContents($code)

Modified: 3.x/trunk/limb/macro/src/tags/into.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/into.tag.php	2007-09-14 18:22:19 UTC (rev 6303)
+++ 3.x/trunk/limb/macro/src/tags/into.tag.php	2007-09-16 09:42:02 UTC (rev 6304)
@@ -32,11 +32,6 @@
 
   function _insert($wrapper, $tree_builder, $point)
   {
-    $this->_insertOrReplace($wrapper, $tree_builder, $point, $replace = false);
-  }
-
-  protected function _insertOrReplace($wrapper, $tree_builder, $point, $replace = false)
-  {
     $insertionPoint = $wrapper->findChild($point);
     if(empty($insertionPoint))
     {
@@ -50,9 +45,6 @@
       $this->raise('Wrap slot not found', $params);
     }
 
-    if($replace)
-      $insertionPoint->removeChildren();
-
     $tree_builder->pushCursor($insertionPoint, $this->location);
   }
 }

Modified: 3.x/trunk/limb/macro/src/tags/slot.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/slot.tag.php	2007-09-14 18:22:19 UTC (rev 6303)
+++ 3.x/trunk/limb/macro/src/tags/slot.tag.php	2007-09-16 09:42:02 UTC (rev 6304)
@@ -22,5 +22,15 @@
  */
 class lmbMacroSlotTag extends lmbMacroTag
 {
+  function generateContents($code)
+  {
+    $slot = $this->getId();
+    //calling slot handler in case of dynamic wrapping
+    $code->writePHP('if(isset($this->__slot_handler_' . $slot . ')) {');
+    $code->writePHP('call_user_func_array($this->__slot_handler_' . $slot . ', array());');
+    $code->writePHP('}');
+
+    parent :: generateContents($code);
+  }
 }
 

Modified: 3.x/trunk/limb/macro/src/tags/wrap.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/wrap.tag.php	2007-09-14 18:22:19 UTC (rev 6303)
+++ 3.x/trunk/limb/macro/src/tags/wrap.tag.php	2007-09-16 09:42:02 UTC (rev 6304)
@@ -22,18 +22,26 @@
  */
 class lmbMacroWrapTag extends lmbMacroTag
 {
+  protected $is_dynamic = false;
+
   function preParse($compiler)
   {
     parent :: preParse($compiler);
 
-    $tree_builder = $compiler->getTreeBuilder();
+    if(!$this->isVariable('with'))
+    {
+      $file = $this->get('with');
+      $this->_compileSourceFileName($file, $compiler);
 
-    $file = $this->get('with');
-    $this->_compileSourceFileName($file, $compiler);
-
-    //if there's no 'into' attribute we consider that <%into%> tags used instead
-    if($into = $this->get('into'))
-      $this->_insert($this, $tree_builder, $into);
+      //if there's no 'into' attribute we consider that <%into%> tags used instead
+      if($into = $this->get('into'))
+      {
+        $tree_builder = $compiler->getTreeBuilder();
+        $this->_insert($this, $tree_builder, $into);
+      }
+    }
+    else
+      $this->is_dynamic = true;
   }
 
   protected function _compileSourceFileName($file, $compiler)
@@ -48,11 +56,6 @@
 
   function _insert($wrapper, $tree_builder, $point)
   {
-    $this->_insertOrReplace($wrapper, $tree_builder, $point, $replace = false);
-  }
-
-  protected function _insertOrReplace($wrapper, $tree_builder, $point, $replace = false)
-  {
     $insertionPoint = $wrapper->findChild($point);
     if(empty($insertionPoint))
     {
@@ -66,10 +69,25 @@
       $this->raise('Wrap slot not found', $params);
     }
 
-    if($replace)
-      $insertionPoint->removeChildren();
+    $tree_builder->pushCursor($insertionPoint, $this->location); 
+  }
 
-    $tree_builder->pushCursor($insertionPoint, $this->location);
+  function generateContents($code)
+  {
+    if($this->is_dynamic)
+    {
+      $method = $code->beginMethod('__slotHandler'. mt_rand());
+      parent :: generateContents($code);
+      $code->endMethod();
+
+      $handlers_str = 'array(';
+      $handlers_str .= '"' . $this->get('into') . '"' . ' => array($this, "' . $method . '")';
+      $handlers_str .= ')';
+
+      $code->writePHP('$this->wrapTemplate(' . $this->get('with') . ', ' . $handlers_str . ');');
+    }
+    else
+      parent :: generateContents($code);
   }
 }
 

Modified: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroWrapTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroWrapTagTest.class.php	2007-09-14 18:22:19 UTC (rev 6303)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroWrapTagTest.class.php	2007-09-16 09:42:02 UTC (rev 6304)
@@ -21,7 +21,7 @@
     lmbFs :: mkdir(LIMB_VAR_DIR . '/tpl/compiled');
   }
 
-  function testSimpleWrap()
+  function testSimpleStaticWrap()
   {
     $bar = '<%wrap with="foo.html" into="slot1"%>Bob<%/wrap%>';
     $foo = '<p>Hello, <%slot id="slot1"/%></p>';
@@ -35,7 +35,7 @@
     $this->assertEqual($out, '<p>Hello, Bob</p>');
   }
 
-  function testWrapWithVariables()
+  function testStaticWrapWithVariables()
   {
     $bar = '<%wrap with="foo.html" into="slot1"%><?php echo $this->bob?><%/wrap%>';
     $foo = '<p>Hello, <%slot id="slot1"/%></p>';
@@ -50,7 +50,7 @@
     $this->assertEqual($out, '<p>Hello, Bob</p>');
   }
 
-  function testNestedWrap()
+  function testNestedStaticWrap()
   {
     $bar = '<%wrap with="foo.html" into="slot1"%><?php echo $this->bob?><%/wrap%>';
     $foo = '<%wrap with="zoo.html" into="slot2"%><p>Hello, <%slot id="slot1"/%></p><%/wrap%>';
@@ -67,7 +67,7 @@
     $this->assertEqual($out, '<body><p>Hello, Bob</p></body>');
   }
 
-  function testMultiWrap()
+  function testMultiStaticWrap()
   {
     $bar = '<%wrap with="foo.html"%><%into slot="slot1"%>Bob<%/into%><%into slot="slot2"%>Thorton<%/into%><%/wrap%>';
     $foo = '<p>Hello, <%slot id="slot2"/%> <%slot id="slot1"/%></p>';
@@ -81,6 +81,39 @@
     $this->assertEqual($out, '<p>Hello, Thorton Bob</p>');
   }
 
+  function testSimpleDynamicWrap()
+  {    
+    $bar = '<%wrap with="$this->layout" into="slot1"%>Bob<%/wrap%>';
+    $foo = '<p>Hello, <%slot id="slot1"/%></p>';
+
+    $bar_tpl = $this->_createTemplate($bar, 'bar.html');
+    $foo_tpl = $this->_createTemplate($foo, 'foo.html');
+
+    $macro = $this->_createMacro($bar_tpl);
+    $macro->set('layout', 'foo.html');
+
+    $out = $macro->render();
+    $this->assertEqual($out, '<p>Hello, Bob</p>'); 
+  }
+
+  function testMixStaticAndDynamicWrap()
+  {
+    $bar = '<%wrap with="$this->layout" into="slot1"%><?php echo $this->bob?><%/wrap%>';
+    $foo = '<%wrap with="zoo.html" into="slot2"%><p>Hello, <%slot id="slot1"/%></p><%/wrap%>';
+    $zoo = '<body><%slot id="slot2"/%></body>';
+
+    $bar_tpl = $this->_createTemplate($bar, 'bar.html');
+    $foo_tpl = $this->_createTemplate($foo, 'foo.html');
+    $zoo_tpl = $this->_createTemplate($zoo, 'zoo.html');
+
+    $macro = $this->_createMacro($bar_tpl);
+    $macro->set('layout', 'foo.html');
+    $macro->set('bob', 'Bob');
+
+    $out = $macro->render();
+    $this->assertEqual($out, '<body><p>Hello, Bob</p></body>');
+  }
+
   protected function _createMacro($file)
   {
     $base_dir = LIMB_VAR_DIR . '/tpl';



More information about the limb-svn mailing list