From 1f9ff36e572b91901c2aa38ebccf6b8f37c53128 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 3 Jun 2013 21:02:47 -0400 Subject: [PATCH] Refactor builtin functions into a TemplateBuiltins class. Also write some poor tests. --- testing/tests/views/template_test.php | 26 ++++++++++++-- views/template.php | 52 +++++++++++++++------------ 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/testing/tests/views/template_test.php b/testing/tests/views/template_test.php index eb0d0dc..7183948 100644 --- a/testing/tests/views/template_test.php +++ b/testing/tests/views/template_test.php @@ -1,11 +1,11 @@ assertEquals('Some other', $template->Render()); } + + public function testBuiltinUrl() + { + $template = Template::NewWithData('builtin', 'Make a URL {%#url "/foo/bar"%}'); + $this->assertEquals('Make a URL ', $template->template()); + + $template = Template::NewWithData('builtin', 'Another {%# url "/foo" %} URL'); + $this->assertEquals('Another URL', $template->template()); + } + + public function testBuiltinImport() + { + $template = Template::NewWithData('test', 'Import {%# import \'_template\' %} tpl'); + $this->assertEquals( + 'Import tpl', + $template->template()); + + $template = Template::NewWithData('test', 'Import {%#import "tpl", array("foo" => "bar")%} import'); + $this->assertEquals( + 'Import "bar"))) ?> import', + $template->template()); + } } diff --git a/views/template.php b/views/template.php index 185c3bb..e67d272 100644 --- a/views/template.php +++ b/views/template.php @@ -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); } } -- 2.43.5