Render HTML safe content in Laravel Blade
June 28, 2022 ‐ 1 min read
Laravel Blade automatically escapes HTML tags if you render content using the double braces ({{ ... }}
). Blade uses the native PHP function htmlspecialchars()
to do so.
This is a good thing, because if users enter valid HTML, Blade makes sure that this won't be rendered as actual HTML (or JavaScript more harmfully). Instead the contents are shown as regular text, thus preventing cross site scripting.
In certain cases however, you want to be able to render HTML using blade. When you know what you are doing. In such cases we shouldn't be using the double braces syntax, but {!! ... !!}
instead.
In your blade templates you can use the following syntax to render unescaped data.
<body>
{!! $html !!}
</body>