Displaying HTML with Blade shows the HTML code
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Displaying HTML with Blade: A Comprehensive Guide for Laravel Developers
Body:
Introduction
In Laravel, we often use Blade templates for rendering views and displaying content. While dealing with HTML strings in Blade, you might have encountered situations when the string is displayed as raw text instead of being rendered as HTML. In this blog, we will address this issue and provide a detailed solution on how to properly display HTML with Blade in Laravel projects.Understanding Why the HTML is Not Rendered
By default, the Blade template engine treats strings containing HTML as raw text to prevent potential security vulnerabilities. This behavior is designed to protect your application from possible attacks. However, sometimes, you may want to display HTML code instead of having it parsed by the browser.Possible Solutions
There are multiple ways to achieve the desired result of showing HTML without compromising the security of your Laravel app: 1. Using the PHP `echo()` function You can still use the `echo()` function to display your HTML string as a workaround in Blade templates. Simply replace `{{$text}}` with ``.
2. Encoding the String as HTML Entities
You can convert the given HTML string into its encoded entity representation using the PHP's htmlspecialchars function. This will allow the browser to render the content without parsing it as code.
{{htmlspecialchars($text)}}
3. Wrapping the String in HTML Tags
Another approach is to wrap the string with HTML tags, like `<div>` and `</div>`. This ensures that the content will be properly rendered as HTML code within your Blade template.
{{'<div>'.$text.'</div>'}}
4. Using Third-party Libraries for Safe Rendering
There are several third-party libraries, such as Laravel Sanitizer or HTML Purifier, that provide an easy way to sanitize and render the given HTML string in a safe manner.
{{Html::fromString($text)}}
5. Using Laravel's Blade Directive `raw()`
Blade also provides a directive called `@raw`, which allows you to disable auto-escaping for a specific string. This directive ensures that the HTML code will be displayed as it is without any modifications.
{{$text}} @raw
6. Using Laravel's Blade Directive `safe()`
The `@safe` directive can also be used to display raw data in a safe manner, but it should only be used when you are certain that the input is coming from a trusted source and doesn't require any further sanitization or processing.
{{$text}} @safe