Miva Merchant, eCommerce, SEO, Wordpress, & general web development and consultation.

Scot's Scripts Blog

Passing a variable by reference in a Miva Script function is a neat trick when you might have to affect different kinds of data over time in the function but don't want to mess anything up.

Sometimes we don't know what data a Miva Script function in a module or script will need to affect in the future. Instead of creating more functions, passing a variable by reference can be quite useful.

Examples & How To's, Miva Scripting 101

If your mivascript app changes over time during upgrades and whatnot, sometimes a function might need to return more info than you've planned for. If it's a function that you're using all over the place, adding parameters or changing the return value can cause more problems than is worth it.

One way to solve this is to assign global variables, but it's generally frowned upon as far as coding goes because you don't always know what you might be messing up somewhere else by using global variables.

Passing a Variable by ReferenceOne of the best ways to get around this is to add a VAR style parameter - reference variable - to a function that can be used as a "catch all" for data.

Here is a function that takes a character input and returns it after changing it to lower case. Yes, I know there's a built in function for this, but it's a good way to demonstrate this.

<MvASSIGN NAME = "l.return" VALUE = "{ tolower(l.character) }" />
<MvFUNCRETURN VALUE = "{ l.return }" />

Super simple, right? It takes a character then returns it after changing it to lower case. 

However, what if you want to know if it's a letter A? If you are calling this function in your mivascript more than once, you don't want to change the return value because then you'll need to change it in other places in your code which is a good way to introduce bugs.

Instead, let's pass a variable by reference using a VAR style parameter in our function and use it to let us know if the character is a vowel.

<MvFUNCTION NAME = "lowercase" PARAMETERS = "character,info var" STANDARDOUTPUTLEVEL="">
<MvASSIGN NAME = "l.return" VALUE = "{ tolower(l.character) }" />
<MvIF EXPR = "{ 'A' CIN l.character }">
<MvASSIGN NAME = "l.info:a" VALUE = "{ 'yes' }" /> </MvIF>
<MvFUNCRETURN VALUE = "{ l.return }" />

Now when you call the function like this:

<MvASSIGN NAME = "l.old_char" VALUE = "{ 'A' }" />
<MvASSIGN NAME = "l.new_char" VALUE = "{ lowercase(l.old_char,l.info) }" />

You can then evaluate the l.info:a structure to see the data that was assigned, in this case the string yes.

<MvEVAL EXPR = "{ l.info:a }" />

The beauty of this method is that you can keep on adding more data to the l.info structure without changing anything else in your miva script module or app which saves you from any additional chance of introducing bugs.

Not all functions need something like this, but if you have any itch that a function you are creating may need to return more information but you're not sure what it is, using this method can save you a lot of headaches.

(0/5, vote) my rating:
overall rating:

Please log in to leave a comment.

The blog posts on Scot's Scripts are served by Scot's Blogger, a Miva Merchant blogging module that makes it easy to create a Wordpress style blog, rich content, and extra pages in your Miva Merchant store.