[limb-svn] r6275 - in 3.x/trunk/limb/view: src tests/cases

svn at limb-project.com svn at limb-project.com
Sat Sep 8 19:44:13 MSD 2007


Author: korchasa
Date: 2007-09-08 19:44:12 +0400 (Sat, 08 Sep 2007)
New Revision: 6275
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6275

Added:
   3.x/trunk/limb/view/src/lmbBlitzView.class.php
   3.x/trunk/limb/view/tests/cases/lmbBlitzViewTest.class.php
Log:
rough lmbBlitzView

Added: 3.x/trunk/limb/view/src/lmbBlitzView.class.php
===================================================================
--- 3.x/trunk/limb/view/src/lmbBlitzView.class.php	                        (rev 0)
+++ 3.x/trunk/limb/view/src/lmbBlitzView.class.php	2007-09-08 15:44:12 UTC (rev 6275)
@@ -0,0 +1,49 @@
+<?php
+
+lmb_require('limb/view/src/lmbView.class.php');
+
+class lmbBlitzView extends lmbView
+{
+    private $templateInstance;
+
+    function __call($methodName, $params)
+    {
+        $tpl = $this->getTemplateInstance();
+        if(!method_exists($tpl, $methodName)) {
+            throw new lmbException(
+                'Wrong template method called', 
+                array(
+                    'template class' => get_class($tpl),
+                    'method' => $methodName,
+                )
+            );
+        }
+        return call_user_method_array($methodName, $tpl, $params);        
+    }
+
+    function getTemplateInstance()
+    {
+        if(!$this->templateInstance) {
+            if(!$this->hasTemplate()) {
+                throw new lmbException('template not defined');
+            }
+            $this->templateInstance = new Blitz($this->getTemplate());
+        }
+        return $this->templateInstance;
+    }
+    
+    function processVariables()
+    {
+        $vars = $this->getVariables();
+        foreach ($vars as $name => $value) {
+            $this->getTemplateInstance()->set(array($name => $value));
+        }        
+    }
+
+    function render()
+    {
+        $this->processVariables();
+        return $this->getTemplateInstance()->parse();
+    }
+
+}
\ No newline at end of file

Added: 3.x/trunk/limb/view/tests/cases/lmbBlitzViewTest.class.php
===================================================================
--- 3.x/trunk/limb/view/tests/cases/lmbBlitzViewTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/view/tests/cases/lmbBlitzViewTest.class.php	2007-09-08 15:44:12 UTC (rev 6275)
@@ -0,0 +1,80 @@
+<?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/view/src/lmbBlitzView.class.php');
+
+class lmbBlitzViewTest extends UnitTestCase
+{
+    private function _createTemplateFile($name, $source)
+    {
+        file_put_contents($path = LIMB_VAR_DIR . $name, $source);
+        return $path;
+    }
+
+    function testRenderSimpleVars()
+    {
+        $template = '{{$hello}}{{$again}}';
+        $path = $this->_createTemplateFile('/simple.bhtml', $template);
+
+        $view = new lmbBlitzView($path);
+        $view->set('hello', 'Hello message!');
+        $view->set('again', 'Hello again!');
+
+        $this->assertEqual($view->render(), 'Hello message!Hello again!');
+    }
+    
+    function testManualTemplateFunctionCall()
+    {
+        $template = '{{BEGIN foo}}{{END}}';
+        $path = $this->_createTemplateFile('/simple.bhtml', $template);
+
+        $view = new lmbBlitzView($path);        
+        $this->assertTrue($view->hasContext('foo'));
+        $this->assertFalse($view->hasContext('bar'));
+    }
+
+    function testRenderIteratedTemplates()
+    {
+        $template = 
+            '{{ BEGIN outer }}o'
+                .'{{ $outer_var }}'
+                .'{{ BEGIN inner }}i'
+                    .'{{ $inner_var }}'
+                .'{{ END inner }}'
+            .'{{ END }}';
+
+        $data = array (
+                array(
+                    'outer_var' => 'a',
+                    'inner' => array(
+                        array('inner_var' => '1'),
+                        array('inner_var' => '2'),
+                        array('inner_var' => '3'),
+                        ),
+                array(
+                    'outer_var' => 'b',
+                    'inner' => array(
+                        array('inner_var' => '4'),
+                        array('inner_var' => '5'),
+                        array('inner_var' => '6'),
+                        ),
+                )
+                )
+        );
+        
+        $out = 'oai1i2i3obi4i5i6';
+        
+        $path = $this->_createTemplateFile('/iteration.bhtml', $template);
+
+        $view = new lmbBlitzView($path);
+        $view->set('outer', $data);
+
+        $this->assertEqual($view->render(), $out);
+    }
+    
+}



More information about the limb-svn mailing list