Unescape or html decode

Unescape or html decode

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

Stefan Bogdanescu

Founder & Senior Architect

Unescape or HTML Decode: When Twig Protects You Too Much

It happens frequently in full-stack development, particularly when integrating legacy backends with modern templating engines. You are rendering data from a PHP controller that has already prepared clean strings for the browser. However, upon switching to Twig, those same values suddenly appear double-encoded: &lt;a href=&#039;... instead of the expected <a href='...'>.

This occurs because Twig treats all output by default as untrusted data. When you use the standard {{ item.Link }} syntax, Twig automatically applies HTML escaping to prevent Cross-Site Scripting (XSS) attacks. It converts characters like <, >, and ' into their HTML entity equivalents. If your backend logic assumes Twig will render raw HTML, this automatic protection breaks the layout and renders tags as plain text.

The solution depends entirely on the trustworthiness of your data source. You cannot simply apply a blanket "unescape" strategy without understanding the security implications.

The Security Trade-off: raw vs. escape

If you confirm that the data in item.Link is safe—meaning it originated from a trusted database column that never accepts user input directly, or has been sanitized on the server side before reaching Twig—you can bypass the automatic escaping filter. In Twig 1.12.2 and later versions, you must explicitly tell the template engine to treat the variable as trusted HTML.

You do this by appending the |raw filter:

<td>{{ item.Link | raw }}</td>

This instruction tells Twig: "Do not convert < or > into &lt; and &gt;; print them exactly as they are." This restores the intended HTML structure without requiring any changes to your PHP backend.

When You Need escape_html Explicitly

The inverse scenario is equally common but often misunderstood. If you have data that must be displayed as text (for example, a filename containing <script> tags), relying on the default behavior can sometimes feel restrictive if you are trying to embed content dynamically. However, Twig's default behavior is actually to escape by default unless raw is used.

If you were writing raw HTML in Twig and needed to display a variable that shouldn't be escaped, using the default {{ item.Name }} would break your code. Conversely, if you are generating safe HTML strings in PHP (like a formatted email body or a table row) and passing them to Twig, you must use |raw.

It is crucial not to confuse this with the escape("html") filter. Using {{ item.Link | escape("html") }} would force escaping on a variable that might already be escaped in your PHP code, resulting in double-encoding (&amp;lt;). Always check the state of the data before it hits the template. If the PHP variable holds <a>, use |raw. If it holds &lt;a&gt;, use the default output or explicitly escape again to ensure safety.

Handling Specific Characters and Encoding Artifacts

The source material mentions seeing &#039; for single quotes. This is standard HTML entity encoding for a single quote ('). Twig's escaping mechanism converts ' to &#039; (or sometimes just &#39; depending on the environment) because it treats single quotes as special characters in certain contexts or simply adheres to strict HTML5 entity standards.

When you apply |raw, these entities revert to their literal character counterparts, restoring the original quote marks required for valid HTML attribute values. This is distinct from "unescaping" a string that was intentionally encoded in PHP. If your PHP code explicitly stored &lt;a&gt; in the database (which is bad practice), Twig would display it as text by default. If you want to render that text literally, you stick with the default behavior. If your PHP code stored <a> but Twig turned it into &lt;a&gt;, then |raw is the correct fix.

Implementation Best Practices

To prevent future confusion in mixed PHP/Twig environments:

  1. Sanitize at the Source: Never rely on Twig to sanitize data that came from a user input field or an external API. Perform sanitization in the controller layer.
  2. Audit Data Flow: Before applying |raw, trace the variable back to its origin. If it touches user input at any point before reaching Twig, do not use |raw.
  3. Default Behavior Awareness: Remember that {% autoescape true %} is the default setting in Twig. It applies to every block unless you are in a specific mode or using filters.

By understanding that {{ item.Link }} is an escape function and {{ item.Link | raw }} is the override, you can seamlessly integrate dynamic HTML generation from your backend into Twig templates without compromising security or breaking layout.

Our network