Displaying HTML with Blade shows the HTML code

Displaying HTML with Blade shows the HTML code

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Blade Renders HTML as Text and How to Fix It

When working with Laravel, you might encounter a scenario where a variable containing valid HTML tags appears as plain text in your view instead of being rendered as markup. Consider this specific case: you have a string like '<p><strong>Lorem</strong> ipsum dolor <img src="images/test.jpg"></p>' stored in a variable named $text. If you try to output it directly using Blade's double braces syntax—{{ $text }}—you will see the raw HTML code displayed to the user. This happens because the default behavior of {{ }} in Blade is to escape HTML entities, ensuring that special characters are safely encoded and not interpreted as markup by the browser.

This escaping mechanism exists for a critical security reason: preventing Cross-Site Scripting (XSS) attacks. By default, Laravel assumes that any content passed to your view is untrusted input from users or external sources. Escaping this input guarantees that malicious scripts embedded in strings cannot execute within the context of your application. However, when you are displaying content that you explicitly trust—such as a string generated by your own logic or fetched from a controlled internal API—you need to override this default safety behavior.

The solution is straightforward: switch from double braces to single braces. Using {{ $text }} tells Blade to treat the variable as safe and output it exactly as is. Conversely, using {!! $text !!} achieves the same result but explicitly signals that you are rendering unescaped HTML. While both approaches work for displaying trusted content, they carry different implications regarding code clarity and security posture.

Choosing Between {{ }} and {!! !!}

There is a subtle but important distinction between these two syntaxes regarding how they interact with Blade directives. When using the double braces {{ $text }}, Laravel first escapes the HTML entities within the string. If your variable contains &lt;p&gt;, it outputs those literal characters. However, if you want to render that as a paragraph tag, you must use single braces {!! $text !!}.

It is crucial to understand what happens inside the double braces when dealing with Blade syntax. If your $text string contains Blade directives like @if or {{ 'nested' }}, using {!! $text !!} will render those directives as literal text strings rather than executing them. For example, if $text equals 'User: {{ name }}', then {!! $text !!} outputs the exact characters "User: {{ name }}". If you intended to execute the nested Blade logic, you would need to use {{ $text }} first, but this would re-escape the content. Therefore, if your string contains raw HTML mixed with dynamic data that should be rendered as markup (not executed as code), {!! !!} is the correct choice for rendering the HTML portion while accepting the risk of potential XSS if the source isn't strictly controlled.

The e() Helper and Blade Compatibility

You might wonder why PHP's native echo function displays the HTML correctly in your tests or other contexts while Blade does not by default. In raw PHP, echo $text outputs the string without modification unless you explicitly use the htmlspecialchars() function or a similar escape helper. The discrepancy arises because Blade templates are essentially processed strings where {{ }} is a shorthand for "escape and output."

To maintain consistency with standard PHP practices while still using Blade's templating engine, you can utilize the e() helper function within your Blade views. If you define $text as your HTML string, writing <p>{{ e($text) }}</p> will output the escaped version, whereas <p>{!! $text !!}</p> outputs the raw HTML. The e() function is essentially an alias for htmlspecialchars, which converts special characters to their HTML entities.

Security Implications and Best Practices

The decision to use {!! !!} should never be taken lightly. While this syntax solves the immediate problem of displaying stored HTML, it bypasses Laravel's built-in XSS protection for that specific variable. If the $text variable ever contains user input—such as a comment or post retrieved from a database without sanitization—using {!! $text !!} could expose your application to security vulnerabilities.

Always ensure that any content rendered with {!! !!} originates from a trusted source, such as a value computed internally by your application logic or a known-safe configuration file. If you are displaying user-generated content, you must sanitize it first before rendering it as HTML. This might involve stripping dangerous tags using libraries like DOMDocument or ensuring the input was sanitized at the controller level.

For more complex scenarios involving rich text editors or CMS integrations where HTML is expected, consider storing content in a database column that explicitly supports HTML types and implementing strict validation rules to allow only safe tags. If you need to share resources regarding secure HTML handling in Laravel projects, you can find further guidance at https://laravelcompany.com.

Final Considerations

In summary, the issue of displaying HTML code instead of rendered markup stems from Blade's default escaping behavior designed to protect against XSS. To resolve this, replace {{ $text }} with {!! $text !!}. This change instructs the engine to output the string as raw HTML. However, this flexibility comes with the responsibility of ensuring the content is safe. Always verify the origin and nature of the data you are rendering before opting to disable automatic escaping.

Our network