0

I have the following properties string

GET 50% OFF ANY M'EDIUM OR L"AR"GE PIZZA!

I am using it in an HTML onclick markup like so

onclick="trackPromoCta(encodeURI(${properties.ctaTwoTextRight @ context='text'}));"

However this outputs invalid html. I tried @context of scriptString and that escapes but only for inside JavaScript not for inside HTML markup. I tried all of the other options as well and none of them actually escape special characters for rendering HTML.

I saw someone once use a @format to search the string for these characters and escape them for HTML but I can't find out how to use @format to do this.

The expected output should be

onclick="trackPromoCta(encodeURI('GET 50% OFF ANY M'EDIUM OR L"AR"GE PIZZA!'));"

James
  • 235
  • 3
  • 15
  • Can you add some details about what you are seeing, when trying the different contexts? Seems like the 'attribute' context should work since this is going in an html attribute, but you indicated that you tried all other options and none of the worked. – Jordan Shurmer Feb 15 '18 at 13:58

2 Answers2

1

Take a look at the HTL spec for display context: https://github.com/Adobe-Marketing-Cloud/htl-spec/blob/master/SPECIFICATION.md#121-display-context

What you need is scriptString since your string property will eventually be used as a javascript string literal.

${properties.jcr:title @ context='scriptString'} <!--/* Applies JavaScript string escaping */-->

Also, you need to enclose your HTL expression with single quotes, for example:

var str = '${'this is a js string literla' @ context='scriptString'}'

The HTL code for you specific example would be:

onclick="trackPromoCta(encodeURI('${properties.ctaTwoTextRight @ context='scriptString'}'));"
Ahmed Musallam
  • 9,523
  • 4
  • 27
  • 47
0

The @context value "text", "html" or "attribute" will return encoded values in your resulting html. As per documentation too, text encodes all HTML special characters.

If you go through your html's code using "View Page Source" and not via "Inspect element of developer tools". You will see the expected outcome.

onclick="trackPromoCta(encodeURI('GET 50% OFF ANY M&#39;EDIUM OR L&#34;AR&#34;GE PIZZA!'));"

Reference: https://helpx.adobe.com/experience-manager/htl/using/expression-language.html

Utkarsh
  • 589
  • 3
  • 19
  • Sorry but that is not the case. This was using text before and the invoice event registers an error in the console because the script was malformed from a quote being in the code. – James Feb 13 '18 at 00:50
  • Additionally this code is added to the page through ContextHub targeting. – James Feb 13 '18 at 16:28
  • Best way to solve your problem is use WCMUSEPojo class and escape your properties value in that class before you render it on using HTL – Utkarsh Feb 15 '18 at 15:45