xwero / laravel-twig-html
HTML builder twig extension
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=8.2
- spatie/laravel-html: ^3.2
- twig/twig: ^3.7
Requires (Dev)
- illuminate/http: ^10.19
- pestphp/pest: ^2.15
- spatie/ignition: ^1.9
README
This is a html builder twig extension for people who use Twig in Laravel.
The Laravel twig integration package comes with a formbuilder extension that uses LaravelCollective/html. This package is abandoned, with the suggestion to use spatie/html.
My package uses the suggested library, maybe in a quirky way. But I saw the fluent API of the library, and I wanted the same in twig.
The out of the box twig integration code: {{ form_open({"route":"home"}) }}
Laravel twig html code: {{ h()|e_form|form_route("home")|em_open }}
As you can see this package involves more typing. I will add some shortcuts for frequently used combinations.
Usage
The function html()
can not be rendered on its own. It is the start value to create the tags. The short version is h()
.
The element filters are prefixed with e_
. And the element method filters are prefixed with em_
.
The difference between the two is that the element filters are standalone, while the element method filters need an element for the filter to work.
When you add a string as an argument it will be used as the first argument of the underlying method. You can add the arguments as a list, but then the amount of items has to be the same as arguments of the underlying method. To substitute the default arguments you can use named keys that match the default argument keys.
The defaults are pragmatic but when a combination of arguments can't result in usable html tag, the previous value will be returned. For the element filters this means an error object can be returned. For the element method filters there will be no changes.
Developing
You can run tests with ./vendor/bin/pest
.
Unfortunately the form_route filter uses the route method without a slash so it looks for it in the Form class. Just add the slash, and all tests will run.
Other Laravel behaviours, like form_token, can not be tested without adding it to a Laravel project.