Fórumok

Velocity in .vm files vs. Velocity in Web Content

thumbnail
Dominik domih, módosítva 12 év-val korábban

Velocity in .vm files vs. Velocity in Web Content

Junior Member Bejegyzések: 57 Csatlakozás dátuma: 2008.11.10. Legújabb bejegyzések
I have a question to using velocity in liferay. There are two (or possibly more) ways to use velocity in liferay.
  • Velocity in .vm files for the main theme layout
  • 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, módosítva 12 év-val korábban

RE: Velocity in .vm files vs. Velocity in Web Content

Liferay Master Bejegyzések: 894 Csatlakozás dátuma: 2009.02.18. Legújabb bejegyzések
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
thumbnail
David H Nebinger, módosítva 12 év-val korábban

RE: Velocity in .vm files vs. Velocity in Web Content

Liferay Legend Bejegyzések: 14916 Csatlakozás dátuma: 2006.09.02. Legújabb bejegyzések
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.
thumbnail
Dominik domih, módosítva 12 év-val korábban

RE: Velocity in .vm files vs. Velocity in Web Content

Junior Member Bejegyzések: 57 Csatlakozás dátuma: 2008.11.10. Legújabb bejegyzések
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
velocityContext.put("groupName", GroupLocalServiceUtil.getGroup(groupId).getName()); 
to the transform method.

Now I could do
$groupName

to get the community name in wcm with velocity emoticon

cheers
Paro Nasser, módosítva 12 év-val korábban

RE: Velocity in .vm files vs. Velocity in Web Content

New Member Bejegyzések: 15 Csatlakozás dátuma: 2012.01.12. Legújabb bejegyzések
Hi Dominik,

can you please say what exactly you do or post your JournalVmUtil.java?
thumbnail
Dominik domih, módosítva 12 év-val korábban

RE: Velocity in .vm files vs. Velocity in Web Content

Junior Member Bejegyzések: 57 Csatlakozás dátuma: 2008.11.10. Legújabb bejegyzések
Look at the transform(..) - method in this file. There are already some lines starting with
velocityContext.put("variableName", variableValue)

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



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


Deploy the bundle and create a new wcm template with
$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, módosítva 12 év-val korábban

RE: Velocity in .vm files vs. Velocity in Web Content

New Member Bejegyzések: 15 Csatlakozás dátuma: 2012.01.12. Legújabb bejegyzések
Hi Dominik,

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

package com.liferay.portlet.journal.util;

import com.liferay.portal.kernel.xml.Element;
import com.liferay.portlet.journal.TransformException;

import java.util.List;
import java.util.Map;

/**
 * @author	  ......
 */
public class JournalVmUtil {

	public static List<templatenode> extractDynamicContents(Element parent)
		throws TransformException {

		return _instance._velocityTemplateParser.extractDynamicContents(
			null, parent);
	}

	public static String transform(
			Map<string, string> tokens, String viewMode, String languageId,
			String xml, String script)
		throws TransformException {

		return _instance._velocityTemplateParser.transform(
			null, tokens, viewMode, languageId, xml, script);
	}

	private JournalVmUtil() {
		_velocityTemplateParser = new VelocityTemplateParser();
	}

	private static JournalVmUtil _instance = new JournalVmUtil();

	private VelocityTemplateParser _velocityTemplateParser;

}</string,></templatenode>


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


Thanks 4 help!
thumbnail
Bhupesh Nagda, módosítva 12 év-val korábban

RE: Velocity in .vm files vs. Velocity in Web Content

New Member Bejegyzések: 4 Csatlakozás dátuma: 2012.03.16. Legújabb bejegyzések
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??
thumbnail
Dominik domih, módosítva 12 év-val korábban

RE: Velocity in .vm files vs. Velocity in Web Content

Junior Member Bejegyzések: 57 Csatlakozás dátuma: 2008.11.10. Legújabb bejegyzések
@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