Laravel 4: DRY Up Your Views With Form & HTML Macros

This is just a quick post to give a couple of examples where form/HTML macros are useful, and to advocate their usage in general.

Reusable Components

In my larger projects I like to create form and HTML macros to DRY up repetitive view-related tasks and also make the views more clean and concise. Here are a couple of examples:

Here we have created a couple of reusable components: a Select Province drop-down and a Select Country drop-down. The great thing about this is that the logic for populating the dropdowns is contained to one method (including caching!), and usage throughout the app is extremely simple:

Wrapping jQuery

I also like to use form/HTML macros to wrap elements that have added jQuery functionality. For example, I may have a city field using typeahead.js to automatically suggest cities populated from the database as the user starts typing. The form macro literally acts as a wrapper around a text field and applies the typeahead class to it, and then jQuery does this rest. For example:

I prefer this because the logic is contained to one method, and if we ever need to modify the markup for all typeahead search fields in the future we can do this from a centralized location.

These are all form macro examples, but the concepts are the same for HTML macros!

Stay tuned for an upcoming post where I’ll show how to move this global code into object oriented classes loaded by the Laravel core via a service provider.