1
0
mirror of https://github.com/monstra-cms/monstra.git synced 2025-08-07 05:37:03 +02:00

Html Helper: Custom Macros - added

This commit is contained in:
Sergey Romaneko
2012-10-04 15:29:49 +03:00
parent e78e410f71
commit 6f2beb78a7

View File

@@ -46,6 +46,38 @@
}
/**
* Registers a custom macro.
*
* <code>
*
* // Registering a Htmlk macro
* Html::macro('my_element', function() {
* return '<element id="monstra">';
* });
*
* // Calling a custom Html macro
* echo Html::my_element();
*
*
* // Registering a Html macro with parameters
* Html::macro('my_element', function(id = '') {
* return '<element id="'.$id.'">';
* });
*
* // Calling a custom Html macro with parameters
* echo Html::my_element('monstra');
*
* </code>
*
* @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
* should be passed through this method to prevent XSS injections.
@@ -275,4 +307,21 @@
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.");
}
}