<p>The easiest way i've found to do that is to use TemplateHelper::getRaw()</p>

<p>So in your "prepValue" function inside your fieldtype class do something like this</p>

<pre><code>function prepValue($value) {
    //any other data manipulation stuff
    return TemplateHelper::getRaw($value);
}
</code></pre>

<p><a href="http://buildwithcraft.com/docs/plugins/field-types#modifying-your-fieldtypes-stored-data-for-use">More info on prepValue in the docs.</a></p>

CC BY-SA 3.0
2014-08-17T14:46:07.427Z
2014-08-17T14:46:07.427Z
Keith Mancuso
<p>Here are two ways I know of that allow you to mark your content as safe before you hand it off to the template.  I'm not sure they will fit your exact use case, but perhaps they are a place to start:</p>

<p>1) Twig_Markup</p>

<p>From a variable you can return a new instance of Twig_Markup to the template:</p>

<pre><code>return new \Twig_Markup($yourReturnData, craft()-&gt;templates-&gt;getTwig()-&gt;getCharset());
</code></pre>

<p>2) is_safe</p>

<p>When creating a Twig filter, you can return that data to the template marking it as safe by passing <code>'is_safe' =&gt; array('html')</code> as the third argument to the Twig_Filter_Method.</p>

<pre><code>public function getFilters()
{
  return array(
    'yourtwigfilter' =&gt; new Twig_Filter_Method(
      $this, 
     'customFunction', 
     array(
       'is_safe' =&gt; array('html')
     )
  ));
}
</code></pre>

CC BY-SA 3.0
2014-08-04T22:15:02.257Z
Twig_Markup was my first thought too, however in the context of using this within against a model (which is within being returned from a custom field), it doesn't quite work.  In the context of `{{entry.customField}}` the actual variable being parsed is hitting the native `__toString()` method on the model. That expects a plain string returned, not a Twig_Markup object.
Joel Bradbury
CC BY-SA 3.0
2014-08-04T14:35:29.387Z
2014-08-04T14:43:34.147Z
2014-08-04T14:43:34.147Z
Ben Parizek
<p>I have a model that returns markup as part of a variable. To escape twig's encoding we have to include the raw filter, like so : </p>

<pre><code>{{ entry.customField | raw }}
</code></pre>

<p>The encoded output of that field is never useful, so ideally we'd have a way to get the same result without that twig filter. ie : </p>

<pre><code>{{ entry.customField }}
</code></pre>

<p>Looking at the various twig and template components I doubt this is possible, but is there any hidden ways to get this working? </p>

<p>This is all designed to remove that extra requirement from the usage, so wrapping unescape tags around it isn't want I'm looking for.</p>

<plugin-development><templating>
How can I pre-raw a Model attribute for the Twig template? (ie. "myModel.myAttr" without requiring the raw filter)
# How can I pre-raw a Model attribute for the Twig template? (ie. "myModel.myAttr" without requiring the raw filter)

(Asked by: username_0 on 2014-08-04)

I have a model that returns markup as part of a variable. To escape twig's encoding we have to include the raw filter, like so : 
{{ entry.customField | raw }}

The encoded output of that field is never useful, so ideally we'd have a way to get the same result without that twig filter. ie : 
{{ entry.customField }}

Looking at the various twig and template components I doubt this is possible, but is there any hidden ways to get this working? 
This is all designed to remove that extra requirement from the usage, so wrapping unescape tags around it isn't want I'm looking for.

**Tags:** plugin-development, templating

---

## 2 answers

### Answer by username_1 on 2014-08-17. Score: 8

The easiest way i've found to do that is to use TemplateHelper::getRaw()
So in your "prepValue" function inside your fieldtype class do something like this
function prepValue($value) {
    //any other data manipulation stuff
    return TemplateHelper::getRaw($value);
}

More info on prepValue in the docs.


---

### Answer by username_2 on 2014-08-04. Score: 4

Here are two ways I know of that allow you to mark your content as safe before you hand it off to the template.  I'm not sure they will fit your exact use case, but perhaps they are a place to start:
1) Twig_Markup
From a variable you can return a new instance of Twig_Markup to the template:
return new \Twig_Markup($yourReturnData, craft()->templates->getTwig()->getCharset());

2) is_safe
When creating a Twig filter, you can return that data to the template marking it as safe by passing 'is_safe' => array('html') as the third argument to the Twig_Filter_Method.
public function getFilters()
{
  return array(
    'yourtwigfilter' => new Twig_Filter_Method(
      $this, 
     'customFunction', 
     array(
       'is_safe' => array('html')
     )
  ));
}

> **Comments:**
> **username_0**: Twig_Markup was my first thought too, however in the context of using this within against a model (which is within being returned from a custom field), it doesn't quite work.  In the context of `{{entry.customField}}` the actual variable being parsed is hitting the native `__toString()` method on the model. That expects a plain string returned, not a Twig_Markup object.  

---

