sFire PHP Framework

Template engine: For and foreach loops

You can use loops into your view. To do so, you may use the for or foreach method. These are similar like the PHP loops as you can see below.

In this section we will handle:

  • For loops
  • Foreach loops

For loops

You can easily create a basic for loop like:

@for($i =  0; $i < 5; $i++)
    {{ $i }} 
@endfor

In this case the "$i" is created by the for loop for you to use within the loop. You may also use your own assigned variables:

@for($i =  $foo; $i < $bar; $i++)
    {{ $i }} 
@endfor

Foreach loops

To loop through a dataset, you can use the foreach loop which is also similar to the PHP foreach loop.

@foreach(['a', 'b', 'c'] as $value)
    {{ $value }} 
@endforeach

@foreach(['a', 'b', 'c'] as $key => $value)
    {{ $key }} - {{ $value }} 
@endforeach