From 92883cac00f1b91b23d3356320f29735f7b29d5b Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 2 Jun 2013 17:02:22 -0400 Subject: [PATCH] Write the initial builtin Template macro functions implementation. --- views/template.php | 67 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 7 deletions(-) diff --git a/views/template.php b/views/template.php index 71cc98c..185c3bb 100644 --- a/views/template.php +++ b/views/template.php @@ -1,11 +1,11 @@ data; - $_vars = array_merge($this->vars, $vars); - $render = function () use ($_template, $_vars) { - extract($_vars); - eval('?>' . $_template . '<' . '?'); + $__template_data = $this->data; + $__template_vars = array_merge($this->vars, $vars); + $render = function () use ($__template_data, $__template_vars) { + extract($__template_vars); + eval('?>' . $__template_data . '<' . '?'); }; ob_start(); @@ -247,6 +258,7 @@ class Template // futher thransform the statement. switch ($macro[0]) { case '=': return $this->_ProcessInterpolation(substr($macro, 1)); + case '#': return $this->_ProcessBuiltin(substr($macro, 1)); default: return $macro; } } @@ -280,6 +292,47 @@ class Template $expression = trim(substr($macro, 0, $formatter_pos)); return 'echo hoplite\\base\\filter\\' . $function . '(' . $expression . ')'; } + + protected function _ProcessBuiltin($macro) + { + $macro = trim($macro); + $function_pos = strpos($macro, ' '); + if ($function_pos === FALSE) + throw new TemplateException('No macro function specified'); + + $function = substr($macro, 0, $function_pos); + $args = substr($macro, $function_pos); + switch ($function) { + case 'url': return $this->_Builtin_url($args); + case 'import': return $this->_Builtin_import($args); + default: + throw new TemplateException("Invalid macro function '$function'"); + } + } + + protected function _Builtin_url($args) + { + return "echo hoplite\\views\\MakeURL($args)"; + } + + protected function _Builtin_import($args) + { + $template = $args; + $vars = ''; + + $template_pos = strpos($args, ','); + if ($template_pos !== FALSE) { + $template = substr($args, 0, $template_pos); + $vars = substr($args, $template_pos + 1); + } + + if ($vars) + $vars = "array_merge(\$__template_vars, $vars)"; + else + $vars = '$__template_vars'; + + return "hoplite\\views\\Inject($template, $vars)"; + } } class TemplateException extends \Exception {} -- 2.22.5