24/7 Support: 800.608.6482


Developer Videos

Videos | Assign and Eval

Version 9

Video Transcript

One of the most important concepts in mastering the Miva Merchant Temple language is understanding how to create and output variables to the page. There are two core functions that do this within the Miva Merchant Template language. The first one is called

Now the value is the expression to evaluate and save into the variable. So here, I have a string for ‘hello world.’ That value is being saved back into g.variable. Now you notice I have ‘hello world’ wrapped in single quotes. This identifies it as a string. If I didn't have the single quotes Miva would try to evaluate that as a variable which doesn't exist. The third piece of this is the output to the page. Anytime you want to output a variable to the page, you’re always going to use &mvt: and then the variable name. Here it’s :global because I created a global variable and then my variable name is variable. Now this is using Miva Merchant’s structures which I’ll go into more in the next video. For now just know that this whole variable here will output whatever you have in this expression. mvt:eval is very similar to mvt:assign, except it doesn't create a variable. What it does is it evaluates the expression that you pass in and will output that to the page.

Let's jump over to Miva and see how we use these two tags in action. Here I am over in the Miva Merchant Admin. Let's go into the Storefront page so we can add some code. So here is the same code I was working with. What we should expect to happen is the value of hello world will be output one time here, followed by a break, followed by that same hello world output a second time using mvt:eval. Let’s update this and see what happens. If I jump back to the front of the web site, here’s my Miva Merchant Storefront, hit “Refresh” and you’ll see I have hello world hello world. However, one really nice thing with mvt:assign is, because it's always evaluating this as an expression, you can mix variables with string, concatenation, or even math operators. Let's do some more samples.

So let's jump back to the Miva Merchant Admin and see what else we can do with this. Because this value is always being evaluated, you could do a lot of powerful things like output variables, concatenate variables, or variables with strings or even math operations. Let’s take a look and try another example. Instead of just outputting a string hello world, I’m going to do a string mixed with a math operator. I’m going to say “10 + 10 = and then the concatenation operator is the $ sign. I’m actually going to do 10 + 10. What this should evaluate is the string 10 + 10 equals concatenated with the expression 10+10 which should give 20 and that should output one time here because we’re outputting the variable and a second time here using mvt:eval. Let’s update this and see what happens. I’ll go back to the front end and hit “Refresh” to see what we get. There we go, 10 + 10 = 20.

Let’s take a look at one more example. One of the real powerful parts of mvt:assign and mvt:eval is they give you full access to all of the built-in functions of the Miva Merchant engine. Now, we’re going to go more indepth in one of the next videos, but let me give you an example. Say I want to calculate the length of a string. So I’m going to come in here and create a new variable called g.length and the value is going to use a built in Miva function called “len.” So here I’m going to do the length of my name. Of course it's wrapped in single quotes because it's a string. So that function will return the length of whatever's passed into it and that could be a string, or it could also be a variable. So the value of g.length should contain the number of characters, including the space, that are in my name. So if I output this to the page and the outputs always terminated by a semicolon, hit “Update” come back to the Storefront page and hit “Refresh.” So here we have thirteen, which is the number of characters in my first and last name, including the space. Now this is just one example, there are hundreds of built-in functions that we have access to which we’ll dive into deeper in future videos, but for now it's important to note that mvt:assign and mvt:eval are a cornerstone component of the Miva Merchant Template Language. Understanding how they work and the flexibility will take you a long way indeveloping Miva Merchant stores.

Example Code

Reference Documentation

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

This website uses cookies to identify visitors, track visitors to our website, store login session information and to remember your user preferences. By continuing to use this site you agree to our use of cookies. Learn More.

This website uses cookies. By continuing to use this site you agree to our use of cookies. Learn More.


Copyright © 1997 – 2024 Miva®, Miva Merchant®, MivaPay®, MivaCon®, Camp Miva®, Miva Connect®, Miva, Inc. All Rights Reserved.