Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

How to get the content of a flex object unescaped

twig

Solved by complanar View solution

Started by complanar 5 days ago · 4 replies · 124 views
5 days ago

I have a simple flex object and one of its properties named title contains an ampersand sign &. When I access those property in twig I always get it escaped as & even when using the |raw filter.

Example {{ object.title|raw }} renders to Foo & Bar if the content of the yaml file is title: Foo & Bar (with a literal unescaped ampersand). When I use {{ object.getProperty('title') }} in twig this comes out the same.

This may be acceptable when rendering as html but i want to output some events as ics file and want to preserve the original input.

Is there something I am missing?

last edited 09/16/26 by complanar
4 days ago

I guess somewhere on the route there is htmlspecialchars() (but I could not track it down in the source code) and I am aware that i could just replace these symbols back to it's original.

I'd rather avoid that as it seems quite silly to escape something and then turning everything back manually.

last edited 09/16/26 by complanar
3 days ago

Tracked it down a little further.

{{ object.title|raw }} yields the correct results when called inside the page template (which lists all objects), but when used inside of a partial template and input via {% include 'flex/' ~ type ~ '/object/' object_layout ~ '/ics.twig' with { object: object_context } %} it somehow seems to be escaped again.

Adding{% autoescape false %} … {% endautoescape %} around the include tag does not help.

Trying to use the render tag as in the html templates does not work somehow it yields an error: ERROR: Layout 'list-export-ical' for flex object 'events' was not found.

If I include the same template via {% include ... %} it is found …

File structure:

TXT
user/
├─ themes/
│  ├─ mytheme/
│  │  ├─ templates/
│  │  │  ├─ flex/
│  │  │  │  ├─ events/
│  │  │  │  │  ├─ collection/
│  │  │  │  │  │  ├─ export-ical.ics.twig
│  │  │  │  │  ├─ object/
│  │  │  │  │  │  ├─ list-export-ical.ics.twig
│  │  │  ├─ flex-objects-events.ics.twig
pages/
├─ share/
│  ├─ icscalendar/
│  │  ├─ flex-objects-events.md

When requesting route /share/icscalendar.ics flex-objects-events.ics.twig is called which loads the events directory collection and includes export-ical.ics.twig (for outputting the whole ics calendar) which in turn includes list-export-ical.ics.twig inside a loop over all objects of the events collection and renders one single event.

When I use {% render %} inside of export-ical.ics.twig the above mentioned error is thrown.

3 days ago Solution

Sometimes it can be so simple. I just had to wrap the whole content of list-export-ical.ics.twig in {% autoescape false %} … {% endautoescape %} as described in https://twig.symfony.com/doc/3.x/api.html#escaper-extension :

Warning

The autoescape tag has no effect on included files.

I am marking this post as solution and keep it here in case somebody has the same problem as me and is searching the forum.

👍 1
3 days ago

Weird nevertheless, that a direct raw filter {{ object.title|raw }} does not work, though.

Suggested topics

Topic Participants Replies Views Activity
Support · by Rick Beebe, 5 days ago
2 94 2 days ago
Support · by blu3prince, 2 days ago
0 45 2 days ago
Support · by alex, 3 days ago
2 85 2 days ago
Support · by Marcel, 1 week ago
1 139 3 days ago
Support · by JakobDB, 2 weeks ago
1 127 2 weeks ago