Add custom HTML macros to HtmlBuilder
This commit is contained in:
1 parent
3c28b890e2
commit
617756d715
1 file changed
+36
@@ -11,6 +11,13 @@ class HtmlBuilder {
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* The registered html macros.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $macros;
|
||||
|
||||
/**
|
||||
* Create a new HTML builder instance.
|
||||
*
|
||||
@@ -22,6 +29,18 @@ class HtmlBuilder {
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a custom HTML macro.
|
||||
*
|
||||
* @param string $name
|
||||
* @param callable $macro
|
||||
* @return void
|
||||
*/
|
||||
public function macro($name, $macro)
|
||||
{
|
||||
$this->macros[$name] = $macro;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an HTML string to entities.
|
||||
*
|
||||
@@ -275,4 +294,21 @@ class HtmlBuilder {
|
||||
if ( ! is_null($value)) return $key.'="'.e($value).'"';
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically handle calls to the html class.
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
if (isset($this->macros[$method]))
|
||||
{
|
||||
return call_user_func_array($this->macros[$method], $parameters);
|
||||
}
|
||||
|
||||
throw new \BadMethodCallException("Method {$method} does not exist.");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user