24/7 Support: 800.608.6482

Get Started

Videos | Global vs Local Variables

ver. 9.0 and later

Video Transcript

Today we’re going to talk about the difference between global and local variables in Miva Merchant. There are two types of variables you can use in Miva’s template language. A global variable and a local variable. Now, we’re going to use our mvt assign tags that we learned in the last video to create some variables. It’s really easy to tell the difference between a global variable and a local variable. A global variable will always be prefixed with g. And then whatever name the variable is. So here I have a variable called g.name with a value, which is a string because it’s in single quotes, of Brennan. Now, if I want to output that value to the page, you’re always going to reference it using this global keyword. So it’s &mvt:global:name; this will output whatever value I put in the single quotes.

Now let’s say you wanted to use your variable in a conditional statement, like an if l statement. To do that, you always use the g. and then whatever the variable name is. You wouldn’t use the &mvt:global:name here. This only outputs the variable to the screen. You can’t do any conditional statements against it. Let’s take a look at what that looks like. So here’s an example of an “if statement” in Miva Merchant. Now, we’re going to dive into this in more detail in later videos. For now, let’s take a look at the expression here. We’re saying, if g.name is equal to the string value of Brennan and because it is assigned to the string value of Brennan here, we should expect this to output the value as Brennan.

Global variables are exactly as they sound. They are global in scope. They are available anywhere on the page. If you define the global variable at the top of the page you can use it, reference it and change it anywhere else on the page.

Now let’s take a look at local variables. So here we have the same mvt:assign tag so we’re creating the variable and the variable is l.name. So l stands for local. The variable name is name, and the value again is Brennan. Now I have a variable called g.name and l.name and these are two completely different variables, completely different in scope. Looking at the convention of global, you’d expect a local value to be displayed here, but that’s not how Miva works. Instead, for any local variables you simply put &mvt: and then the variable name. However, this will not output any value to the screen. This is really the big difference between local variables and global variables when working with the Miva template language. A global variable is available anywhere and can be output anywhere on the page. However a local variable can not be output anywhere on the page. Local variables can only be used in conditional statements like if l.name equals Brennan like we did here above. This would output correctly. Local variables are also local in scope from where they were defined. For example, say you defined a local variable within a foreach loop or a while loop. That variable will only be accessible within that loop. Whereas if you created a global variable within that loop, it would be available within that loop, but also anywhere else outside on the same page.

A common question we see is, “How do I know that a variable I’m working with is global or local?” It’s really easy to tell. A global variable is always prefixed with g. and a local variable is always prefixed with l. Or, if you’re looking at the output, a global variable will always have this global keyword when it’s being output to the page.

Now, the last thing I want to talk about in this video is the l.settings structure and we’re going to dive into this a lot more in the next video. However, I want to introduce it to you here. So a few seconds ago I said a local variables can not be output to the page. Well, that’s actually not true. There’s a special local variable called l.settings which acts very similarly to a global variable where it’s global in scope for that page but it’s still a local variable. Miva uses l.settings quite a bit throughout all its page templates. You’ll see it referenced everywhere in Miva’s template language. L.settings:something, l.settings is very common and you’ll work with it quite a bit. So think of l.settings as a special local variable where you can assign different values into this l.settings and it will act similar to a global variable where you can output anything to the page. So here’s a quick example, so I have l.settings:name. So this whole thing is a variable that I’m creating and then I’m giving it the value of l.name which is what we assigned up here which should contain the value “Brennan.” So here in this case I can use &mvt:name to output this to the page. So while this may be confusing at first, l.settings will become second nature to you because it’s used so much throughout the Miva Merchant template language.

One other thing to note when working with global and local variables in Miva Merchant, you don’t have to have to declare a variable as a string or a float, you just declare the variable and it can take on any of those values. Let’s look at a few examples. So here I’m going to create a new variable using mvt:assign. You’ll notice I have some snippets I’m using for sublime text that make this much easier. These are available to download as part of this training series. I’m going to create a variable called g.test and here I can give it the value of string, of another variable, of an expression of two variables added together, of a number, I can do almost anything I want without having to declare ahead of time that this variable is a string or is a number. So, I can do this where the value is 5, I can do an expression where you add two numbers together like 5+5 so this would evaluate to 10, or I could do a string and strings are always wrapped in single quotes, or I can give it a value of another variable.



Example Code

Reference Documentation




comments powered by Disqus

Looking for Developer Docs?

We have a whole section for that, including: Developer Training Series, Template Language docs, Module Development tutorials and much, much more.

Head to the Developer Section

Miva believes that all online businesses should have access to a scalable ecommerce platform that can meet their unique business requirements. Miva offers PCI compliant ecommerce, hosting, and custom website design and development solutions. Miva customers have processed over $100 billion in online sales since 1997.

Copyright © 2016 Miva, Inc - All Rights Reserved   Privacy Policy | Store Policy

Links
Contact Us
Receive Tips & Updates

Copyright © 2017 Miva, Inc - All Rights Reserved

Back To The Top