Posts Learn Components new Snippets new Categories Tags About
/

How to Display HTML in Laravel Blade

Learn how to display HTML in Laravel Blade that's returned from a source that's a string value

Created on Oct 01, 2021

27 views

Sometimes you might have a string of HTML tags/components that's returned from the database or 3rd party source and you want to render it in Laravel Blade. If using the normal blade "{{ $content }}" curly braces to output it, it will be rendered as a string and not HTML. This behavior happens because Laravel by default escapes the value when using the syntax.
{{ $content }}
To render the raw HTML you will have to unescape it and to do that you can make use of the following syntax.
{!! $renderHTMLContent !!}
By default using the syntax above automatically unescapes any value using the PHP "htmlspecialchars" function but make sure to validate the data if it's entered by the user. Behind the scene, the compiled template looks like below.
{{ $content }} is converted to <?php echo e($content); ?>
While for the unescaped version, it will be as follows.
{!! $renderHTMLContent !!} is converted to <?php echo $renderHTMLContent; ?>
new

If you like our tutorial, do make sure to support us with some coffee. We need the juice to write the content during the night 🤙🏻