Refactor builtin functions into a TemplateBuiltins class.
authorRobert Sesek <rsesek@bluestatic.org>
Tue, 4 Jun 2013 01:02:47 +0000 (21:02 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Tue, 4 Jun 2013 01:02:47 +0000 (21:02 -0400)
Also write some poor tests.

testing/tests/views/template_test.php
views/template.php

index eb0d0dc3fcf0721c187316365ea4b9c467e9975f..718394812ab3f363e52c4d4fd8535c861b2c575c 100644 (file)
@@ -1,11 +1,11 @@
 <?php
 // Hoplite
 // Copyright (c) 2011 Blue Static
-// 
+//
 // This program is free software: you can redistribute it and/or modify it
 // under the terms of the GNU General Public License as published by the Free
 // Software Foundation, either version 3 of the License, or any later version.
-// 
+//
 // This program is distributed in the hope that it will be useful, but WITHOUT
 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
@@ -106,4 +106,26 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
 
     $this->assertEquals('Some other', $template->Render());
   }
+
+  public function testBuiltinUrl()
+  {
+    $template = Template::NewWithData('builtin', 'Make a URL {%#url "/foo/bar"%}');
+    $this->assertEquals('Make a URL <?php hoplite\\views\\TemplateBuiltins::MakeURL("/foo/bar") ?>', $template->template());
+
+    $template = Template::NewWithData('builtin', 'Another {%#   url "/foo"   %} URL');
+    $this->assertEquals('Another <?php hoplite\\views\\TemplateBuiltins::MakeURL("/foo") ?> URL', $template->template());
+  }
+
+  public function testBuiltinImport()
+  {
+    $template = Template::NewWithData('test', 'Import {%# import \'_template\' %} tpl');
+    $this->assertEquals(
+        'Import <?php hoplite\\views\\TemplateBuiltins::Import(\'_template\', $__template_vars) ?> tpl',
+        $template->template());
+
+    $template = Template::NewWithData('test', 'Import {%#import "tpl", array("foo" => "bar")%} import');
+    $this->assertEquals(
+        'Import <?php hoplite\\views\\TemplateBuiltins::Import("tpl", array_merge($__template_vars,  array("foo" => "bar"))) ?> import',
+        $template->template());
+  }
 }
index 185c3bb582ed3753e78d1091a57e438c84645e93..e67d27252bb1df12b2898235dae89b023a23f516 100644 (file)
@@ -21,26 +21,6 @@ use \hoplite\base\Profiling;
 require_once HOPLITE_ROOT . '/base/filter.php';
 require_once HOPLITE_ROOT . '/base/profiling.php';
 
-/*!
-  Renders a template with additional vars.
-  @param string The template name to render
-  @param array Variables with which to render.
-*/
-function Inject($name, $vars = array())
-{
-  echo TemplateLoader::Fetch($name)->Render($vars);
-}
-
-/*! @brief Creates a URL via RootController::MakeURL().
-  This requires the root controller be set in the $GLOBALS as
-  hoplite\http\RootController.
-  @param string Path.
-*/
-function MakeURL($path)
-{
-  return $GLOBALS['hoplite\http\RootController']->MakeURL($path, FALSE);
-}
-
 /*!
   Template parses a a text file (typically HTML) by expanding a small macro
   language into "compiled" PHP.
@@ -301,7 +281,7 @@ class Template
       throw new TemplateException('No macro function specified');
 
     $function = substr($macro, 0, $function_pos);
-    $args = substr($macro, $function_pos);
+    $args = substr($macro, $function_pos + 1);
     switch ($function) {
       case 'url':    return $this->_Builtin_url($args);
       case 'import': return $this->_Builtin_import($args);
@@ -312,7 +292,7 @@ class Template
 
   protected function _Builtin_url($args)
   {
-    return "echo hoplite\\views\\MakeURL($args)";
+    return "hoplite\\views\\TemplateBuiltins::MakeURL($args)";
   }
 
   protected function _Builtin_import($args)
@@ -331,7 +311,33 @@ class Template
     else
       $vars = '$__template_vars';
 
-    return "hoplite\\views\\Inject($template, $vars)";
+    return "hoplite\\views\\TemplateBuiltins::Import($template, $vars)";
+  }
+}
+
+/*!
+  Namespace for builtin template macros.
+*/
+class TemplateBuiltins
+{
+  /*!
+    Renders a template with additional vars.
+    @param string The template name to render
+    @param array Variables with which to render.
+  */
+  static public function Import($template, $vars)
+  {
+    echo TemplateLoader::Fetch($template)->Render($vars);
+  }
+
+  /*! @brief Creates a URL via RootController::MakeURL().
+    This requires the root controller be set in the $GLOBALS as
+    hoplite\http\RootController.
+    @param string Path.
+  */
+  static public function MakeURL($path)
+  {
+    echo $GLOBALS['hoplite\http\RootController']->MakeURL($path, FALSE);
   }
 }