Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Content & Markdown

Escaping html in twig

Started by Mike 9 years ago · 6 replies · 3915 views
9 years ago

I am using the import plugin to import the contents of json files into pages.

In order for the json to render fully I am having to escape, as per the twig docs at https://twig.symfony.com/doc/2.x/filters/escape.html

{{ page.header.imports.ex_config|json_encode(constant('JSON_PRETTY_PRINT')) | escape }}

This is giving me a problem in that the urls, html and xml which is included in the json is now being rendered with unwanted escape characters, for example a url is being rendered with unwanted backslashes:

"serverPublicAddress": "http:\/\/127.0.0.1:8000",

Is there any way around this? Is it possible to customize the escape strategy to prevent it happening?

9 years ago

try this:

TWIG
{{ page.header.imports.ex_config|json_encode(constant('JSON_PRETTY_PRINT') b-or constant('JSON_UNESCAPED_SLASHES'))|raw }}
👍 1
9 years ago

Yes @paul that has worked. Thank you 🙂

9 years ago

Hello again @paul

I have just tested this further and it is giving me a problem with some json parameters which contain xml. This xml content has escaped backslashes which must be retained...

Can you think of a fix for this?

9 years ago

Hi @paul

I got this working by using

{{ page.header.imports.ex_config|json_encode(constant('JSON_PRETTY_PRINT') b-or constant('JSON_UNESCAPED_SLASHES'))|e('html') }}

I am now trying to achieve same thing with importing yaml files

I am using:

{{ page.header.imports.file1|yaml_emit(constant('YAML_ANY_ENCODING')) }}

but the xml metadata field which, in the original file, is this:

TXT
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><EntityDescriptor entityID="your_subdomain.com" xmlns="urn:oasis:names:tc:SAML:2.0:metadata"><SPSSODescriptor AuthnRequestsSigned="false" WantAssertionsSigned="true" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"><NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress</NameIDFormat><AssertionConsumerService index="1" Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://accountname.com/access/saml"/></SPSSODescriptor></EntityDescriptor>

Is coming out as:

TXT
metadata: urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress

I am using the documentation at http://php.net/manual/en/function.yaml-emit.php

Would you know how to control the escaping for the yaml file?

9 years ago

I've actually mostly answered my own question for this here:

/forum/content-markdown/escaping-html-when-importing-yaml-t7932?u=koremike

Still having an issue with a >- block chomping indicator being removed

Suggested topics

Topic Participants Replies Views Activity
Content & Markdown · by Jochen, 8 months ago
6 97 8 months ago
Content & Markdown · by Ton Haarmans, 1 year ago
10 185 1 year ago
Content & Markdown · by Jan L'Am, 1 year ago
4 148 1 year ago
Content & Markdown · by Leonardo, 1 year ago
3 61 1 year ago
Content & Markdown · by belthasar, 1 year ago
4 255 1 year ago