The Else Block

Show alternate content when a variable isn't set using {{else}}.


The {{else}} Block

Show alternate content when a variable isn't set.


User 1 — all fields present:
Welcome, Alice!

Your role: Administrator

Contact: [email protected]

User 2 — only name:
Welcome, Bob!

You have not been assigned a role yet.

⚠ No email on file. Please update your profile.

User 3 — name and role, no email:
Welcome, Charlie!

Your role: Editor

⚠ No email on file. Please update your profile.

<?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("demo7");

# User with all fields set
$user1 = [
    'name'  => 'alice',
    'role'  => 'administrator',
    'email' => '[email protected]',
];

# User missing role and email
$user2 = [
    'name' => 'bob',
];

# User with role but no email
$user3 = [
    'name'  => 'charlie',
    'role'  => 'editor',
];

echo "<h4>The <code>{{else}}</code> Block</h4>";
echo "<p>Show alternate content when a variable isn't set.</p><hr>";

echo "<h5>User 1 — all fields present:</h5>";
echo $T->render($template, $user1);

echo "<h5>User 2 — only name:</h5>";
echo $T->render($template, $user2);

echo "<h5>User 3 — name and role, no email:</h5>";
echo $T->render($template, $user3);
<div class="card mb-3">
    <div class="card-body">
        <h5>Welcome, {{name|ucfirst}}!</h5>
        {{isset $role}}
            <p class="text-success">Your role: <strong>{{role|ucwords}}</strong></p>
        {{else}}
            <p class="text-muted">You have not been assigned a role yet.</p>
        {{/isset}}
        {{isset $email}}
            <p>Contact: {{email}}</p>
        {{else}}
            <p class="text-warning">&#9888; No email on file. Please update your profile.</p>
        {{/isset}}
    </div>
</div>