I am designing a new template for my asset publisher to get all contents with a class
com.liferay.portlet.journal.model.JournalArticle
from a specific site.
In my web content structure i have:
-Image
inside this image:
-there is tow Text variable
-one for Title
-one for Description
here is my structure code:
<root available-locales="en_US" default-locale="en_US">
<dynamic-element dataType="image" fieldNamespace="wcm" indexType="keyword" localizable="true" name="myImage" readOnly="false" repeatable="false" required="false" showLabel="true" type="wcm-image" width="">
<dynamic-element dataType="string" indexType="keyword" localizable="true" name="title" readOnly="false" repeatable="false" required="false" showLabel="true" type="text" width="small">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Title]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
<dynamic-element dataType="string" indexType="keyword" localizable="true" name="desc" readOnly="false" repeatable="false" required="false" showLabel="true" type="text" width="small">
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Description]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
<meta-data locale="en_US">
<entry name="label">
<![CDATA[Image]]>
</entry>
<entry name="predefinedValue">
<![CDATA[]]>
</entry>
<entry name="tip">
<![CDATA[]]>
</entry>
</meta-data>
</dynamic-element>
and here is how it's appears : Screenshot of my structure
Now i wrote this code in my asset template using (VM) or Velocity Macros
#if (!$entries.isEmpty())
#foreach ($entry in $entries)
#set($renderer = $entry.getAssetRenderer() )
#set($className = $renderer.getClassName() )
#if( $className == "com.liferay.portlet.journal.model.JournalArticle" )
#set( $journalArticle = $renderer.getArticle() )
#set( $document = $saxReaderUtil.read($journalArticle.getContent()) )
#set( $rootElement = $document.getRootElement() )
#foreach( $dynamicElement in $rootElement.elements() )
#if( "myImage" == $dynamicElement.attributeValue("name") )
#set( $myImage = $dynamicElement.element("dynamic-content").getText() )
#end
<img src="$myImage" >
#end
#end
#end
from this code i can reach my image, but my problem is that i cannot find a way to get the variables inside the image , one with name="title" and another with name="desc" ? So, i need to reach these tow text variables
So the simplest way to examine data from structure fields is to print the variable and look what is inside. I took structure from your post. So i've created a structure then article based on it.
Then i've printed the $myImage
variable and it looks like this:
{name=myImage, data=/image/journal/article?img_id=22009&t=1503342749476, type=image, options=[], title={name=title, data=colorful bird, type=text, options=[]}, desc={name=desc, data=this is a birdie, type=text, options=[]}}
So as you can see it contains an image, title and description so to extract them, just use(THIS IS MY TEMPLATE CODE FOR SCREEN BELOW):
$myImage
$myImage.title.getData()
$myImage.desc.getData()
Final effect with $myImage printed:
If you want a name attribute just use $myImage.title.name
, it will give you what is inside of name.
EDIT Ok, I'm sorry i misread your question. You are creating the asset publisher template not article template. So i think your way is the right way to display embedded variables. Below is your modified template that displays image, title, and description:
#foreach ($entry in $entries)
#set($renderer = $entry.getAssetRenderer() )
#set($className = $renderer.getClassName() )
#if( $className == "com.liferay.portlet.journal.model.JournalArticle" )
#set( $journalArticle = $renderer.getArticle() )
#set( $document = $saxReaderUtil.read($journalArticle.getContent()) )
#set( $rootElement = $document.getRootElement() )
#foreach( $dynamicElement in $rootElement.elements() )
#if( "myImage" == $dynamicElement.attributeValue("name") )
#set( $myImage = $dynamicElement.element("dynamic-content").getText() )
#end
#foreach( $embeddedElement in $dynamicElement.elements() )
#if( "title" == $embeddedElement.attributeValue("name") )
#set( $title = $embeddedElement.elements().get(0).getText() )
#end
#if( "desc" == $embeddedElement.attributeValue("name") )
#set( $desc = $embeddedElement.elements().get(0).getText() )
#end
#end
<br/>
<img src="$myImage" >
<br/>
<br/>
Title: $title
<br/>
Description: $desc
#end
#end
#end