Skip to content

POS1 Counter#


POS1 is a built-in loop counter in Miva Merchant’s template language. It provides the current iteration index within a loop and is available in both <mvt:foreach> and <mvt:while> loops. Starting from 1, POS1 increments by one with each iteration.

In nested loops, additional counters like POS2, POS3, and so on are automatically introduced, reflecting the level of nesting.


Syntax#

Using POS1 in a Loop:#

<mvt:foreach iterator="item" array="array_name">
    <!-- Code to execute for each iteration -->
    <mvt:if expr="POS1 EQ 1">
        <!-- This block executes for the first iteration -->
    </mvt:if>
</mvt:foreach>

Examples#

Basic Iteration#

Display the iteration index for each item in the loop using POS1:

<mvt:foreach iterator="product" array="category_listing:products">
    Iteration: &mvt:POS1; - Product Name: &mvt:product:name;<br>
</mvt:foreach>

Output:

Iteration: 1 - Product Name: Product A
Iteration: 2 - Product Name: Product B
Iteration: 3 - Product Name: Product C
...


Targeting a Specific Iteration#

Perform an action only for a specific iteration.

<mvt:foreach iterator="product" array="category_listing:products">
    <mvt:if expr="POS1 EQ 5">
        <p>This is the 5th product: &mvt:product:name;</p>
    </mvt:if>
</mvt:foreach>

Output (only on the 5th iteration):

This is the 5th product: Product E


Highlighting Multiples#

Apply logic for every Nth iteration (e.g., multiples of 4).

<mvt:foreach iterator="product" array="category_listing:products">
    <mvt:if expr="POS1 MOD 4 EQ 0">
        <p>Product &mvt:POS1; is a multiple of 4: &mvt:product:name;</p>
    </mvt:if>
</mvt:foreach>

Output (for multiples of 4):

Product 4 is a multiple of 4: Product D
Product 8 is a multiple of 4: Product H
...


Nested Loops#

Use POS1 and POS2 in nested loops to track iterations at different levels.

<mvt:foreach iterator="category" array="categories">
    <p>Category (POS1): &mvt:POS1; - &mvt:category:name;</p>

    <mvt:foreach iterator="product" array="category:products">
        <p>&emsp;Product (POS2): &mvt:POS2; - &mvt:product:name;</p>
    </mvt:foreach>
</mvt:foreach>

Output:

Category (POS1): 1 - Electronics
    Product (POS2): 1 - Laptop
    Product (POS2): 2 - Smartphone
Category (POS1): 2 - Clothing
    Product (POS2): 1 - T-Shirt
    Product (POS2): 2 - Jeans


Best Practices#

  1. Use POS1 for Logic, Not Display:

    • POS1 cannot be output directly to the screen but can be used for testing conditions.
  2. Keep Track of Nesting:

    • For nested loops, remember the counters (POS2, POS3, etc.) correspond to their respective nesting levels.
  3. Combine with Conditional Logic:

    • Leverage POS1 with operators like EQ, MOD, or ranges to target specific iterations.
  4. Comment Your Code:

    • For complex loops, document how POS1 and other counters are being used.

Advanced Example: Custom Row Formatting#

Highlight every other row in a product table using POS1 for alternating styles.

<table>
    <mvt:foreach iterator="product" array="category_listing:products">
        <mvt:if expr='POS1 MOD 2 EQ 0'>
            <mvt:assign name="l.settings:parity" value="'even'" />    
        </mvt:if>
        <mvt:if expr='POS1 MOD 2 NE 0'>
            <mvt:assign name="l.settings:parity" value="'odd'" />
        </mvt:if>
        <tr class="&mvt:parity;">
            <td>&mvt:POS1;</td>
            <td>&mvt:product:name;</td>
        </tr>
    </mvt:foreach>
</table>

Output:

<tr class="odd">
    <td>1</td>
    <td>Product A</td>
</tr>
<tr class="even">
    <td>2</td>
    <td>Product B</td>
</tr>
...


Summary#

The POS1 counter is an invaluable tool for managing loop iterations in Miva templates. By understanding its behavior and leveraging nested counters (POS2, POS3, etc.), developers can create dynamic and flexible logic in their templates.

Key Points:#

  • POS1 starts at 1 and increments by one for each iteration.
  • It is available in both <mvt:foreach> and <mvt:while> loops.
  • Nested loops automatically create additional counters like POS2.