Questions tagged [liferay-velocity]

They are mostly used to either 1)Create web content in liferay in conjunction with structures to parse user populated data from structure fields and present it in a view form,using the business logic,or 2)Create theme template file for creating a skeleton structure for themes

Velocity templates are widely used in liferay themes and dynamic web content. They allow us to segregate data(provided by user in fields retrieved from structures) and business logic which is coupled with presentation layer in template. Liferay provides various variables which can be directly used to access objects available in liferay. Service layer of liferay(along with custom user services) can be accessed within a template using serviceLocator.

Usage:

To access an element from structure with name as 'image':

<img src=$image.getData()/>

where '$' is used to access an element while getData() used to access data from the element

To use conditional statements:

#if($permissionChecker.getUserId()==10198)
    $text.getData()
#end 

We can use conditional logic(if,if-else,if-elseif) to check for a particular condition and use any of the default available object variable 'permissionChecker' for condition

To use loops to iterate through siblings or children of an elements:

#foreach($currentText in $text.getSiblings())

    $currentText.getData()

#end

We can iterate through the repeatable elements or other elements in a structure by using a looping statements for the same.

To use service locator to access liferay/custom services

#set($userLocalService = $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
#set($user = $userLocalService.getUserById(10198)
$user

We can use serviceLocator to access Liferay/custome service in a velocity template and use methods from the same.

#set ($customPortletLocalService = $serviceLocator.findService("custom-portlet", "com.liferay.custom.service.CustomPortletLocalService"))

Further reference links:

1.Using structures and templates in Liferay

2.Java World:Using velocity Engines

134 questions
9
votes
1 answer

Create object in velocity template

I am writing velocity templates for my liferay theme and I am wondering, whether it is possible to create a new object inside the velocity template. The reason is that in liferay there is no contextTool registered in the context and I really want to…
Patrick Cornelissen
  • 7,968
  • 6
  • 48
  • 70
4
votes
2 answers

How to get Server Name and Server Port from Liferay velocity template?

In jsp page we can get Server Name and Server Port by using request.getServerName() and request.getServerPort().` As we can't get HttpServletRequest from Liferay velocity template, Is there any other way to get both Server Name and Server…
Boat
  • 515
  • 2
  • 8
  • 28
4
votes
2 answers

rendering Liferay page URLs inside of portlets Liferay 6.1

I'm new to liferay and I'm almost positive this is blazingly simple to do: Using velocity markup, I want to be able to generate links to pages within my Liferay website and embed them inside of my portlets on different pages. I have a vague idea…
max7
  • 800
  • 2
  • 16
  • 37
3
votes
2 answers

Sort list of objects in Velocity template - Liferay

I've created a structure in Liferay as below
Tejas Kanani
  • 133
  • 1
  • 2
  • 9
3
votes
2 answers

Liferay Portal 6.2 - How to get theme_display in Web Content Template

I am trying get the "theme_display" in a Web Content Template (on Liferay Portal 6.2). But it does not work. #set($serviceContext = $portal.getClass().forName("com.liferay.portal.service.ServiceContextThreadLocal").getServiceContext()) …
Lucas Boemeke
  • 211
  • 2
  • 6
3
votes
1 answer

How to add an existing portlet to Web Content Display using Velocity templates

I want to create a new page using Web Content Display with Velocity Templates and Structures. On my page I would like to have multiple tabs and on each tab needs to be added programatically from a template of an existing portlet. I know how to add…
user1694543
  • 31
  • 1
  • 2
3
votes
2 answers

Using custom services or liferay services in liferay themes (velocity templates)?

How to use custom services method in liferay themes in velocity files like init_custom.vm, portal_normal.vm etc. I see liferay provides a lot of variables of helper utility classes like $portalUtil for PortalUtil, $getterUtil for GetterUtil and so…
Prakash K
  • 11,669
  • 6
  • 51
  • 109
3
votes
1 answer

How to use java String.matches(str) method in Velocity Templates

I'm attempting to do a match on the user agent in velocity templates. $ua does print out but Matches! does not. What am I doing wrong? #set( $ua = $request.getHeader('User-Agent'))

$ua

#if( $ua.matches('/Windows.(NT|XP|ME|9)/'))…
old_adam
  • 31
  • 1
  • 6
2
votes
3 answers

Velocity (VM) template request parameters: Getting GET variables

How do i access GET variables as passed in the URI in a VM template? This works only when loading the widget URL: $request.get("parameters").get("fav").get(0) I'm looking for a neat solution that works with friendly URLs. Here's my testing…
Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124
2
votes
2 answers

Changes to init_custom.vm not showing up

I'm building a new Liferay theme and being crushed by this problem. When I make changes to the init_custom.vm, the only possible way for me to see the changes is to restart Tomcat. When I make other changes to my theme, such as editing my CSS or…
Rob Gonzalez
  • 579
  • 1
  • 6
  • 19
2
votes
2 answers

How to delete the Navigation Menu title in liferay theme?

I created a theme and copied the navigation.ftl from Liferay Classic Theme but in my theme, The navigation title is showing up as shown in the below screen-shot. If I am logged in as an Administrator, I am able to edit the text and make it blank…
dev_in_prog
  • 121
  • 1
  • 18
2
votes
1 answer

How to create Structure & Template programmatically in Liferay 6

I need to create the Structure and Template progrmatically through java code.I used following code snippets. Structure: public void createStructure(String userName,long userId){ log_.info("Inside create structure "); long…
2
votes
1 answer

Liferay: Dynamically populate 'select' structure field

I am building a liferay theme, which has two columns side by side. The right column is where the web contents will be displayed. My implementation aims to build a menu (left column) that gets the titles from the web content titles and builds…
chadct
  • 43
  • 1
  • 9
2
votes
3 answers

how to show values associate with a custom document in liferay 6.2

I created a document type, and I'm using Asset Publisher to display this types of documents I would like view the values that I associated with my custom document, that I created with the document library. What is the way to do this with velocity? I…
user60108
  • 3,270
  • 2
  • 27
  • 43
2
votes
1 answer

Access Liferay Global Velocity Variable in Custom Portlet

As of now, Liferay provides a set of global variables as defined in init.vm that can be used by theme templates. These variables are set in init.vm and is located inside Liferay ROOT.war : $PATH_TO_WEBAPPS/ROOT/html/themes/_unstyled/templates. And…
Runcorn
  • 5,144
  • 5
  • 34
  • 52
1
2 3
8 9