ViewFish Templating

The unless Statement

The {{unless}} statement is the inverse of {{isset}}. It shows content when a variable is not set or is empty.

Syntax

{{unless $variable}}
    Content shown when variable is NOT set.
{{/unless}}

With an else block:

{{unless $variable}}
    Content shown when variable is NOT set.
{{else}}
    Content shown when variable IS set.
{{/unless}}

Example

Template:

{{unless $discount}}
    <p>No discount available. Regular price: ${{price}}</p>
{{else}}
    <p>Discount applied! You save ${{discount}}.</p>
{{/unless}}

{{unless $in_stock}}
    <p>Out of stock. Check back later.</p>
{{/unless}}

PHP:

// Product with discount:
$data = ['price' => '49.99', 'discount' => '10.00', 'in_stock' => '1'];
echo $t->render($template, $data);
// Shows: "Discount applied! You save $10.00."

// Product without discount, out of stock:
$data = ['price' => '49.99'];
echo $t->render($template, $data);
// Shows: "No discount available. Regular price: $49.99"
// Shows: "Out of stock. Check back later."

When to use unless vs isset

  • Use {{isset}} when you want to show something only when data is present.
  • Use {{unless}} when you want to show something only when data is missing — like warnings, placeholders, or fallback content.

Both support the {{else}} block for full if/else logic.

You can see this in action in Example 10.