In part 11 we took a look at procedures and some of their basic uses. However there is more to procedures than meets the eye. One of the best kept secrets is the ability to pass variables through to them.
Think about our quotebox. The way we set it up, we assumed that our quotebox would always be aligned to the right. Of course we could change the code so that it always sits on the left instead, but what if we wanted the option to put it on the left or the right?
One way round this would be to simply create two procedures - one for left aligned boxes, one for those aligned right, but this is a bit of a waste of time and means we end up with more code to maintain.
The other solution is to keep one procedure and simply pass through a variable when we call the procedure where we tell it whether to align it left or right.
We'd then call the procedure like this:
<quotebox align="left">This is a left aligned
quotebox.</quotebox>
Using this, within our procedure we know have a variable called
align which is set to the value left.
Of course our procedure isn't actually using it yet, so until we change it, our quoteboxes will continue to act exactly as they did before - right aligned.
To put it to some use, we need to call our variable inside our quotebox procedure. As we've called our variable ALIGN when calling our procedure, we simply use that name within the procedure, thus we get:
<@DEF NAME="QUOTEBOX">
<table cellpadding="3" cellspacing="0" border="5" width="150"
align="${ALIGN}">
<tr><td width="150"><b>
</@DEF>
In this case, we're passing through a valid value for aligning tables, so we can slot it straight into our table code.
We can also extend the function further by adding a bit of checking to see if ALIGN exists when the procedure is called, and set a default if it is not. Using the methods we learnt in part 12, we can simply add a default by changing quotebox to something like this:
<@DEF NAME="QUOTEBOX">
<@IF NAME="ALIGN">
<@ELSE>
<@SET NAME="ALIGN" VALUE="RIGHT">
</@IF>
<table cellpadding="3" cellspacing="0" border="5" width="150"
align="${ALIGN}">
<tr><td width="150"><b>
</@DEF>
Here we've simply added a default to align the quotebox right if a value is not set.
With procedures, you're not limited to sending just one variable through - you can send as many as you like. As long as each one has a unique name, its not a problem. Which means that if you want to pass through several variables, like this:
<FUNCTIONANME NAME="spoo" TYPE="foo" SORTBY="goo"
TOTAL="moo">
Then you can. Just call them using <@GET NAME="name"> or
${TYPE} as you need to, and you're away!
Example files for this page are here.