mirror of
https://github.com/monstra-cms/monstra.git
synced 2025-08-08 06:06:53 +02:00
Form Helper: Custom Macros - added
This commit is contained in:
@@ -22,6 +22,14 @@
|
|||||||
class Form {
|
class Form {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The registered custom macros.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public static $macros = array();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Protected constructor since this is a static class.
|
* Protected constructor since this is a static class.
|
||||||
*
|
*
|
||||||
@@ -32,6 +40,38 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a custom macro.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
*
|
||||||
|
* // Registering a Form macro
|
||||||
|
* Form::macro('my_field', function() {
|
||||||
|
* return '<input type="text" name="my_field">';
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* // Calling a custom Form macro
|
||||||
|
* echo Form::my_field();
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* // Registering a Form macro with parameters
|
||||||
|
* Form::macro('my_field', function($value = '') {
|
||||||
|
* return '<input type="text" name="my_field" value="'.$value.'">';
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* // Calling a custom Form macro with parameters
|
||||||
|
* echo Form::my_field('Monstra');
|
||||||
|
*
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @param string $name Name
|
||||||
|
* @param Closure $macro Macro
|
||||||
|
*/
|
||||||
|
public static function macro($name, $macro) {
|
||||||
|
Form::$macros[$name] = $macro;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an opening HTML form tag.
|
* Create an opening HTML form tag.
|
||||||
*
|
*
|
||||||
@@ -360,4 +400,21 @@
|
|||||||
return '</form>';
|
return '</form>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamically handle calls to custom macros.
|
||||||
|
*
|
||||||
|
* @param string $method
|
||||||
|
* @param array $parameters
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function __callStatic($method, $parameters) {
|
||||||
|
|
||||||
|
if (isset(Form::$macros[$method])) {
|
||||||
|
return call_user_func_array(Form::$macros[$method], $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException("Method [$method] does not exist.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user