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?
Velocity is a Java-based templating engine. It's an open source web framework designed to be used as a view component in the MVC architecture, and it provides an alternative to some existing technologies such as JSP. Velocity can be used to generate XML files, SQL, PostScript and most other text-based formats.
Velocity templates were deprecated in Liferay Portal 7.0 and are now removed in favor of FreeMarker templates in Liferay DXP 7.2.
1 answer. Or in your Java code, try: URL url = new URL("image. png");
To use, either add this pair of macros to your template, or better yet, to your VM_Global_library. vm file. Then from the template you want to debug, you can simply add #showDebugPopup() to the bottom of the template you want to debug, and it will pop up a window with debugging information.
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());
}
I searched for hours on the same question, finally found the unit test code that shows everything necessary.
http://svn.apache.org/repos/asf/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/StringResourceLoaderRepositoryTestCase.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With