Let’s take a look at the Foreach loop within Miva Merchant’s template language. A foreach loop is a commonly used programming structure to loop through things on a page. Things can be products, categories, it could be customers, really, it could be anything. The syntax for a foreach loop is mvt:foreach and then there’s two attributes to the tag. The first one is the iterator. This is just a name that you give to each iteration of the loop. So here we’re calling it product. The second one is the array. So this is the array of things that we’re looping through. So this example is from the category page, CTGY. Here the array for category_listing:products will always contain the products assigned to whatever category you’re on. Now you notice that the array doesn’t contain a g. or a l.settings like you’d expect. This is just a shorthand convention that’s used commonly with foreach loops. Even though it’s not explicitly there, this is referring to l.settings:category_listing:products. So if you’re familiar with a foreach loop with any other programming language, this works exactly the same. It’s going to loop through each of the items within the array. In this use case, my array contains information about products assigned to this category. So, I want to output the product name and the product price for each of the products. Let’s take this code over to Miva and execute it to see what happens.
So here, I’m on the CTGY page, which is the Category Display page. And within this is the Category Product List Layout controls the layout of the category listing. If l look here at this code, you’ll see that here is the foreach loop. I have iterator for product and here’s my array for category listing in products. I’m going to go down here even further to Category Header and Footer and I’ll put my code in here and hit the “Update” button. Now if I go back to the frontend and I look at a category page, here’s a category page without my code. That category listing is controlling the layout of each of the products that are assigned to this category. If I refresh this to add my code my code was in the header, so it prints the product name and the product price for each of the products that are assigned to this category.
So let’s take a look at a couple other examples. So here, this is a foreach loop that was pulled from OSEL which controls the payment selection and the shipping selection. Here, we have an arrayment of payment methods. Again, this is referencing l.settings:paymentmethods, even though it’s not explicitly there. The iterator can be anything you want. It’s just a name you’re giving to each iteration of the loop. So whatever name you give to iterator, you’re going to reference here when you output the variables to the page. So this will loop through all payment methods that are available within my store and I’m outputting the payment method code.
The same thing for shipping methods. We have an array of shipping methods, all available shipping methods, the iterator again here is called “method” but I could call this whatever I want and I’m outputting the shipping method name. You’ll see foreach loops used everywhere throughout Miva Merchant. Anytime you’re looping through anything, most likely it’s a foreach loop doing it and will follow this syntax.