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.

2 Answers 2


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.


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(
       'is_safe' => array('html')
  • 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. Aug 4, 2014 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.