html()
Twig Function
Functions prefixed with html_
perform tasks that are useful when dealing with HTML markup. The helper maps directly to the Html
PHP class and its methods. For example:
{{ html_strip() }}
The above is the PHP equivalent of the following:
<?= Html::strip() ?>
Methods in camelCase should be converted to snake_case.
You may also apply the HTML functions as a Twig filter.
{{ ''|html_strip }}
# html_strip()
Removes HTML from a string.
// Outputs: Hello world
{{ '<strong>Hello world</strong>'|html_strip }}
You may pass the first argument as allowable tags.
// Outputs: <p>Text</p>
{{ '<p><b>Text</b></p>'|html_strip('<p>') }}
# html_limit()
Limits HTML with specific length with a proper tag handling.
{{ '<p>Post content...</p>'|html_limit(100) }}
To add a suffix when limit is applied, pass it as the second argument. Defaults to ...
.
{{ '<p>Post content...</p>'|html_limit(100, '... Read more!') }}
# html_clean()
Cleans HTML to prevent most XSS attacks.
{{ '<script>window.location = "http://google.com"</script>'|html_clean }}
# html_email()
Obfuscates an e-mail address to prevent spam-bots from sniffing it.
{{ 'me@mysite.tld'|html_email }}
For example:
<a href="mailto: {{ 'me@mysite.tld'|html_email }}">Email me</a>
<!-- The above will output -->
<a href="mailto: mailto:a@b.c">Email me</a>
# html_mailto()
Outputs a complete anchor link with obfuscated email.
{{ 'me@mysite.tld'|html_mailto }}
← form()