<p>Whenever you output anything with Twig’s <code>{{ output tags }}</code>, the contents will get encoded by default. You have to pass the content through the <a href="http://twig.sensiolabs.org/doc/filters/raw.html" rel="noreferrer"><code>|raw</code></a> filter in order for it to be output verbatim.</p>

<pre class="lang-twig prettyprint-override"><code>{{ entry.myFieldHandle|raw }}
</code></pre>

CC BY-SA 3.0
2014-10-10T17:30:44.013Z
Thanks, @Brandon Kelly.  Passing the content through `|raw` works, but I'd like this work to be handled by my pugin on the backend.  When I try returning `TemplateHelper::getRaw($html)` in my `__toString()` function, I get an Internal Server Error:  `__toString()` must return a string.
Angela T
CC BY-SA 3.0
2014-10-10T18:05:59.750Z
Doh! Guess you’ll have to stick with `|raw` :-/
Brandon Kelly
CC BY-SA 3.0
2014-10-09T16:01:40.777Z
2014-10-10T18:05:32.640Z
2014-10-10T18:05:32.640Z
Brandon Kelly
<p>Try this:</p>

<pre><code>public function getFunctions() {
    return array (
        'my_func' =&gt; new \Twig_SimpleFunction('my_func', array($this, 'my_func'), array('is_safe' =&gt; array('html')))
    );
}
</code></pre>

<p>This being the important part:</p>

<pre><code>array('is_safe' =&gt; array('html'))
</code></pre>

CC BY-SA 3.0
2015-03-09T22:09:57.963Z
2015-03-09T22:09:57.963Z
Henry
<p>Not sure about model, but you can return raw, unescaped value from plugin variable like this:</p>

<pre><code>use craft\helpers\Template as TemplateHelper;

class someClass
{
    public function someFunction()
    {
        return TemplateHelper::raw($some_values);
    }
}
</code></pre>

CC BY-SA 4.0
2019-09-12T22:02:29.363Z
2019-09-12T22:02:29.363Z
Piotr Pogorzelski
<p>I've created a field type that stores its value as a model. That model’s <code>__toString()</code> method returns the result of a <code>craft()-&gt;templates-&gt;render()</code> call:</p>

<pre class="lang-php prettyprint-override"><code>public function render() 
{
    $render = craft()-&gt;templates-&gt;render('myplugin/templates/mymodeltemplate');
    return $render;
}

public function __toString()
{
    return $this-&gt;render();
}
</code></pre>

<p>When I output the model from a template, like so:</p>

<pre class="lang-twig prettyprint-override"><code>{{ entry.myField }}
</code></pre>

<p>Twig encodes the resulting HTML as a string with HTML entities.  Running the string through <code>html_entity_decode()</code> before returning it in <code>__toString()</code> doesn't help.  I'm assuming this string formatting is happening somewhere at the Twig level.</p>

<p>Being new to Craft, I'd love to understand a little more of what's going on here.  How does the string representation of an element model get passed to and handled by Twig?</p>

CC BY-SA 3.0
2014-10-09T15:55:48.883Z
Hey Angela, I’ve edited your question to focus on the first issue it was bringing up. Try to stick to that single-question format going forward. So your routing question should be its own separate question, for example.
Brandon Kelly
CC BY-SA 3.0
2014-10-09T18:08:32.613Z
Thanks so much for helping me organize & navigate this, Brandon!
Angela T
<templating><plugin-development><custom-fieldtype>
How do I prevent Twig from encoding HTML?
# How do I prevent Twig from encoding HTML?

(Asked by: username_0 on 2014-10-09)

I've created a field type that stores its value as a model. That model’s __toString() method returns the result of a craft()->templates->render() call:
public function render() 
{
    $render = craft()->templates->render('myplugin/templates/mymodeltemplate');
    return $render;
}

public function __toString()
{
    return $this->render();
}

When I output the model from a template, like so:
{{ entry.myField }}

Twig encodes the resulting HTML as a string with HTML entities.  Running the string through html_entity_decode() before returning it in __toString() doesn't help.  I'm assuming this string formatting is happening somewhere at the Twig level.
Being new to Craft, I'd love to understand a little more of what's going on here.  How does the string representation of an element model get passed to and handled by Twig?

**Tags:** templating, plugin-development, custom-fieldtype
> **Comments:**
> **username_1**: Hey Angela, I’ve edited your question to focus on the first issue it was bringing up. Try to stick to that single-question format going forward. So your routing question should be its own separate question, for example.  
> **username_0**: Thanks so much for helping me organize & navigate this, Brandon!  

---

## 3 answers

### Answer by username_1 on 2014-10-09. Score: 8

Whenever you output anything with Twig’s {{ output tags }}, the contents will get encoded by default. You have to pass the content through the |raw filter in order for it to be output verbatim.
{{ entry.myFieldHandle|raw }}

> **Comments:**
> **username_0**: Thanks, @Brandon Kelly.  Passing the content through `|raw` works, but I'd like this work to be handled by my pugin on the backend.  When I try returning `TemplateHelper::getRaw($html)` in my `__toString()` function, I get an Internal Server Error:  `__toString()` must return a string.  
> **username_1**: Doh! Guess you’ll have to stick with `|raw` :-/  

---

### Answer by username_2 on 2015-03-09. Score: 3

Try this:
public function getFunctions() {
    return array (
        'my_func' => new \Twig_SimpleFunction('my_func', array($this, 'my_func'), array('is_safe' => array('html')))
    );
}

This being the important part:
array('is_safe' => array('html'))


---

### Answer by username_3 on 2019-09-12. Score: 2

Not sure about model, but you can return raw, unescaped value from plugin variable like this:
use craft\helpers\Template as TemplateHelper;

class someClass
{
    public function someFunction()
    {
        return TemplateHelper::raw($some_values);
    }
}


---

