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

Not able to set meta fields coming from Java class

Avatar

Level 2

Hi,

I am getting five values from my view helper(class extending WCMUse), these fields are meta, property1, property2, value1, value2 . My purpose is to set these values in metatag like: <meta  property1=value1  property2=value2 /> .Code doing this in Sightly component is:

<ul data-sly-list.metafield="${metaBean.metaArray}">

   <${metafield.meta}  ${metafield.property1}=${metafield.value1} ${metafield.property2}=${metafield.value2} />

</ul>

But while doing so it is getting set as

<${metafield.meta} ${metafield.property1}="TagName1" ${metafield.property2}="TagValue1"/>

Any help on how can i set it properly ?

Regards,

Surendra

1 Accepted Solution

Avatar

Correct answer by
Employee

I found the way to do it:

<element data-sly-use.meta="com.adobe.examples.htl.core.attributes.DynamicAttributes" data-sly-element="${meta.elementName @ context='unsafe'}" data-sly-attribute="${meta.attributes}"></element>

full example here htl-examples/DynamicAttributes.java at master · heervisscher/htl-examples · GitHub

View solution in original post

13 Replies

Avatar

Level 5

seems like value of "metafield.value1" is "TagName1" and "metafield.value2" is "TagValue1".

If you want to get four different properties in single tag, you can create a POJO class containing these properties as variable and their getter, setter. Create a List of type this class(e.g. metaArray) in your WCMUse pojo set the properties and return it.

OR, if only one meta tag is required you can simply  set the values in four different variables in WCMUse pojo and fetch them in your html.

Avatar

Employee

Can try first with data-sly-element to set the element name.

HTL Block Statements

Avatar

Employee

Also for the attributes, you can use data-sly-attribute, and pass in a Map with the attributes.

Avatar

Level 5

Hi Surendra,

I have few questions and options.

1) I think you are trying to generate meta tags then why would you need ${metafield.meta}. I think it will always be a meta tag.

2) Instead of list of properties,you can generate the meta tags in wcmusepojo and add to list as string.

3) Now you can loop over the string and show as html.

<sly data-sly-list.generatedMetaString ="${metaBean.metaArray}">

  ${generatedMetaString @ context='html'}

</sly>

Note: Seems like data-sly-element="${'meta'}" is not working for some reason. Every other tag is working fine.

data-sly-attribute will be useful if you pass object directly like

someobject = {'property1':'value1','property2':'value2'}

<meta data-sly-attribute="${someobject}"/>

Avatar

Level 2

Hi Susheel,

Thanks for replying

For question(1): I do need ${metafield.meta} because it can be meta or link or anything else updated in future.

For question(2): I tried sending List of String from ViewHelper, below code is doing that

                    <sly data-sly-list.metafield="${metaBean.metaArray}">

                        ${metafield.completeMeta} 

                    </sly>  (here completeMeta is evaluating to <meta property=TagName1 content=TagValue1/>

                    But doing this is causing <meta property=TagName1 content=TagValue1/> to appear on the page and

                    page source is looking something like   &lt;meta property=TagName1 content=TagValue1/&gt;

Any further suggestions?

Regards,

Surendra

Avatar

Level 5

You should pass the context as html.

  ${metafield.completeMeta @ context='html'}

Avatar

Employee

Why not use data-sly-attribute? This is designed for it...

Avatar

Level 2

susheel : adding @context='html' is causing values not to appear both on the page as well as in the page source.

Feike Visser​ : Earlier I had tried with data-sly-element with following sightly code:

                          <${metafield.meta}  ${metafield.property1}=${metafield.value1} ${metafield.property2}=${metafield.value2} data-sly-                                     element="${metafield.meta @context='unsafe'}"></sly>     It was producing below output on the page source.

                          <meta ${metafield.property1}="TagName1" ${metafield.property2}="TagValue1">  .

                           I was not getting the values of ${metafield.property1} and ${metafield.property2}. Currently tried to find out how data-                              sly-attribute can be levaraged, but no success.

                       Any further hints?

Avatar

Level 5

You need to construct an object in backend and pass it this way if you want to use data-sly-attribute.

Attributes cannot be directly given if its dynamic. Thats the reason its showing whatever expression you give.

someobject = {'property1':'value1','property2':'value2'} 

<sly data-sly- element="${metafield.meta @context='unsafe'}" data-sly-attribute="${someobject}"></sly>

Avatar

Level 5

As you have everything in metafield.

You might try to create a list of maps, where metafield.meta is the key.

and value is another map object which contains your attributes.

Something like below roughly.

Map<String,String> attributes = new HashMap<>();

map.put("property1","value1");

map.put("property2","value2");

list.add(metaelement,attributes);

<sly data-sly-list.metafield="${metaBean.list}">

    <sly data-sly-element="${key @context='unsafe'}" data-sly-attribute="${metafield[key]}"></sly>

</sly>

Avatar

Correct answer by
Employee

I found the way to do it:

<element data-sly-use.meta="com.adobe.examples.htl.core.attributes.DynamicAttributes" data-sly-element="${meta.elementName @ context='unsafe'}" data-sly-attribute="${meta.attributes}"></element>

full example here htl-examples/DynamicAttributes.java at master · heervisscher/htl-examples · GitHub

Avatar

Level 2

It worked for me too

Thanks a ton !!