Default Values
Provide fallback values for missing or empty variables with |default:"value".
Default Values
Use {{var|default:"fallback"}} to provide a value when a variable is missing or empty.
Full profile:
Charlie Bucket
Role: Special Agent
Location: Walla Walla, Washington, United States
Bio: Young boy, fond of candy.
Only name provided:
Williaam Wonka
Role: Chocolate Manufacturer
Location: Unknown, Earth
Bio: No bio provided.
Empty data — all defaults:
Anonymous User
Role: Guest
Location: Unknown, Earth
Bio: No bio provided.
<?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("demo9");
# Full profile
$profile1 = [
'name' => 'Charlie Bucket',
'role' => 'Special Agent',
'city' => 'walla walla, Washington',
'country' => 'united states',
'bio' => 'Young boy, fond of candy.',
];
# Sparse profile — defaults will fill in
$profile2 = [
'name' => 'Williaam Wonka',
'role' => 'Chocolate Manufacturer',
];
# Empty profile — all defaults
$profile3 = [];
echo "<h4>Default Values</h4>";
echo "<p>Use <code>{{var|default:\"fallback\"}}</code> to provide a value when a variable is missing or empty.</p><hr>";
echo "<h5>Full profile:</h5>";
echo $T->render($template, $profile1);
echo "<h5>Only name provided:</h5>";
echo $T->render($template, $profile2);
echo "<h5>Empty data — all defaults:</h5>";
echo $T->render($template, $profile3);
<div class="card mb-3">
<div class="card-body">
<h5>{{name|default:"Anonymous User"}}</h5>
<p><strong>Role:</strong> {{role|default:"Guest"}}</p>
<p><strong>Location:</strong> {{city|default:"Unknown"|ucwords}}, {{country|default:"Earth"|ucwords}}</p>
<p><strong>Bio:</strong> {{bio|default:"No bio provided."}}</p>
</div>
</div>