You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by jtengstrand <jo...@jaybis.se> on 2008/04/06 09:57:50 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-tp16521857p16521857.html
Sent from the Velocity - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org