Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

How to join text inside objects using sightly.

Avatar

Level 4

Hi All,

Just wanted to check what is best practise to join strings which are in java object.

<div class="link-list" data-sly-use.linkList="mypackage.LinkList" data-tags="${properties['cq:tags'] @join=', '}">

Now the linkList object have a Tag array in linkList.tags object, but I cannot use the linkList.tags object with join.

I don't want to use the ${properties['cq:tags']} here I wanted to get it from our object and use it. what is the best way of doing it ?

Thanks!

Shehjad

1 Accepted Solution

Avatar

Correct answer by
Employee

hi shehjad,

Can't you do this?

data-tags="${linkList.tags @join=', '}"

Otherwise can you create a Java-class, and put it in the post?

View solution in original post

9 Replies

Avatar

Employee

Have a look at the @ format option, and data-sly-test to store the temp value.

Avatar

Level 4

Hi Feike,

Thank you for your quick reply. I think my question is not that clear so adding more code level detail.

I have created a java class LinkList.java in which extends WCMUse and below two things I am exposing back to my .html code

    private transient List<ValueMap> links = new ArrayList<ValueMap>();
    private transient Tag[] tags;

Using use I am calling this java class from my .html file

<div class="link-list" data-sly-use.linkList="myPackage.LinkList" data-tags="${properties['cq:tags'] @join=', ' @ context='attribute'}">

I want to add a data-tags to my div like data-tags="properties:orientation, properties:style".

Ideally from data consistency point of view I wanted to get this string 'properties:orientation, properties:style' from my tags i.e ${linkList.tags} (Tag array exposed by my java class) rather than reading it from my currentNode which stores it like string array.

As I am pretty new to sightly I am interested in knowing how this can be done in best way? Sorry if my question is unclear :)

Thanks!

Shehjad

Avatar

Correct answer by
Employee

hi shehjad,

Can't you do this?

data-tags="${linkList.tags @join=', '}"

Otherwise can you create a Java-class, and put it in the post?

Avatar

Level 4

Hi Feike,

data-tags="${linkList.tags @join=', '}" does not work :( . I have attached the java class (Which I have modified but I am doing lot more stuff in the real file) and .html code also.

In my dialog I am using a simple tag field. 

  <tags
            jcr:primaryType="cq:Widget"
            cls="cq-propsdialog-tags"
            fieldLabel="Tags/Keywords"
            name="./cq:tags"
            xtype="tags"/>

My .html code looks like.

<div data-sly-use.linkList="myPackage.LinkList" data-tags="${properties['cq:tags'] @join=', '}">
Test code.
</div>

This is my expected output

<div data-tags="properties:orientation, properties:style">
    Test code
</div>

But I don't like using properties['cq:tags'] for join operation rather I would like to derive it from my linkList object, as now in my .html code I am reading values from 2 different source one is properties object and for other stuff I am using linkList object.

Appreciate your help on this.

Thanks!

Shehjad

Avatar

Employee

Ok, try this...

 

<div data-sly-list="${linkList.tags}" data-sly-unwrap>
   <div data-sly-test.result="${ itemList.first? '{1}' : '{0},{1}' @ format = [result, item.tagID]}" data-sly-unwrap></div>
</div>

<h2>${result}</h2>

Avatar

Employee

On a personal side I would more prefer to do this in Java or Javascript to improve re-use and readability.

Avatar

Level 4

You are Awesome! This works like a charm smiley ... Thank you so much

Thanks!

Shehjad

Avatar

Level 4

Hi Feike,

I have one more question, from the code readability point of view, is it a good idea to use data-sly-test for storing temporary variable?

Here we are doing something similar to what we use c:set in JSTL. Do you think if sightly introduce a new data-sly-set kind of tag for setting and storing temporary variable would be  a good idea?

Thanks!

Shehjad 

Avatar

Level 4

Yes Feike, we have decided to do this in our java code. Thanks for your valuable input on this.