Vista combinada Visión Plana Vista de árbol
Discusiones [ Anterior | Siguiente ]
toggle
Dominik Hofbauer
Velocity in .vm files vs. Velocity in Web Content
24 de agosto de 2011 6:03
Respuesta

Dominik Hofbauer

Ranking: Junior Member

Mensajes: 57

Fecha de incorporación: 10 de noviembre de 2008

Mensajes recientes

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
24 de agosto de 2011 6:48
Respuesta

Oliver Bayer

Ranking: Liferay Master

Mensajes: 889

Fecha de incorporación: 18 de febrero de 2009

Mensajes recientes

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
24 de agosto de 2011 7:14
Respuesta

David H Nebinger

Community Moderator

Ranking: Liferay Legend

Mensajes: 11793

Fecha de incorporación: 1 de septiembre de 2006

Mensajes recientes

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 Hofbauer
RE: Velocity in .vm files vs. Velocity in Web Content
16 de marzo de 2012 9:55
Respuesta

Dominik Hofbauer

Ranking: Junior Member

Mensajes: 57

Fecha de incorporación: 10 de noviembre de 2008

Mensajes recientes

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
2 de marzo de 2012 5:20
Respuesta

Paro Nasser

Ranking: New Member

Mensajes: 15

Fecha de incorporación: 12 de enero de 2012

Mensajes recientes

Hi Dominik,

can you please say what exactly you do or post your JournalVmUtil.java?
Dominik Hofbauer
RE: Velocity in .vm files vs. Velocity in Web Content
16 de marzo de 2012 9:56
Respuesta

Dominik Hofbauer

Ranking: Junior Member

Mensajes: 57

Fecha de incorporación: 10 de noviembre de 2008

Mensajes recientes

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
8 de marzo de 2012 2:35
Respuesta

Paro Nasser

Ranking: New Member

Mensajes: 15

Fecha de incorporación: 12 de enero de 2012

Mensajes recientes

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
16 de marzo de 2012 1:30
Respuesta

Bhupesh Nagda

Ranking: New Member

Mensajes: 4

Fecha de incorporación: 16 de marzo de 2012

Mensajes recientes

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 Hofbauer
RE: Velocity in .vm files vs. Velocity in Web Content
16 de marzo de 2012 10:10
Respuesta

Dominik Hofbauer

Ranking: Junior Member

Mensajes: 57

Fecha de incorporación: 10 de noviembre de 2008

Mensajes recientes

@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