Skip to content

<mvt:while>#


The <mvt:while> loop in Miva Merchant is a powerful construct for repeating a block of code as long as a specified condition evaluates to true. This loop provides developers with a flexible way to handle iterative tasks, making it a fundamental tool for dynamic template logic.


Syntax#

The <mvt:while> loop requires an expr attribute to define the condition that will be evaluated during each iteration. The loop executes as long as this condition remains true.

<mvt:while expr="condition">
    <!-- Code to execute while condition is true -->
</mvt:while>

Example:#

<mvt:assign name="g.counter" value="0" />
<mvt:while expr="g.counter LT 5">
    Iteration: &mvt:global:counter; <br>
    <mvt:assign name="g.counter" value="g.counter + 1" />
</mvt:while>

Output:

Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4


Attributes#

Attribute Description
expr Defines the condition that is evaluated during each iteration.

Loop Control Tags#

<mvt:whilestop />#

Stops the loop execution when a condition is met, allowing the program to exit the loop prematurely.

<mvt:whilecontinue />#

Skips the current iteration and returns to the beginning of the loop if a condition is met.


Examples#

Incrementing a Counter#

<mvt:assign name="l.counter" value="0" />
<mvt:while expr="l.counter LTE 10">
    The value of counter is: &mvt:counter; <br>
    <mvt:assign name="l.counter" value="l.counter + 1" />
</mvt:while>

Output:

The value of counter is: 0
The value of counter is: 1
The value of counter is: 2
The value of counter is: 3
The value of counter is: 4
The value of counter is: 5
The value of counter is: 6
The value of counter is: 7
The value of counter is: 8
The value of counter is: 9
The value of counter is: 10


Using <mvt:whilecontinue />#

<mvt:assign name="g.counter" value="0" />
<mvt:while expr="g.counter LT 10">
    <mvt:if expr="g.counter MOD 2 EQ 0">
        <mvt:whilecontinue />
    </mvt:if>
    Odd number: &mvt:global:counter; <br>
    <mvt:assign name="g.counter" value="g.counter + 1" />
</mvt:while>

Output:

Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9


Using <mvt:whilestop />#

<mvt:assign name="g.counter" value="0" />
<mvt:while expr="g.counter LT 10">
    <mvt:if expr="g.counter EQ 5">
        <mvt:whilestop />
    </mvt:if>
    Iteration: &mvt:global:counter; <br>
    <mvt:assign name="g.counter" value="g.counter + 1" />
</mvt:while>

Output:

Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4


Best Practices#

  1. Avoid Infinite Loops: Always ensure your condition will eventually evaluate to false to prevent infinite loops.

  2. Use Loop Control Tags Wisely:

    • Use <mvt:whilestop /> to exit loops early when necessary.
    • Use <mvt:whilecontinue /> to skip specific iterations.
  3. Combine with Conditional Logic: Combine <mvt:while> with conditional statements to create dynamic and flexible logic.

  4. Comment Complex Loops: Document the purpose of loops to improve code readability and maintainability.


Summary#

The <mvt:while> loop provides robust functionality for iterative tasks in Miva templates. By understanding its syntax, attributes, and control tags, you can efficiently handle dynamic content and enhance your store’s templates.