ForEach

The ForEach Loop in Ahmed Template Engine for INEX SPA is used to iterate over arrays or collections, allowing dynamic rendering of lists and repeated elements.

Syntax

@foreach($array as $item)
    <!-- Code to execute for each item in the array -->
@endforeach

Example:

@foreach($users as $user)
    <p>{{$user['name']}} - {{$user['email']}}</p>
@endforeach

This will loop through the $users array and display the name and email of each user.

Using Index

If you need to access the index, use:

@foreach($users as $index => $user)
    <p>#{{$index + 1}} - {{$user['name']}}</p>
@endforeach

Checking for an Empty Array

Use @empty to handle cases where the array is empty:

Nesting ForEach Loops

You can nest loops for multi-dimensional arrays:

Breaking Out of a Loop

To exit a loop early, use @break:

Skipping an Iteration

To skip a specific iteration, use @continue:

Conclusion

The ForEach Loop in Ahmed Template Engine provides a powerful and flexible way to iterate over arrays, making template development more dynamic and efficient.

Last updated