Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T17:30:44.013Z
Answers.Text: 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.
Answers.UserDisplayName: Angela T
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T18:05:59.750Z
Answers.Text: Doh! Guess you’ll have to stick with `|raw` :-/
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-09T16:01:40.777Z
Answers.LastActivityDate: 2014-10-10T18:05:32.640Z
Answers.LastEditDate: 2014-10-10T18:05:32.640Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-09T22:09:57.963Z
Answers.LastActivityDate: 2015-03-09T22:09:57.963Z
Answers.UserDisplayName: Henry
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-12T22:02:29.363Z
Answers.LastActivityDate: 2019-09-12T22:02:29.363Z
Answers.UserDisplayName: Piotr Pogorzelski
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-09T15:55:48.883Z
Comments.Text: 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.
Comments.UserDisplayName: Brandon Kelly
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-09T18:08:32.613Z
Comments.Text: Thanks so much for helping me organize & navigate this, Brandon!
Comments.UserDisplayName: Angela T
Tags: <templating><plugin-development><custom-fieldtype>
Title: How do I prevent Twig from encoding HTML?
ThreadText: # 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:
  
  ```php
  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:
  
  ```twig
  {{ 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 username_0, 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, username_1!  
  
  ---
  
  ## 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`](http://twig.sensiolabs.org/doc/filters/raw.html) filter in order for it to be output verbatim.
  
  ```twig
  {{ entry.myFieldHandle|raw }}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks, @username_1.  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);
      }
  }
  
  ```
  
  
  ---
  
  