Use case for html()

Hi there,

What’s the use case for using the helper html()? Can you please provide any examples?

Thanks.

The helper does the same as the html()field method:

<?= html($page->title()) ?>

is the same as

<?= $page->title()->html() ?>

With the difference that the field method can only be used on a field, whereas the html() helper can be used with a simple string.

Thanks. I was meaning, why should I use this helper at all? I understand the use case for kirbytext() (converting Markdown to HTML), but I don’t understand why I would use html() instead of just echoing the text.

Consider these two examples:

<?= html('<script>alert("Oops, too bad. I just deleted your webpage")</script>') ?>
<?= '<script>alert("Oops, too bad. I just deleted your webpage")</script>' ?>

Test them in your browser one by one.

Additionally, the html()helper/method allows you to remove tags:

<?= html('<script>alert("Oops, too bad. I just deleted your webpage")</script>', false) ?>

The script tag and everything inside it is then rendered as text <script>alert("Oops, too bad. I just deleted your webpage")</script>

1 Like