<?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
$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());
+ }
}
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.
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);
protected function _Builtin_url($args)
{
- return "echo hoplite\\views\\MakeURL($args)";
+ return "hoplite\\views\\TemplateBuiltins::MakeURL($args)";
}
protected function _Builtin_import($args)
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);
}
}