<mvt:foreach>
#
The <mvt:foreach>
loop is a cornerstone of the Miva Merchant Template Language, allowing developers to iterate over arrays of data. This makes it an essential tool for dynamically rendering product listings, category details, customer data, and more.
By mastering <mvt:foreach>
, developers can efficiently process collections of items, creating dynamic and responsive templates.
Syntax#
The <mvt:foreach>
loop requires two key attributes:
iterator
: A placeholder variable that represents the current item in the array during each iteration.array
: Specifies the array of items to iterate over.
Syntax:#
<mvt:foreach iterator="iterator_variable" array="array_name">
<!-- Code to execute for each item in the array -->
</mvt:foreach>
Example:#
<mvt:foreach iterator="product" array="category_listing:products">
<p>Product Name: &mvt:product:name;</p>
<p>Price: &mvt:product:price;</p>
</mvt:foreach>
Attributes#
Attribute | Description |
---|---|
iterator |
Placeholder variable for the current item in the loop. |
array |
Specifies the array to iterate over. This is typically a data collection. |
Additional Tags for Loop Control#
<mvt:whilestop />
#
Halts the execution of the loop if a specified condition is met. This is useful for breaking out of a loop early.
<mvt:whilecontinue />
#
Skips the current iteration and continues with the next one if a specified condition is met.
Example: Controlling Loop Execution#
<mvt:foreach iterator="product" array="category_listing:products">
<mvt:if expr="product:price LT 10">
<mvt:whilecontinue />
</mvt:if>
<p>Product Name: &mvt:product:name;</p>
<p>Price: &mvt:product:price;</p>
</mvt:foreach>
In this example:
- Products with a price less than 10 are skipped, and the loop continues with the next product.
Examples#
Iterating Over Products#
<mvt:foreach iterator="product" array="category_listing:products">
<p>Product Name: &mvt:product:name;</p>
<p>Price: &mvt:product:price;</p>
</mvt:foreach>
Output: Displays a list of product names and their prices for the current category.
Nested Loops#
You can use nested <mvt:foreach>
loops to process related arrays, such as products and their reviews.
<mvt:foreach iterator="product" array="category_listing:products">
<p>Product Name: &mvt:product:name;</p>
<mvt:foreach iterator="review" array="product:reviews">
<p>Review: &mvt:review:content;</p>
</mvt:foreach>
</mvt:foreach>
Using Shortened Array References#
Miva often uses shorthand for array references, omitting prefixes like g.
or l.settings
. For example:
array="category_listing:products"
impliesl.settings:category_listing:products
.
Filtering Items in a Loop#
<mvt:foreach iterator="product" array="category_listing:products">
<mvt:if expr="product:stock EQ 0">
<mvt:whilecontinue />
</mvt:if>
<p>Product Name: &mvt:product:name;</p>
</mvt:foreach>
Explanation:
- Products that are out of stock (
product:stock EQ 0
) are skipped.
Best Practices#
-
Meaningful Iterator Names:
- Use descriptive names for iterators (e.g.,
product
,category
) to improve readability.
- Use descriptive names for iterators (e.g.,
-
Break Large Loops:
- Use
<mvt:whilestop />
and<mvt:whilecontinue />
to handle specific conditions efficiently.
- Use
-
Optimize Nested Loops:
- Minimize nesting to improve performance and readability.
-
Use Comments:
- Add comments to explain complex logic within loops.
Advanced Example: Conditional Rendering with Foreach#
<mvt:foreach iterator="product" array="category_listing:products">
<p>
<mvt:if expr="product:stock EQ 0">
Out of Stock: &mvt:product:name;
<mvt:else>
Available: &mvt:product:name;
</mvt:if>
</p>
</mvt:foreach>
Output:
- Displays “Out of Stock” or “Available” next to each product name based on stock availability.
Summary#
The <mvt:foreach>
loop is an indispensable tool for developers working with arrays in Miva templates. By understanding its syntax, attributes, and advanced control features, you can build dynamic and efficient templates tailored to your store’s needs.