##Building general elements
The following builder methods can be used to generate general HTML elements like links, div
s, span
s, etc... All these methods return instances of Spatie\Html\Elements
. Of course all element methods are available on the returned instances.
-
function a(?string $href = '', ?string $text = ''): A
-
function button(?string $text = '', ?string $type = 'button'): Button
-
function div($contents = null): Div
-
function element(string $tag): Element
-
function mailto(string $email, string $text = ''): A
-
function span($contents = null): Span
-
function tel(?string $number, ?string $text = ''): A
##Working with a model
You can couple the HTML builder to a model using the model()
method. This way elements will be built with the given model and its attributes in mind. Generally speaking this simply means that form fields will automatically try to use the values from the corresponding model attributes.
$user = new User(['name' => 'Johnny']);
html()->model($user);
echo html()->text('name');
If you're working with forms you can use the modelForm()
and closeModelForm()
helper methods to generate forms coupled to a model.
The endModel()
method is used to stop using a given model to build elements.
##Form building
The HTML builder has some great methods for building entire forms. By default all fields in these forms will automatically use their corresponding session()->old()
values if available.
##form()
method
function form(string $method = 'POST', string $action = '')
The form()
method will return a Spatie\Html\Elements\Form
class. It will have the _token
and _method
fields as children by default.
Generally speaking you'll want to use this in combination with open()
and close()
to generate the opening and closing tags for the form in your template.
{{ html()->form('PUT', '/update-url')->open() }}
{{ html()->text('username') }}
{{ html()->form()->close() }}
<form method="POST" action="/update-url">
<input type="hidden" name="_token" value="ABC123">
<input type="hidden" name="_method" value="PUT">
<input type="text" name="username">
</form>
##Building a form with a model
To make things easier we've added the modelForm()
and closeModelForm()
methods to easily open and close a form that's coupled to a model. Under the hood the model()
and endModel()
methods are used.
{{ html()->modelForm($user, 'PUT', '/update-url')->open() }}
{{ html()->text('name') }}
{{ html()->email('email')}}
{{ html()->closeModelForm() }}
<form method="POST" action="/update-url">
<input type="hidden" name="_token" value="ABC123">
<input type="hidden" name="_method" value="PUT">
<input type="text" name="name" value="Johnny">
<input type="email" name="email" value="heres@johnny.com">
</form>
##Form-related elements
-
function checkbox(string $name = '', ?bool $checked = false, ?string $value = '1'): Input
-
function email(string $name = '', ?string $value = ''): Input
-
function input(?string $type = '', string $name = '', ?string $value = ''): Input
-
function fieldset($legend = null): Fieldset
-
function hidden(string $name = '', ?string $value = ''): Input
-
function label($contents = null, string $for = ''): Label
-
function legend($contents = null): Legend
-
function option(?string $text = '', ?string $value = '', $selected = false): Option
-
function password(string $name = ''): Input
-
function radio(string $name = '', ?bool $checked = false, ?string $value = ''): Input
-
function select(string $name = '', iterable $options = [], ?string $value = ''): Select
-
function submit(?string $text = ''): Button
-
function text(string $name = '', ?string $value = ''): Input
-
function textarea(string $name = '', ?string $value = ''): Textarea
-
function token(): Input