Apache's Velocity — getTemplate(). Actually its allowing to pass the .vm file name , can i pass the string/object here? is there any method available to pass the string/object?
Asked
Active
Viewed 9,451 times
7
-
Possible duplicate of [How to use String as Velocity Template?](http://stackoverflow.com/questions/1432468/how-to-use-string-as-velocity-template) – Toparvion Apr 18 '17 at 02:37
3 Answers
3
This is a sample code that is working for me.
Velocity version: 1.7
I use log4j as a logger.
import org.apache.log4j.Logger;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
import org.apache.velocity.runtime.resource.util.StringResourceRepository;
private static void velocityWithStringTemplateExample() {
// Initialize the engine.
VelocityEngine engine = new VelocityEngine();
engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
engine.setProperty("runtime.log.logsystem.log4j.logger", LOGGER.getName());
engine.setProperty(Velocity.RESOURCE_LOADER, "string");
engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
engine.addProperty("string.resource.loader.repository.static", "false");
// engine.addProperty("string.resource.loader.modificationCheckInterval", "1");
engine.init();
// Initialize my template repository. You can replace the "Hello $w" with your String.
StringResourceRepository repo = (StringResourceRepository) engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
repo.putStringResource("woogie2", "Hello $w");
// Set parameters for my template.
VelocityContext context = new VelocityContext();
context.put("w", "world!");
// Get and merge the template with my parameters.
Template template = engine.getTemplate("woogie2");
StringWriter writer = new StringWriter();
template.merge(context, writer);
// Show the result.
System.out.println(writer.toString());
}

Georgios Syngouroglou
- 18,813
- 9
- 90
- 92
-
can we use class resource loader with String resource loader, both together? – Asad Shakeel May 04 '20 at 09:56
2
I searched for hours on the same question, finally found the unit test code that shows everything necessary.

Brian Lowe
- 21
- 2
1
look into the StringResourceLoader

Nathan Bubna
- 6,823
- 2
- 35
- 36
-
I found one example for StringResourceLoader. http://velocity.apache.org/engine/devel/apidocs/org/apache/velocity/runtime/resource/loader/StringResourceLoader.html . But we are having our template info in string formate. we need to pass the string instead of .vm. what should i do for this scenario. please if there is any example code. – vasantharajan Jul 01 '11 at 11:49
-
1The StringResourceLoader allows you to directly add templates to your repository and then retrieve them like any other template. StringResourceLoader.getRepository().putStringResource(myTemplateName, myTemplateString); – Nathan Bubna Jul 01 '11 at 15:37
-
`StringResourceLoader.getRepository()` returns null. Is there any property to set? – Asad Shakeel May 04 '20 at 11:17
-
Been a long time. Probably should configure a VelocityEngine to use it and initialize the engine first with properties like these: resource.loader = string string.resource.loader.class = org.apache.velocity.runtime.resource.loader.StringResourceLoader – Nathan Bubna May 05 '20 at 12:57