Best Practices

This page is a summary of best practices for coding Lasso sites. The page contains tips about how to code your site so that you can catch errors easily and create the most efficient code possible.

Conditionals

[If('literal' == $mystring)] ... [/if]  Best practice
[If('literal' = $mystring)] ... [/if]  Typo generates error

If the literal is on the right side of the variable and you accidentally mistype the operator as a single = equal sign then the variable will be assigned the value of the literal and no error will be generated. The conditional will be passed a value of Null or False.

[If($mystring == 'literal')] ... [/if]  Common practice
[If($mystring = 'literal')] ... [/if]  Typo modifies $mystring variable

Testing against multiples values

If you need to check a value against several other values, there is a better way than :

If: $mystring == 'literal' || $mystring == 'literal2' || $mystring == 'literal3' || $mystring == 'literal4';
 ...
/if;

You can group the allowed values in an array like this :

If: array('literal', 'literal2', litteral3') >> $mystring;
...
/if;

or even better, lets you use dynamic conditional tests :

var('allowed_values') = array('literal', 'literal2', litteral3');
If: $allowed_values >> $mystring;
...
/if;

Setting and returning variables

A variable is created using the [Variable]- or [Var]-tag:

Variable: 'VariableName' = 'SomeValue';
Var: 'VariableName' = 'SomeValue';

Once a variable is created it can be set and retrieved using that same tag, but it is better to use the shorthand metod, $VariableName. The reason being that in case of a typo, the first method creates a new variable which can lead to difficult debugging, whereas the shorthand method throws an error:

var: 'VariableName' = '';
...
$VariableName = 'Hello world!';  Best practice
Output: $Variable_Name;  Typo generates error


var: 'VariableName' = '';
...
var: 'VariableName' = 'Hello world!';
Output: (var: 'Variable_Name');  Typo creates a new variable