Skip to content

<mvt:foreach>#


The foreach loop is a vital programming construct within Miva Merchant’s template language, commonly employed for iterative processing of various entities on a page. These entities could range from products and categories to customer data and beyond.

Syntax#

The syntax for a foreach loop comprises the following elements:

<mvt:foreach iterator="product" array="category_listing:products">
    Code to execute for each product in the array
</mvt:foreach>
Attribute Description
iterator The iterator serves as a placeholder name for each iteration of the loop.
array This attribute specifies the array of items to be iterated over.

Additional Tags#

While Loops in Miva Merchant also support two additional tags to control loop execution:

<mvt:whilestop />#

This tag halts the execution of the loop if a specified condition is met, allowing program flow to continue beyond the loop.

<mvt:whilecontinue />#

Conversely, while continue returns program flow to the beginning of the loop if a condition is met, enabling conditional iteration within the loop.

Examples#

Despite the absence of explicit g. or l.settings prefixes, the array reference within the foreach loop follows a convention that implies l.settings:category_listing:products. This shorthand notation is a common practice in Miva’s template language.

For instance, consider iterating over products within a category to output their names and prices:

<mvt:foreach iterator="product" array="category_listing:products">
    Product Name: &mvt:product:name; <br>
    Product Price: &mvt:product:price; <br>
</mvt:foreach>

Summary#

Foreach loops are extensively utilized throughout Miva Merchant’s template language for various tasks, from rendering product listings to handling payment and shipping options. Understanding the foreach loop syntax and its application is essential for effective template development within Miva Merchant.