Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Sandra del Moral Lazo
Use velocity in a hook
September 18, 2013 1:49 AM
Answer

Sandra del Moral Lazo

Rank: New Member

Posts: 23

Join Date: August 7, 2013

Recent Posts

Hi,

I have a login hook and I wanna generate a code with a velocity template, but when try to get the template's file give me this error:
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'C:/Users/sdelmora/template.vm'


but the file exists because if I do it with a File and a FileReader works. ( I try it with absolute path and relative path too)

The code of function works too, because in other portlet the template is generate correctly.
This is my velocity code:
 1
 2        VelocityContext velocityContext = new VelocityContext();
 3               
 4        Velocity.setProperty("resource.loader", "webapp");
 5        Velocity.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");
 6        Velocity.setApplicationAttribute("javax.servlet.ServletContext", httpRequest.getSession().getServletContext());
 7        Velocity.init();
 8               
 9        Template tpl;
10
11                tpl = Velocity.getTemplate("C:/Users/sdelmora/template.vm");


Any idea about the problem?

Thanks!
David H Nebinger
RE: Use velocity in a hook
September 18, 2013 5:30 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7153

Join Date: September 1, 2006

Recent Posts

getTemplate() does not take a whole file path. Instead it takes a single name that represents a resource to load.

From the Velocity doco, I think you'd have to do something like:

1Properties p = new Properties();
2p.setProperty("file.resource.loader.path", "C:/Users/sdelmora");
3Velocity.init( p );


You're basically setting up velocity to use a resource from your servlet, yet you're not pulling the resource from the servlet. If you're going to use a file resource, then use a file resource.
Sandra del Moral Lazo
RE: Use velocity in a hook
September 18, 2013 7:00 AM
Answer

Sandra del Moral Lazo

Rank: New Member

Posts: 23

Join Date: August 7, 2013

Recent Posts

But I need put some parameters into the template, and for it I was doing getTemplate.

This is my code:
 1
 2    VelocityContext velocityContext = new VelocityContext();
 3    Velocity.setProperty("resource.loader", "webapp");
 4    Velocity.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");                        
 5    Velocity.setApplicationAttribute("javax.servlet.ServletContext",httpRequest.getSession().getServletContext());
 6    Velocity.init();
 7    Template tpl;
 8    tpl = Velocity.getTemplate("templatesl/template.vm");
 9    velocityContext.put("initializes", initializes);
10    Writer writer = new StringWriter();
11    tpl.merge(velocityContext, writer);   
12    System.out.println(writer.toString());
13    return writer.toString();


Do you know some documentation about Velocity in Liferay?
Exists some way to do this? Because if I do it with property I don't know how do pass the parameter and get a string.


Thank you very much.
David H Nebinger
RE: Use velocity in a hook
September 18, 2013 11:12 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7153

Join Date: September 1, 2006

Recent Posts

Your issue is not a Liferay issue, it's a developer issue. It has nothing to do with Liferay, you're just using velocity incorrectly.
Sandra del Moral Lazo
RE: Use velocity in a hook
September 18, 2013 11:55 AM
Answer

Sandra del Moral Lazo

Rank: New Member

Posts: 23

Join Date: August 7, 2013

Recent Posts

What would you say with I am not use correctly velocity?
I want generate a code dinamically with some variables and for this I made getTemplate and pass the parameter. (the code that I put in the before comment)
Exist any other way to do this? Or what I should do for use correctly velocity, because I don't understand you.
My knowledge is that velocity let create a template and put parameter I this is wanna do, I belive that is use more for theme but I think it can be correct too.

Thanks
David H Nebinger
RE: Use velocity in a hook
September 18, 2013 8:31 PM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 7153

Join Date: September 1, 2006

Recent Posts

Okay, if you actually read the velocity doco, you'll find that the getTemplate() method does not take a filesystem path. All it takes is a name. You use it like:

1tpl = Velocity.getTemplate("mytemplate.vm");


That's all.

The properties that you set control how the mytemplate.vm file is loaded.

If you use the file resource loader, the mytemplate.vm file will be searched for in the path you specify.

However, you are using the web resource loader. This attempts to find the mytemplate.vm file in the web app itself, and does not look externally in the filesystem for the template.

So you're not using it correctly because:

a) you're trying to pass a full path as the argument, and that's not supported.
b) you want the template to come from the filesystem, but the properties you're using say to load it from the web app itself.
Sandra del Moral Lazo
RE: Use velocity in a hook
September 25, 2013 11:08 PM
Answer

Sandra del Moral Lazo

Rank: New Member

Posts: 23

Join Date: August 7, 2013

Recent Posts

Thanks! I understand and it works.