So far, we’ve worked with two variable types; local variables which are referenced l. and global variables which are referenced g. There is actually a third variable type called “System Variables.” System Variables are referenced s. These are a special type of variable that are initialized by the engine when the script first starts executing so the engine handles all the heavy lifting. System Variables are things like cgi environment variables, http header variables, and Miva specific variables that are available to your script in the page template language.
There are two types of System Variables. There’s Dynamic and there’s static. The big difference between the two is that Dynamic Variables are reset every time they are used in an expression. Static Variables get set one time when the script initializes and they stay the same throughout the rest of the script. A good example of Dynamic Variables is time. All the time functions that are available to Miva Script in the Miva engine have both static and dynamic versions., so you can choose what version you want to work with depending on your needs. However, most System Variables are static. They get set once at the beginning of the script and they don’t change throughout the rest of the execution. Another great part about System Variables is they give you access to all the http headers that you’re familiar working with. When an http request comes into the server, either via get for form post, there are headers sent with every request. These include things like the http_referer, the user agent, and cookie data. All these headers automatically get converted to System Variables. The header name gets prefixed with http_ and then the variable name. If there’s a dash in the name it gets converted to an underscore. One kind of tricky thing about System Variables is that they cannot be directly output to the page. However, you can output them using mvt:eval and mvt:assign. They also can be used in conditional statements like if .s:remote_address which is their clients ip address, equals something. I’ll show you an example of that shortly.
Let’s jump over to Mivascript.com and take a look a good example of System Variables. So over in Mivascript.com on the left navigation there’s a link for System Variables. So this tells you a little bit about what System Variables are and how to use them. There’s a bunch of dynamic time related functions here. There’s also static versions of those. There’s a list of a lot of common System Variables. Some you may use a bit are the http_referer which is the referring url which the current page was accessed from, the http_user_agent, this is very common when you’re dealing with mobile and asking for user agent strings. The query_string, remote_addr which is the clients ip address, information about the server, and executing script can also be found here. Now, this is not a complete list. There are actually more system variables than are outlined here, but these are a lot of the common ones. Now you may be asking, how do I get the full list and see what’s available. In the next video we are going to talk about Token list, which I’ve briefly mentioned in our previous video.
Token List is a built in Miva Merchant tool that allows you to see all local, global and system variables right from the admin. Token list is going to be a go to resource when trying to identify what variables are available on what pages. Let’s jump back to our text editor and take a look at some examples. So here I have two system variables; s.remote_addr which is the ip address of the client and s.http_referer which is the referring page URL. So as we’ve seen with global variables, when you have a g.variable name when you output it to the page you reference &mvt:global and then the variable name., so you’d expect the same syntax to work. While the syntax exists, it doesn’t actually output anything to the page. In order to output a system variable directly to the page, you need to use mvt:eval or mvt:assign. So let me grab this code here and I’m going to bring it over to Miva and see what happens.
In our Miva Merchant 9 Admin, I’m going to search for our SFNT page and paste in the code. Then I’ll jump back to the front end and hit refresh and you’ll see it does not print because it’s pointing to nothing, but right below it, it actually has the ip address and this is our mvt:eval statement that outputs the ip address.
Jumping back to the text editor, let’s take a look at the second example. So here we have s.http_referrer and this is the referring page URL to the page you came from to get to the page you’re at now. And here, instead of using mvt:eval we’re using mvt:assign. I’m going to assign the system variable to a global variable, we’ll call it g.referer and then I’m going to output the global variable to the page. We’ll copy and replace the other code and hit update. What we see here is the referrer looks to be a category page. So it’s the category page of SVCS. If I wanted to change that I can click on “Test Category” and then click back to Home on our Storefront page, and you’ll see our page referrer changed. Now the category code is test. So this refer variable will always reference the previous URL that you came from.
While these are only two examples of using System Variables, there are hundreds of uses for them and they are a very powerful feature of Miva Merchant. Understanding what variables are available and how to use them will take you a long way in developing Miva Merchant stores.