From 6f2beb78a7be0ed3979923548f7a43c36b6c5c7a Mon Sep 17 00:00:00 2001 From: Sergey Romaneko Date: Thu, 4 Oct 2012 15:29:49 +0300 Subject: [PATCH] Html Helper: Custom Macros - added --- monstra/helpers/html.php | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/monstra/helpers/html.php b/monstra/helpers/html.php index 9796d5c..82ecfc0 100644 --- a/monstra/helpers/html.php +++ b/monstra/helpers/html.php @@ -45,6 +45,38 @@ // Nothing here } + + /** + * Registers a custom macro. + * + * + * + * // Registering a Htmlk macro + * Html::macro('my_element', function() { + * return ''; + * }); + * + * // Calling a custom Html macro + * echo Html::my_element(); + * + * + * // Registering a Html macro with parameters + * Html::macro('my_element', function(id = '') { + * return ''; + * }); + * + * // Calling a custom Html macro with parameters + * echo Html::my_element('monstra'); + * + * + * + * @param string $name Name + * @param Closure $macro Macro + */ + public static function macro($name, $macro) { + Html::$macros[$name] = $macro; + } + /** * Convert special characters to HTML entities. All untrusted content @@ -274,5 +306,22 @@ public static function toText($str) { return htmlspecialchars($str, ENT_QUOTES, 'utf-8'); } + + + /** + * Dynamically handle calls to custom macros. + * + * @param string $method + * @param array $parameters + * @return mixed + */ + public static function __callStatic($method, $parameters) { + + if (isset(Html::$macros[$method])) { + return call_user_func_array(Html::$macros[$method], $parameters); + } + + throw new RuntimeException("Method [$method] does not exist."); + } } \ No newline at end of file