Taken from the tip of the week for May 30, 2008, this article discusses how an inline processes its parameters and how an array of common parameters can be used to make your code easier to maintain.
Inlines form the core of many pages written in Lasso. There are often many inlines on a page, nested or otherwise. This tip describes some techniques for dealing with inline parameters including re
An [Inline] tag accepts a series of parameters that determine the database action which is to be performed. The parameters are read by Lasso in the order they are specified in the tag.
Some parameters like
Other parameters like search terms, 'Field_Name'='Value', and sort parameters,
An inline will also accept an array of parameters. Each of the elements of the array are inserted into the array as if they had been typed out at the location of the array. This allows parameters for an inline to be assembled in a variable and then passed all at once to an inline.
Throughout a page or your entire site you'll notice that many inlines contains the same set of parameters. Often an entire site will use the same database, table, keyfield name, log
This is a typical inline which finds "John Doe" in the specified database. If we are working on an internal site we may find that most of our inlines reference the "myCompany" database. We may want to make sure that we default to
[Inline(-Log='none', -Database='myCompany', -Table='People', -KeyField='id', -MaxRecords='all', -Search', -Op='eq', 'first_name'='John', -Op='eq', 'last_name'='Doe')] ... [/Inline]
We can separate out the common parameters into an array and then pass that array as the first parameter to the inline. First, we create a variable $peopleInline which is defined as an array with the common parameters. We could define this variable in a shared include for use throughout a site or we could define it at the top of the page.
Note that the inline tag will only accept an array. It does not parse a string variable to look for parameters, so you can't build $peopleInline as a string.
[Var('peopleInline') = Array( -Log='none', -Database='myCompany', -Table='People', -KeyField='id', -MaxRecords='all')]
Next, in each inline that references the same database and table we insert the variable $peopleInline rather than specifying the
[Inline($peopleInline, -Search', -Op='eq', 'first_name'='John', -Op='eq', 'last_name'='Doe')] ... [/Inline]
If we make this change to multiple inlines and later find that we want to change the log
We can override parameters from the $peopleInline variable by specifying them explicitly within the inline after the $peopleInline variable. Since Lasso will use the last parameter of each type specified, our explicit
The following code will find only the first 10 matches for "John Doe".
[Inline($peopleInline, -Search', -MaxRecords=10, -Op='eq', 'first_name'='John', -Op='eq', 'last_name'='Doe')] ... [/Inline]
This override behavior allows us to set up a collection of defaults for all inlines on the page, but to use whatever specific values we need in each inline.
We can extend the technique described above to create dynamic inlines. This can be useful when we have logic that modifies not just the values within the inline, but also the number of parameters in the inline.
We create a variable $localInline as an array which is going to contain the parameters for our inline.
Var('localInline') = Array;
We then insert each of the parameters for the inline into this array. We might insert the
$localInline->Insert(-Log='none'); $localInline->Insert(-Database='myCompany'); $localInline->Insert(-Table='People'); $localInline->Insert(-KeyField='id'); $localInline->Insert(-MaxRecords='all'); $localInline->Insert(-Search='');
Name/Value parameters can be inserted in the same fashion. Here, the parameters to find "John Doe" are inserted into the inline. Note that the operator must be inserted first, then the name/value parameter.
$localInline->Insert(-Op='eq'); $localInline->Insert('first_name'='John'); $localInline->Insert(-Op='eq'); $localInline->Insert('last_name'='Doe');
Conditionals can be used to determine which parameters are added to the inline. For example, this code checks what field should be used to sort the inline and inserts different
If($sortfield >> 'first'); $localInline->Insert(-SortField='First_Name'); $localInline->Insert(-SortOrder='Ascending'); Else($sortfield >> 'last'); $localInline->Insert(-SortField='Last_Name'); $localInline->Insert(-SortOrder='Descending'); /If;
Now the entire dynamic variable $localInline is passed into the inline as a single array parameter. Since the variable contains the full specification for the inline action we do not need any other parameters in the inline itself.
[Inline($localInline)] ... [/Inline]
However, should we need to override some of the values in the dynamic variable $localInline we could do so by adding additional parameters within the [Inline] tag.
More information about the inline tags and database action parameters be found in the Language Guide or in the Lasso Reference.
Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.