.columns Less Mixin

Like most frontend developers, I work with columns on an hourly basis.  I don’t really buy in to using frameworks, I don’t have time to learn their intricacies and limitations and frankly, I like knowing how my own code works.

So I wrote my own very simple Less Mixin:

.columns(@cols, @hspace, @padding, @fitinto){
  margin-left: @hspace * -1;
  width: @fitinto + @hspace;
  float: left;
  div.column{
    width: floor(((@fitinto - (@hspace * (@cols - 1))) / @cols)) - (@padding*2);
    padding-left: @padding;
    padding-right: @padding;
    margin-left: @hspace;
    float: left;
  }
}
									

Usage example:

#page{
  .columns(3, 20px, 10px, 500px);
}
									

Modify the default date format in Lemonstand Backend

Having to read American formatted dates in the Lemonstand backend is annoying enough for the developer, but we’re somewhat used to it. Clients on the other hand, are generally not happy to put up with figuring out what the actual date is.

After some googling I discovered that changing the date format for the Lemonstand backend is actually pretty simple!Add the following to the config/config.php file:

$CONFIG['LANGUAGE'] = 'en_gb';

now duplicate the phproad/modules/phpr/localization/dates.en_ca.res file and rename it dates.en_gb.res. You can open this and customise it as you see fit, but it works just fine as it is.

That’s it, all done.

Reading GET variables with JavaScript

I found a great piece of code whilst trying to figure out how to read get variables with javascript.


function $_GET(q,s) {
s = s ? s : window.location.search;
var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i');
return (s=s.replace(/^?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined;
}

Credit goes to Online Aspect

Handy code: ifelse()

As with most web developers about half of my code is collecting information via forms (the other half is presenting that information). When a form is submitted there are a variety of variables that I might want to display back to the user in the form elements. The code snippet below simplifies how I handle which variable to display.

Previously my code for an input field would have used a lot of nested inline if statements to determine which variable to fill the field with. It might have looked something like this:

echo '<input type="text" name="email" value="' . (($_POST['email']) ? $_POST['email'] : (($user['email']) ? $user['email'] : 'Email') . '" />';

The code above would show the posted email address first, and if that’s not present defer to one from the database. If neither of those are present then it would display a label, “Email” in this case.

That’s a nightmare to manage, and it looks nasty. So I wrote a function called postOrNot($post, $other) which simplified the inline if statement a touch. If the first variable was present, it was displayed, if not then it would display the 2nd.

With the introduction of “func_get_args()” to the function it can handle any number of arguments, falling over to each one in turn, and finally returning false if none are present.

function ifelse(){
if(func_num_args() < 2) trigger_error("ifelse() requires 2 or more arguements", E_USER_ERROR);
else {
$args = func_get_args();
foreach($args as $arg){
if($arg) return $arg;
}
return false;
}
}

It's a function that I use pretty much every day and saves me lines and lines of code.