You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by jtengstrand <jo...@jaybis.se> on 2008/04/06 10:03:04 UTC
Macros does not work when running init() + init(properties)
Im working on a code generator, building upon Velocity. I have a problem. The
code generator needs to be able to change the "velocimacro.library" (or be
left out) every time the Velocity is initialized.
The file macro.vm looks like this:
#macro(hello)
Hello!!
#end
The file hello.vm looks like this:
#hello()
When I run the code, it successfully creates the file Hello.txt with the
content:
Hello!!
But when I activate the first line "//init(null)" the macro is not
recognized, and the output of Hello.txt is:
#hello()
The code looks like this (it is just an example to show the problem):
public static void main(String args[]) {
//init(null);
init("uparser/template/java/macro.vm");
VelocityContext velocityContext = new VelocityContext();
String filename = "C:/project/vgen/tmp/Hello.txt";
BufferedWriter out = null;
String templateFile = "uparser/template/java/hello.vm";
try {
Template template =
Velocity.getTemplate(templateFile);
out = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(filename)));
if (template != null) {
template.merge(velocityContext, out);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void init(String macrosTemplateFile) {
Properties properties = new Properties();
if (macrosTemplateFile != null) {
properties.put("velocimacro.library",
macrosTemplateFile);
}
properties.put("file.resource.loader.path",
"C:/Source/Workspace33/vgen/projects");
try {
Velocity.init(properties);
} catch (Exception e) {
System.out.println(e.toString());
return;
}
}
Any ideas?
BR,
Jocke
--
View this message in context: http://www.nabble.com/Macros-does-not-work-when-running-init%28%29-%2B-init%28properties%29-tp16521907p16521907.html
Sent from the Velocity - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Macros does not work when running init() + init(properties)
Posted by Nathan Bubna <nb...@gmail.com>.
Please don't post to both the dev and user lists. This is a usage question.
You can't change the velocimacro library setting after init(). You
will probably have to stop using the Velocity singleton and instead
create a new VelocityEngine each time you need to change an
initialization setting.
On Sun, Apr 6, 2008 at 1:03 AM, jtengstrand <jo...@jaybis.se> wrote:
>
>
> Im working on a code generator, building upon Velocity. I have a problem. The
> code generator needs to be able to change the "velocimacro.library" (or be
> left out) every time the Velocity is initialized.
>
> The file macro.vm looks like this:
> #macro(hello)
> Hello!!
> #end
>
> The file hello.vm looks like this:
> #hello()
>
> When I run the code, it successfully creates the file Hello.txt with the
> content:
> Hello!!
>
> But when I activate the first line "//init(null)" the macro is not
> recognized, and the output of Hello.txt is:
> #hello()
>
> The code looks like this (it is just an example to show the problem):
>
> public static void main(String args[]) {
> //init(null);
> init("uparser/template/java/macro.vm");
> VelocityContext velocityContext = new VelocityContext();
>
> String filename = "C:/project/vgen/tmp/Hello.txt";
> BufferedWriter out = null;
> String templateFile = "uparser/template/java/hello.vm";
>
> try {
> Template template =
> Velocity.getTemplate(templateFile);
> out = new BufferedWriter(new OutputStreamWriter(new
> FileOutputStream(filename)));
>
> if (template != null) {
> template.merge(velocityContext, out);
> }
> } catch (Exception e) {
> e.printStackTrace();
> } finally {
> if (out != null) {
> try {
> out.flush();
> out.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> }
> }
>
> public static void init(String macrosTemplateFile) {
> Properties properties = new Properties();
>
> if (macrosTemplateFile != null) {
> properties.put("velocimacro.library",
> macrosTemplateFile);
> }
>
> properties.put("file.resource.loader.path",
> "C:/Source/Workspace33/vgen/projects");
>
> try {
> Velocity.init(properties);
> } catch (Exception e) {
> System.out.println(e.toString());
> return;
> }
> }
>
> Any ideas?
>
> BR,
> Jocke
>
>
> --
> View this message in context: http://www.nabble.com/Macros-does-not-work-when-running-init%28%29-%2B-init%28properties%29-tp16521907p16521907.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org