Skip to content

Working with Arrays#


Arrays in the Miva Template Language are versatile data structures used to store and manage collections of data. Unlike many programming languages, arrays in Miva are 1-indexed, meaning their first element is accessed using index 1 rather than 0. This distinction is critical for correctly working with arrays in Miva templates.

By understanding how to manipulate arrays, you can efficiently create dynamic and responsive storefronts, iterate over data collections, and manage complex data relationships.


Accessing Array Elements#

Array elements are accessed using square brackets [ ] followed by the index number. The index begins at 1 for the first element.

Syntax:#

array_name[index]:field

Example: Accessing an Element#

<mvt:assign name="l.people[1]:code" value="'P123'" />
<p>First Person Code: &mvt:people[1]:code;</p>

Output:

First Person Code: P123


Arrays vs. Structures#

Arrays:#

  • A collection of data indexed numerically.
  • Example: l.people[1]:name accesses the name field of the first element in the array.

Structures:#

  • A collection of named key-value pairs.
  • Example: l.settings:store:name accesses the name field of the store structure.

Arrays Within Structures:#

Arrays can exist as part of a structure, providing a hierarchical way to manage data.

Example:

&mvt:people[1]:info:name;

In this case:

  • people is an array.
  • Each element contains a structure named info, and the name field is accessed.

Common Array Operations#

Iterating Over Arrays#

Use <mvt:foreach> to iterate through an array and access its elements.

Example: Iterating Over an Array

<mvt:foreach iterator="person" array="l.people">
    <p>Person Name: &mvt:person:name;</p>
    <p>Person Code: &mvt:person:code;</p>
</mvt:foreach>


Counting Elements in an Array#

The miva_array_elements function returns the number of elements in an array.

Example: Counting Elements

<mvt:assign name="g.count" value="miva_array_elements(l.people)" />
<p>Total People: &mvt:global:count;</p>

Output:

Total People: 5


Adding Elements to an Array#

Although arrays are typically populated programmatically, you can manipulate arrays by assigning values.

Example: Adding an Element

<mvt:assign name="l.people[6]:name" value="'New Person'" />
<p>Added: &mvt:people[6]:name;</p>

Output:

Added: New Person


Filtering Arrays#

You can use conditional logic within a loop to filter specific items in an array.

Example: Filtering Items

<mvt:foreach iterator="person" array="l.people">
    <mvt:if expr="person:age GT 18">
        <p>Adult: &mvt:person:name;</p>
    </mvt:if>
</mvt:foreach>


Best Practices#

  1. Use Meaningful Names:

    • Name arrays and their fields clearly to improve code readability.
  2. 1-Indexed Awareness:

    • Remember that Miva arrays are 1-indexed, and adjust your loops and logic accordingly.
  3. Iterate Efficiently:

    • Use <mvt:foreach> for cleaner and more efficient iteration over arrays.
  4. Comment Complex Logic:

    • Document the purpose of nested arrays or complex relationships for better maintainability.

Advanced Example: Product List with Filtering#

This example demonstrates iterating through an array of products, filtering items based on stock availability, and displaying their details.

<mvt:foreach iterator="product" array="category_listing:products">
    <mvt:if expr="l.settings:product:stock GT 0">
        <p>Product Name: &mvt:product:name;</p>
        <p>Price: &mvt:product:price;</p>
        <p>Stock: &mvt:product:stock;</p>
    </mvt:if>
</mvt:foreach>

Output (if stock is available):

Product Name: Product A
Price: $10.00
Stock: 20

Product Name: Product B
Price: $15.00
Stock: 5


Summary#

Arrays are a powerful tool in the Miva Template Language, enabling developers to manage collections of data efficiently. By mastering array syntax, operations, and best practices, you can create dynamic, data-driven templates that enhance your store’s functionality.

Key Takeaways:#

  • Arrays in Miva are 1-indexed.
  • Use <mvt:foreach> for iteration.
  • Combine arrays with structures for hierarchical data management.