mirror of
https://github.com/monstra-cms/monstra.git
synced 2025-08-06 21:26:58 +02:00
Html Helper: Custom Macros - added
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user