Skip to main content

Posts

Showing posts from 2017

Regarding Technology (a departure from WebDev)

I'm departing from WebDev for a minute to blog about technology. I'm as guilty of over-indulging in technology as anyone, especially given that my career demands a technology overload. Today at noon is the solar eclipse and that prompted me to think about the past and the future. I started to think about where the world stands and a song came up on my playlist that seemed perfect for my mood. 27 years ago, Bad Religion wrote a song about the current generation of kids. It would be prophetic if it weren't for the fact that the trend had already begun back then. In any event, it seems now is a good time to look back at the lyrics: 'Cause I'm a twenty-first century digital boy I don't know how to live but I got a lot of toys My daddy's a lazy middle-class intellectual My mommy's on Valium, so ineffectual What child these days does not have multiple devices to play with? Gaming systems, television, computers and tablets are in nearly every home. Kid...

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.

Debate: Identical Operator vs. Equality

Sometimes I call this "loose equality" (==) and "strong equality" (===) but regardless of what you call it, the inclusion of loose equality is one of the most maligned features of PHP.  Traditional programmers say using loose equality operators is a terrible practice - and for the most part, they are right.  In general, when you are checking if two variables are equal, you really mean to check if they are identical.  In other words, a string should not equate with an integer, even though they may *look* the same to a human. However, what traditional programmers tend to miss is that one of PHP's strengths is the ability to be portable.  Strong equality is one of the things that breaks portability.  Therefore, assuming you are validating input and working in the realm of positive integers (non-zero), loose equality is the only way to go! An example of this issue came up for me recently as I was moving my code from MySQL to Azure/MS-SQL in Laravel 5.3.  Fo...

Fun with CSS :before and :after!

Anyone that's ever done front-end work has probably worked with jQuery and its awesome DataTables plugin.  You've probably also run up against the limitations of DataTables, namely that it sorts columns based on ALL of the data you put inside the table cells.  In other words, extra characters inside a table cell can confuse the plugin and it may not sort exactly the way you want it to. For example: You'll notice that the balance and payment columns have a dollar sign and the rate column has a percent sign.  Though these characters may not affect the sorting, other characters could because it turns a numeric sort into a text sort.  While we may be okay here, best practice would dictate that we avoid adding any extra characters in our sortable data. In the image above, the dollar signs and percent signs aren't seen by the DOM nor by the DataTables plugin because I am using the CSS :before and :after tags to add them in as a style. Here's the HTML: ...