Loop Indexes

Access position metadata inside loops: {{@index}}, {{@count}}, {{@first}}, {{@last}}, {{@total}}.


Loop Indexes

Access {{@index}}, {{@count}}, {{@first}}, {{@last}}, and {{@total}} inside loops.


# Name Score Position
1 / 5 Mario 9800 🏆 Leader
2 / 5 Luigi 7200
3 / 5 Peach 6500
4 / 5 Toad 4100
5 / 5 Bowser 1200 💩 Last place
<?php

# require the templating library
require '../vendors/ViewFish/viewfish.php';

# instantiate a ViewFish object
$T = new ViewFish\viewfish();

# set the template path
$T->set_template_path(__DIR__.'/../templates/');

# load the template
$template = $T->load_template("demo8");

# Leaderboard data
$data = [
    'players' => [
        ['name' => 'mario',   'score' => 9800],
        ['name' => 'luigi',   'score' => 7200],
        ['name' => 'peach',   'score' => 6500],
        ['name' => 'toad',    'score' => 4100],
        ['name' => 'bowser',  'score' => 1200],
    ],
];

echo "<h4>Loop Indexes</h4>";
echo "<p>Access <code>{{@index}}</code>, <code>{{@count}}</code>, <code>{{@first}}</code>, <code>{{@last}}</code>, and <code>{{@total}}</code> inside loops.</p><hr>";

echo $T->render($template, $data);
<table class="table table-striped">
    <thead>
        <tr>
            <th>#</th>
            <th>Name</th>
            <th>Score</th>
            <th>Position</th>
        </tr>
    </thead>
    <tbody>
    {{@loop data=players}}
        <tr{{isset $@first}} class="table-success"{{/isset}}{{isset $@last}} class="table-danger"{{/isset}}>
            <td>{{@count}} / {{@total}}</td>
            <td>{{name|ucwords}}</td>
            <td>{{score}}</td>
            <td>{{isset $@first}}&#x1F3C6; Leader{{/isset}}{{isset $@last}}&#x1F4A9; Last place{{/isset}}</td>
        </tr>
    {{/loop}}
    </tbody>
</table>