Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liferay Velocity Templates & Structures. Get Children of Children?

I'm working with Structures and Templates within Liferay 6.1. When trying to get children of rows it appears that the only way to access the information using a loop is with code similar to this:

#foreach
($this in $example.getSiblings())
<h2>$this.getData()</h2>
<p>$this.getChildren().get(0).getData()</p>
#end

Does anyone know how it would be possible to access the data for children of children? I've tried scouring through the Velocity user-guide for an example but cannot get anything to work.

If anyone could point me in the right direction or link some code that they think may work, it would be greatly appreciated.

Many thanks,

Jay.

like image 920
Jay Avatar asked Mar 09 '15 13:03

Jay


People also ask

What is velocity in Liferay Portal?

Velocity is the default template engine in Liferay Portal. Liferay extensively uses velocity templates in the theme. In the templates directory of a given theme all the template files are available. You can implement your own logic in those template files.

What is structures and templates in Liferay?

Structures and Templates is elegant feature in liferay web content management system. When we design web content some time we need same type of design in several places. Whenever we need such type of design and data multiple times in the web content then we will make that design as template.

What is a velocity template?

What's Velocity Template? A velocity template file is a file which has vm as file extension (for example porta_normal.vm). This template file contains velocity related directives, variable, loops etc. In run time velocity engine translate it into web page so that browser can understand it.

How to create a widget template for Liferay DXP/portal?

For Liferay DXP/Portal 7.4+, widget templates only support FreeMarker. Velocity is no longer supported. Follow these steps to create a Widget Template: Navigate to the desired Site where you want to create the widget template. Open the Site Menu and go to Design→ Templates→ Widget Templates. Note


2 Answers

#foreach ( $item in $allItems )
  #set( $childrenFirstLevel = $item.getChildren())
   #foreach ( $childFirstLevel in $childrenFirstLevel)
      #set( $childrenSecondlevel = $childFirstLevel.getChildren())
      #foreach ($childSecondLevel in $childrenSecondLevel)
         <p>$childSecondLevel.data</p>
      #end 
   #end    
#end

You just need nested iterations to get children of children, as it holds in every programming language.

like image 61
jkonst Avatar answered Sep 22 '22 13:09

jkonst


@jkonst is giving the correct answer. Here's some additional information for you to explore velocity more. Consider to just use $this.getChildren().getClass().getName() - this will print the class of the backing Java object, giving you more hints on what to do with it. Same with $this.getChildren().get(0).getClass().getName().

Naturally you'll use this only to explore and debug your velocity templates, but it helps a lot to get a clue what to do with the individual objects.

like image 34
Olaf Kock Avatar answered Sep 22 '22 13:09

Olaf Kock