Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Read custom property from jcr:content through sling model

Avatar

Level 2

Hi Team,

 

I am trying to read custom property from jcr:content through sling model it is working as expected.

properties.get("xyz:description", String.class)) 

 

but xyz:description having html content which is having all html tags.

 

Ex:-xyz:description

<div class="ExternalClassA70F215EC26842B6AB7B672B13D0276A"><p>​With improved upfront , circle<sup>TM</sup> ABC-01 enhances flavors and offers strong synergy with stevia and <b>HFCS</b> <u></u>.<br></p></div>

 

but when I read through properties.get("xyz:description", String.class)), I am getting plain text , superscirpt, bold are not applied in the text.

.

Could you please suggest how to read html content from jcr:content properties.

 

Thanks,

Chandra

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

I am getting complete html. In sling model use property as:

@ValueMapValue
	private String text;
@Override
    public String getText() {
        return text;
    }

At Ui side, Use below to render:

<sly data-sly-use.model="<your-slingmodel">
${model.text @context='html'}

 Hope this helps!

View solution in original post

6 Replies

Avatar

Community Advisor

Hi @chandrareddy, I just checked on my local and I was able to get the HTML markup using properties.get("propertName", String.class) using HTL as ${item.propertyName}.
When I added the context as HTML - ${item.propertyName @ context = "html"}, I was able to output the HTML on to the page.

Please check the HTL context you're passing - https://github.com/adobe/htl-spec/blob/master/SPECIFICATION.md#121-display-context

- Jineet

Avatar

Community Advisor

Hi @chandrareddy , Please use Sling API instead of JCR API to read properties in Models. I give you this piece of code for your reference and hope this helps!

// Injecting property value using ValueMap    
    @ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
    @Nullable
    private String text;
@Override
    public String getText() {
        return text;
    }



Here is how you write in HTL.

<div class="text">${properties.text @context='html'}</div>

Regards,

Aditya.ch

 

Thanks,

Aditya Chabuku

Avatar

Level 2

As @Jineet_Vora to see the html take effect you need to use the context html -> @ context = "html"

Avatar

Correct answer by
Community Advisor

I am getting complete html. In sling model use property as:

@ValueMapValue
	private String text;
@Override
    public String getText() {
        return text;
    }

At Ui side, Use below to render:

<sly data-sly-use.model="<your-slingmodel">
${model.text @context='html'}

 Hope this helps!