Skip to main content

Posts

Showing posts from May, 2017

View Helpers - Custom Output Parser

I was reviewing some blade view code recently and decided it was too verbose. There were some long output statements that were duplicated throughout the view and that always signals a need to refactor or come up with a better method. So I wanted to share (or at least document) what I came up with here. First, the issue: fields stored as boolean "0/1" in the database must be output in "Yes/No" format. Plus, if the field is blank (or null), that must be reflected as well. Add to that the uncertainty of whether the production Microsoft Azure database will return a string or integer value for 1 or 0. This problem necessitates a blade statement like: {{ $object->property === null || $object->property === "" ? "" : $object->property == 1 ? 'Yes' : 'No' }} Like I said...verbose! One solution might be to create a Laravel accessor for the boolean field in the model. But that would mean that if I want the actual boolean ...

Props for Laravel's Blade Templating

While perhaps not as powerful as the Twig template engine, Laravel's Blade template engine does pretty much everything you're likely to need done in your view templates and it does it with more intuitive syntax. So I wanted to give it some props here that it very much deserves. To boil Blade down to its very basics, there are three essential construct types you need to know: {{ $var }} - output a variable {!! $html !!} - output some HTML (previously sanitized, we hope) @something - the equivalent of the if/else/foreach statements in PHP Basically, any PHP statement you need to use will work within the body of these three constructs. So here are some control flow statements in Blade: @foreach ($companies as $i => $company) @endforeach @if ($something === null) @endif And here are three versions of an echo statement that all work the same: {{ $name === null ? 'Unknown Name' : $name }} {{ $name or 'Unknown Name' }} {{ $name ...

Quit parsing people's names!

As an industry, could we please stop parsing people's names into parts? The only place that is useful is on government websites - and I'd even question it then. What happens when someone is simply named "Cher/Bono/Peter Jenkins Jr. II" or whatever? Why should a website dictate how many parts a name should have? Splitting names into pieces causes all kinds of difficulty when interfacing with other websites. There is no point to it. A person is identified by ALL of their name, not a part of it. Let's just drop the name splitting and start identifying users by their WHOLE name. It will be a lot easier for everyone and make for one less column in the database.