Not able to set meta fields coming from Java class | Community
Skip to main content
Level 2
July 26, 2017
Solved

Not able to set meta fields coming from Java class

  • July 26, 2017
  • 13 replies
  • 3607 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Feike_Visser1

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

13 replies

Level 4
July 26, 2017

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.

Feike_Visser1
Adobe Employee
Adobe Employee
July 26, 2017

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

HTL Block Statements

Feike_Visser1
Adobe Employee
Adobe Employee
July 26, 2017

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

susheel
Level 5
July 26, 2017

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}"/>

Level 2
July 26, 2017

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

susheel
Level 5
July 26, 2017

You should pass the context as html.

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

Feike_Visser1
Adobe Employee
Adobe Employee
July 26, 2017

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

Level 2
July 26, 2017

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?

susheel
Level 5
July 26, 2017

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>

Feike_Visser1
Adobe Employee
Adobe Employee
July 26, 2017

I made a small example here:

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

You can have dynamic attributes...

I do see your issue with <meta>.