diff --git a/code/site/components/com_pages/template/default.php b/code/site/components/com_pages/template/default.php index 670ad5844..13b4e7aa6 100644 --- a/code/site/components/com_pages/template/default.php +++ b/code/site/components/com_pages/template/default.php @@ -108,8 +108,8 @@ public function invokeHelper($identifier, ...$params) if(!empty($parts)) { $identifier = implode('.', $parts).'.template.helper.'.$identifier; } - - $helper = $this->createHelper($identifier, $params); + + $helper = $this->createHelper($identifier); //Call the helper function if (!is_callable(array($helper, $function))) { @@ -121,7 +121,13 @@ public function invokeHelper($identifier, ...$params) $params = array_merge($this->getParameters()->toArray(), $params); } - return $helper->$function(...$params); + if(is_numeric(key($params))) { + $result = $helper->$function(...$params); + } else { + $result = $helper->$function($params); + } + + return $result; } public function createHelper($helper, $config = array()) diff --git a/code/site/components/com_pages/template/helper/snippet.php b/code/site/components/com_pages/template/helper/snippet.php new file mode 100644 index 000000000..8effca8d4 --- /dev/null +++ b/code/site/components/com_pages/template/helper/snippet.php @@ -0,0 +1,59 @@ +define($name, $snippet); + } else { + $result = $this->expand($name, $snippet); + } + + return $result; + } + + public function define(string $name, string $snippet, $overwrite = false) + { + $result = false; + + if(!isset($this->__snippets[$name]) || $overwrite) + { + $this->__snippets[$name] = $snippet; + $result = true; + } + + return $result; + } + + public function expand(string $name, array $variables = array()) + { + $result = false; + + if(isset($this->__snippets[$name])) + { + $snippet = $this->__snippets[$name]; + + //Use the php template engine to evaluate + $str = "getObject('template.engine.factory') + ->createEngine('php') + ->loadString($str) + ->render($variables); + + //Find single whitespace before " or before > in html tags and remove it + preg_match_all('#<\s*\w.*?>#', $result, $tags); + + foreach($tags as $tag) { + $result = str_replace($tag, str_replace(array(' >', ' "'), array('>', '"'), $tag), $result); + } + } + else throw new RuntimeException('Snippet: '.$name.' does not exist'); + + return $result; + } +} +