RE: Get value of select field in Application Display Template

Rajasree N, modified 5 Years ago. New Member Post: 1 Join Date: 8/14/19 Recent Posts
Hi Everyone,I am working with Liferay -7.0. I have a Select Field in my structure, where I can select multiple options. I am trying to get the values of the field in Application Display Template (ADT). I am able to get the values of other text fields using saxReader but not able to get the value of select field.  This is the sample code I have written in ADT
<#if entries?has_content>
    <#list entries as curEntry>
        <#assign renderer = curEntry.getAssetRenderer()>
        <#assign className = renderer.getClassName()>
        <#assign article = renderer.getArticle() >
        <#if className == "com.liferay.journal.model.JournalArticle">
            <#assign document = saxReaderUtil.read(article.getContent()) >
            <#assign rootElement = document.getRootElement() >
            <#list rootElement.elements() as dynamicElement >
                 <#if "body" == dynamicElement.attributeValue("name") >
                    <#assign title= dynamicElement.element("dynamic-content").getText() >
                    <strong>${title} </strong>
                 </#if>
        </#if>
    </#list>
</#if>
Can anyone please help me out with the code to get value of select field in Application Display Template.Thanks in advance.
thumbnail
Mohammed Yasin, modified 5 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi ,
 For fetching field value from select you can use below code snippet, you can also use below code for fetching other value s 

&lt;#if entries?has_content&gt;
&nbsp; &nbsp; &lt;#list entries as curEntry&gt;
&nbsp; &nbsp; &nbsp; &nbsp; &lt;#assign renderer = curEntry.getAssetRenderer()&gt;
&nbsp; &nbsp; &nbsp; &nbsp; &lt;#assign className = renderer.getClassName()&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &lt;#if className == "com.liferay.journal.model.JournalArticle"&gt;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;#assign fields = &nbsp;renderer.getDDMFormValuesReader().getDDMFormValues().getDDMFormFieldValues() /&gt;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;#list fields as field&gt;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;#if field.getName() == "selectName"&gt;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;#assign selectValue = jsonFactoryUtil.createJSONArray(field.getValue().getString(locale)).getString(0) /&gt;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;${selectValue}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<!--#if-->
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;#if field.getName() == "textName"&gt;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;#assign textValue = field.getValue().getString(locale) /&gt;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;${textValue}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<!--#if-->
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<!--#list-->&nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<!--#if-->
&nbsp;&nbsp; &nbsp;<!--#list-->&nbsp;
<!--#if-->
thumbnail
Eric D, modified 5 Years ago. Junior Member Posts: 59 Join Date: 3/25/16 Recent Posts
Hi,
As Mohammed writes, you can access from ADT the attributes of the structure but it is not the right way.
It will be nice to include your web content model in your ADT.

The web content model can natively read the web content structure.

Of course, sometimes ADT must access to an attribute of a content to make a filter or an order but the asset publisher can do this and do it better.
 
Like making a Liferay Ext, you should always thinking if it is the right way to do this.

See the complete explanation in this blog :
https://liferay.dev/blogs/-/blogs/divide-and-conquer-rendering-structured-web-content-with-the-asset-publisher