Forums

Home » Liferay Portal » English » 2. Using Liferay » General

Combination View Flat View Tree View
Threads [ Previous | Next ]
Dominik domih
Velocity in .vm files vs. Velocity in Web Content
August 24, 2011 6:03 AM
Answer

Dominik domih

Rank: Junior Member

Posts: 56

Join Date: November 10, 2008

Recent Posts

I have a question to using velocity in liferay. There are two (or possibly more) ways to use velocity in liferay.
  1. Velocity in .vm files for the main theme layout
  2. Velocity in Web Content Templates to create Web Content forms


My need is to get the Community Name within a web content display. So I found $community_name in the theme/classic/templates/navigation.vm file. When trying to reuse the variable within a Web Content Template it will not be parsed. What am I doing wrong or is this a misunderstanding with a velocity context. Isn't it portal wide the same?

Can somebody explain me please or is there another way to display the community name within a web content display?

Thanx in advance...
Oliver Bayer
RE: Velocity in .vm files vs. Velocity in Web Content
August 24, 2011 6:48 AM
Answer

Oliver Bayer

Rank: Liferay Master

Posts: 875

Join Date: February 18, 2009

Recent Posts

Hi Dominik,

there are two java classes in which you define which data is accessible in vm by using a given parameter name.

  • VelocityVariables (all variables defined here are accessible in theme vm's)
  • JournalVmUtil (variables which can be used inside journal templates)


The variable "groupId" should be set inside journal templates. You should be able to use it to retrieve the group object and then output the group name.

HTH Oli
David H Nebinger
RE: Velocity in .vm files vs. Velocity in Web Content
August 24, 2011 7:14 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7252

Join Date: September 1, 2006

Recent Posts

In order to use velocity, you give it a template file and also properties (beans) to use when processing the template. So you're going to be limited in your template to the beans that are defined.

It's quite possible that although you cannot access the value directly as the theme templates can, that you might be able to get there by navigating through the beans that are available.
Dominik domih
RE: Velocity in .vm files vs. Velocity in Web Content
March 16, 2012 9:55 AM
Answer

Dominik domih

Rank: Junior Member

Posts: 56

Join Date: November 10, 2008

Recent Posts

Hi Guys!

Thanks for your response. It was quite helpful to find out which options i have. In the end I simply extended the JournalVmUtil.java by
1velocityContext.put("groupName", GroupLocalServiceUtil.getGroup(groupId).getName());
to the transform method.

Now I could do
1$groupName

to get the community name in wcm with velocity emoticon

cheers
Paro Nasser
RE: Velocity in .vm files vs. Velocity in Web Content
March 2, 2012 5:20 AM
Answer

Paro Nasser

Rank: New Member

Posts: 15

Join Date: January 12, 2012

Recent Posts

Hi Dominik,

can you please say what exactly you do or post your JournalVmUtil.java?
Dominik domih
RE: Velocity in .vm files vs. Velocity in Web Content
March 16, 2012 9:56 AM
Answer

Dominik domih

Rank: Junior Member

Posts: 56

Join Date: November 10, 2008

Recent Posts

Look at the transform(..) - method in this file. There are already some lines starting with
1velocityContext.put("variableName", variableValue)

You can assign any name for variableName. This can be used directly in the template by
1$variableName



For example: You declare in JournalVmUtil.java
1String xy = "a string";
2//now lets forward this string to our template engine so we can use it in a Web Content Template
3velocityContext.put("myString", xy);


Deploy the bundle and create a new wcm template with
1$myString


Save the template, create a web content based on this template. The page with your wcm content will return
a string


Edit: I'm using LR 5.2.3 for this...
Paro Nasser
RE: Velocity in .vm files vs. Velocity in Web Content
March 8, 2012 2:35 AM
Answer

Paro Nasser

Rank: New Member

Posts: 15

Join Date: January 12, 2012

Recent Posts

Hi Dominik,

somehow my JournalVmUtil.java look different, not how you discript. The JournalVmUtil is under portal-impl/src/com.liferay.portlet.journal.util ?

 1package com.liferay.portlet.journal.util;
 2
 3import com.liferay.portal.kernel.xml.Element;
 4import com.liferay.portlet.journal.TransformException;
 5
 6import java.util.List;
 7import java.util.Map;
 8
 9/**
10 * @author      ......
11 */
12public class JournalVmUtil {
13
14    public static List<TemplateNode> extractDynamicContents(Element parent)
15        throws TransformException {
16
17        return _instance._velocityTemplateParser.extractDynamicContents(
18            null, parent);
19    }
20
21    public static String transform(
22            Map<String, String> tokens, String viewMode, String languageId,
23            String xml, String script)
24        throws TransformException {
25
26        return _instance._velocityTemplateParser.transform(
27            null, tokens, viewMode, languageId, xml, script);
28    }
29
30    private JournalVmUtil() {
31        _velocityTemplateParser = new VelocityTemplateParser();
32    }
33
34    private static JournalVmUtil _instance = new JournalVmUtil();
35
36    private VelocityTemplateParser _velocityTemplateParser;
37
38}


there is no line with
1velocityContext.put("variableName", variableValue)


Thanks 4 help!
Bhupesh Nagda
RE: Velocity in .vm files vs. Velocity in Web Content
March 16, 2012 1:30 AM
Answer

Bhupesh Nagda

Rank: New Member

Posts: 4

Join Date: March 16, 2012

Recent Posts

Hi
I am a bit new to this velocity technology, I want to add some images to the existing(classic) theme and work on the look and feel . I am able to edit the CSS through custom.css but when I add image through HTML in portal-normal.vm file image is not shown in the portal. i am adding the following code in portal-normal.vm

<img src="../images/layout/top-header.jpg" alt ="Sorry Image Not Available"/>

is there any specific folder in which I should keep the image file?? i tried various paths but its not working. Also do i need to change some code in the velocity file other than adding the <img> tag??
Dominik domih
RE: Velocity in .vm files vs. Velocity in Web Content
March 16, 2012 10:10 AM
Answer

Dominik domih

Rank: Junior Member

Posts: 56

Join Date: November 10, 2008

Recent Posts

@paro: I was using 5.2.3 for this. Maybe that has changed since LR 6

@Bhupesh: I suggest to deactivate the caching so the server compiles your .vm files every time they are changed. Search the forum for more information when switching to liferay developer mode! e.g. here