Skip to content

Kirby 4.1.2

e()

Smart version of echo with an if condition as first argument

e(mixed $condition, mixed $value, mixed $alternative = null): void

Parameters

Name Type Default Description
$condition * mixed
$value * mixed The string to be echoed if the condition is true
$alternative mixed null An alternative string which should be echoed when the condition is false

Example

With fallback if condition is false:

<?php e(1 === 2, 'hell freezes over', 'everything is ok') ?>

Only echo something when condition is true:

<ul>
<?php foreach($pages as $p): ?>
    <li<?php e($p->isActive(), ' class="active"') ?>>
        …
    </li>
<?php endforeach ?>
</ul>

Note:
e() is a function, therefore the parameters you pass to this function have already been evaluated when you pass them to the method. Trying to do something like the following will therefore throw an error:

<?php e($site->find('not-existing'), $site->find('not-existing')->title(), "No title") ?>

For cases like this, you have the following options, depending on whether you need a fallback value or not:

if statement

<?php if ($p = $site->find('not-existing')) {
    echo $p->title();
} else {
    echo 'No title';
}

Ternary operator

<?= ($p = $site->find('not-existing')) ? $p->title() : 'No title' ?>

Null-safe operator

<?= $site->find('not-existing')?->title() ?>