For Loop Examples for Templex
The for loop feature allows you to create iterative logic with initialization, condition, and increment expressions, similar to for loops in programming languages.
Basic Syntax
Section titled “Basic Syntax”<{ for( $variable = start; condition; increment ) }> Content to repeat<{ endfor }>Examples
Section titled “Examples”Basic Counting Loop
Section titled “Basic Counting Loop”<{ for( $i = 0; $i < 5; $i++ ) }> <p>Iteration <{ $i }></p><{ endfor }>Countdown Loop
Section titled “Countdown Loop”<{ for( $i = 10; $i > 0; $i-- ) }> <p>Countdown: <{ $i }></p><{ endfor }>Step Increment
Section titled “Step Increment”<{ for( $i = 0; $i < 20; $i += 5 ) }> <p>Value: <{ $i }></p><{ endfor }>Step Decrement
Section titled “Step Decrement”<{ for( $i = 100; $i > 0; $i -= 10 ) }> <p>Countdown by 10: <{ $i }></p><{ endfor }>Using Variables for Limits
Section titled “Using Variables for Limits”<{ for( $i = $startValue; $i <= $endValue; $i++ ) }> <p>Number: <{ $i }></p><{ endfor }>Different Comparison Operators
Section titled “Different Comparison Operators”<!-- Less than or equal --><{ for( $i = 1; $i <= 3; $i++ ) }> <p>Item <{ $i }></p><{ endfor }>
<!-- Greater than or equal --><{ for( $i = 5; $i >= 1; $i-- ) }> <p>Reverse: <{ $i }></p><{ endfor }>Nested For Loops
Section titled “Nested For Loops”<{ for( $row = 1; $row <= 3; $row++ ) }> <div class="row"> <{ for( $col = 1; $col <= 3; $col++ ) }> <span class="cell">(<{ $row }>,<{ $col }>)</span> <{ endfor }> </div><{ endfor }>Creating Tables
Section titled “Creating Tables”<table> <{ for( $row = 1; $row <= $rows; $row++ ) }> <tr> <{ for( $col = 1; $col <= $cols; $col++ ) }> <td>Cell <{ $row }>-<{ $col }></td> <{ endfor }> </tr> <{ endfor }></table>Generating Lists
Section titled “Generating Lists”<ul> <{ for( $i = 1; $i <= $itemCount; $i++ ) }> <li>List item number <{ $i }></li> <{ endfor }></ul>Supported Features
Section titled “Supported Features”Initialization Patterns
Section titled “Initialization Patterns”- Simple assignment:
$i = 0 - Variable assignment:
$i = $startValue - Any variable name:
$counter = 1,$index = 0
Condition Operators
Section titled “Condition Operators”- Less than:
$i < 10 - Less than or equal:
$i <= 10 - Greater than:
$i > 0 - Greater than or equal:
$i >= 0 - Equal:
$i == 5 - Strict equal:
$i === 5 - Not equal:
$i != 5 - Strict not equal:
$i !== 5
Increment Types
Section titled “Increment Types”- Post-increment:
$i++(adds 1) - Post-decrement:
$i--(subtracts 1) - Addition assignment:
$i += 2(adds specified value) - Subtraction assignment:
$i -= 2(subtracts specified value)
Data Types
Section titled “Data Types”- Integers:
$i = 0,$i < 10 - Variables:
$i = $start,$i <= $end - Expressions: Any valid numeric expression
Loop Metadata
Section titled “Loop Metadata”For loops expose metadata variables inside the loop body:
| Variable | Description |
|---|---|
$loop_index |
Zero-based iteration index (0, 1, 2, …) |
$loop_count |
Total number of iterations |
$loop_first |
true on the first iteration |
$loop_last |
true on the last iteration |
Note that $loop_index is the iteration count (starting from 0), while the loop variable (e.g. $i) holds the actual value which may differ when using step increments.
Highlighting First and Last Rows
Section titled “Highlighting First and Last Rows”<{ for( $i = 1; $i <= $totalRows; $i++ ) }> <tr<{ if( $loop_first ) }> class="first"<{ endif }><{ if( $loop_last ) }> class="last"<{ endif }>> <td>Row <{ $i }></td> </tr><{ endfor }>Showing Progress
Section titled “Showing Progress”<{ for( $i = 0; $i < $steps; $i++ ) }> Processing step <{ $loop_index }> of <{ $loop_count }>...<{ endfor }>Advanced Examples
Section titled “Advanced Examples”Creating Pagination
Section titled “Creating Pagination”<div class="pagination"> <{ for( $page = 1; $page <= $totalPages; $page++ ) }> <{ if( $page === $currentPage ) }> <span class="current"><{ $page }></span> <{ else }> <a href="?page=<{ $page }>"><{ $page }></a> <{ endif }> <{ endfor }></div>Building Navigation Menus
Section titled “Building Navigation Menus”<nav> <{ for( $level = 1; $level <= $maxLevel; $level++ ) }> <ul class="level-<{ $level }>"> <{ for( $item = 1; $item <= $itemsPerLevel; $item++ ) }> <li><a href="/level<{ $level }>/item<{ $item }>">Item <{ $item }></a></li> <{ endfor }> </ul> <{ endfor }></nav>Usage with PHP
Section titled “Usage with PHP”use Myerscode\Templex\Templex;
$templex = new Templex(__DIR__ . '/templates/');
$data = [ 'startValue' => 1, 'endValue' => 5, 'rows' => 3, 'cols' => 4, 'itemCount' => 10];
$result = $templex->render('my-template.stub', $data);echo $result;Performance Notes
Section titled “Performance Notes”- For loops are processed at template compilation time
- Large loops (1000+ iterations) may impact performance
- Consider using
foreachloops for array iteration instead - Nested loops multiply iteration counts (3x3 = 9 iterations)
Best Practices
Section titled “Best Practices”- Use meaningful variable names:
$row,$item,$indexinstead of$i - Avoid infinite loops: Always ensure the condition will eventually be false
- Consider alternatives: Use
foreachfor arrays,iffor simple conditions - Limit nesting: Deep nesting can make templates hard to read
- Use variables for limits: Makes templates more flexible and reusable