Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Generate XML using Sightly

Avatar

Level 2

Hi,

I would like to use Sightly to generate XML and properly XML escape the text. So what I would like to do is

<myTag>${properties.text @context='xml'}</myTag>

but this does not seem to be possible.

I have seen that in Sightly one has access to some Java backed objects like 'xssAPI'. So now I would like to use the method xssAPI.encodeForXML but I do not see how I could call this method in Sightly.

Any help how to do xml escaping in sightly is appreciated.

Thanks,
Ronald

1 Accepted Solution

Avatar

Correct answer by
Employee

hi ronald,

thanks for your feedback.

I raised the following issue for the XML-support: https://issues.apache.org/jira/browse/SLING-4260

Furthermore I know that it is on the roadmap to be able to call methods with parameters within Sightly.

Here an example how you can solve this via Javascript and Sightly:

<myTag data-sly-call="${ xmlEncode @ valueToEncode = 'yourValue'}"></myTag> <template data-sly-template.xmlEncode="${ @ valueToEncode}"> <div data-sly-unwrap data-sly-use.encode="${ 'xmlEncoding.js' @ xssApi = xssApi, valueToEncode=valueToEncode}">${encode.encodedValue}</div> </template>

Javascript-file:

"use strict"; use( function () { return { encodedValue: this.xssApi.encodeForXML(this.valueToEncode) }; });

best,
Feike

View solution in original post

6 Replies

Avatar

Employee

Hi ronald,

You can only use the following options for context: http://docs.adobe.com/docs/en/aem/6-0/develop/sightly.html#Display Context

In your case you need to create a Java-class that will call the xssApi, and use this via the Use-api (data-sly-use).

best,
Feike

Avatar

Level 2

Hi Feike,

thanks for your reply. That is what I feared and makes me want to use JSP.

I basically want to create an XML view of all of my components so as far as I can see I would need to create a Java class for each and every one of them just to XML encode the output.

I think it would be great if

a) One could create ones own display contexts to be used in Sightly

b) One could call methods in Sightly expressions

I do not quite understand why in the Sightly documentation "xssAPI" is mentioned as a standard Java backed objects as I do not see how it could ever be used in a Sightly template.

Thanks again and all the best,

Ronald

Avatar

Employee

hi ronald,

I will raise internally the point that we have to support @ context='xml'.

Still you don't have to write a java class for every component.

Via data-sly-template you can just use one Java-class that is then used in multiple components/templates.

best,
feike

Avatar

Level 2

Hi Feike,

thanks for your feedback. I see that I could write a single Java class and re-use it for every component. I just think that it will grow and get unwieldy pretty fast.

At the end of the day I really think that Sightly should support the execution of methods in expressions - same as JSP does.

All the best,

Ronald

Avatar

Correct answer by
Employee

hi ronald,

thanks for your feedback.

I raised the following issue for the XML-support: https://issues.apache.org/jira/browse/SLING-4260

Furthermore I know that it is on the roadmap to be able to call methods with parameters within Sightly.

Here an example how you can solve this via Javascript and Sightly:

<myTag data-sly-call="${ xmlEncode @ valueToEncode = 'yourValue'}"></myTag> <template data-sly-template.xmlEncode="${ @ valueToEncode}"> <div data-sly-unwrap data-sly-use.encode="${ 'xmlEncoding.js' @ xssApi = xssApi, valueToEncode=valueToEncode}">${encode.encodedValue}</div> </template>

Javascript-file:

"use strict"; use( function () { return { encodedValue: this.xssApi.encodeForXML(this.valueToEncode) }; });

best,
Feike

Avatar

Level 2

Hi Feike,

thanks for raising the issue and for your suggested work around. I appreciate it.

All the best,

Ronald