geirm 00/11/02 07:28:49
Modified: examples Example.java example.vm test.sh velocity.properties
Log:
Fixed Example.java and commented it. Also the .vm didn't reflect the new #foo()
syntax for directives. Also, updated the properties file (took the one from
test/misc).
Revision Changes Path
1.2 +46 -22 jakarta-velocity/examples/Example.java
Index: Example.java
===================================================================
RCS file: /home/cvs/jakarta-velocity/examples/Example.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Example.java 2000/09/30 17:04:20 1.1
+++ Example.java 2000/11/02 15:28:33 1.2
@@ -52,48 +52,72 @@
* <http://www.apache.org/>.
*/
-import java.io.FileWriter;
-import java.io.FileOutputStream;
-
-import java.util.ArrayList;
-import java.util.Hashtable;
import org.apache.velocity.io.FastWriter;
-
+import org.apache.velocity.runtime.Runtime;
import org.apache.velocity.Context;
import org.apache.velocity.Template;
-import org.apache.velocity.Configuration;
-import org.apache.velocity.runtime.TemplateFactory;
-import org.apache.velocity.runtime.TemplateLoader;
+import java.io.FileWriter;
+import java.io.FileOutputStream;
+
+import java.util.ArrayList;
/**
- * This class the testbed for Velocity. It is used to
- * test all the directives support by Velocity.
+ * This class is a simple demonstration of how the Velocity Template Engine
+ * can be used in a standalone application.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
- * @version $Id: Example.java,v 1.1 2000/09/30 17:04:20 jvanzyl Exp $
+ * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
+ * @version $Id: Example.java,v 1.2 2000/11/02 15:28:33 geirm Exp $
*/
+
public class Example
{
public Example(String templateFile)
{
try
{
- Configuration.setPropertiesFile("velocity.properties");
- TemplateLoader loader = TemplateFactory.getLoader();
- Template template = loader.getTemplate(templateFile);
+ /*
+ * setup
+ */
+
+ Runtime.init("velocity.properties");
+ FastWriter fw = new FastWriter(
+ System.out, Runtime.getString(
+ Runtime.TEMPLATE_ENCODING));
+
+ fw.setAsciiHack(Runtime.getBoolean(
+ Runtime.TEMPLATE_ASCIIHACK));
+
+ /*
+ * Make a context object and populate with the data. This
+ * is where the Velocity engine gets the data to resolve the
+ * references (ex. $list) in the template
+ */
+
Context context = new Context();
context.put("list", getNames());
-
- FastWriter fw = new FastWriter(
- System.out, Configuration.getString(
- Configuration.TEMPLATE_ENCODING));
-
- fw.setAsciiHack(Configuration.getBoolean(
- Configuration.TEMPLATE_ASCIIHACK));
+ /*
+ * get the Template object. This is the parsed version of your
+ * template input file.
+ */
+
+ Template template = Runtime.getTemplate(templateFile);
+
+ /*
+ * Now have the template engine process your template using the
+ * data placed into the context. Think of it as a 'merge'
+ * of the template and the data to produce the output stream.
+ */
+
template.merge(context, fw);
+
+ /*
+ * flush and cleanup
+ */
+
fw.flush();
fw.close();
}
1.2 +1 -1 jakarta-velocity/examples/example.vm
Index: example.vm
===================================================================
RCS file: /home/cvs/jakarta-velocity/examples/example.vm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- example.vm 2000/09/30 17:04:20 1.1
+++ example.vm 2000/11/02 15:28:35 1.2
@@ -4,7 +4,7 @@
$this is great!
-#foreach $name in $list
+#foreach( $name in $list )
$name is great!
#end
1.2 +5 -1 jakarta-velocity/examples/test.sh
Index: test.sh
===================================================================
RCS file: /home/cvs/jakarta-velocity/examples/test.sh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- test.sh 2000/09/30 17:04:20 1.1
+++ test.sh 2000/11/02 15:28:36 1.2
@@ -1 +1,5 @@
-java -cp .:../velocity.jar Example example.vm > output
+echo "Running Example with input file 'example.vm'"
+
+java -cp .:../bin/velocity-0.4.jar Example example.vm > output
+
+echo "Output of Example is in the file 'output'"
1.3 +32 -33 jakarta-velocity/examples/velocity.properties
Index: velocity.properties
===================================================================
RCS file: /home/cvs/jakarta-velocity/examples/velocity.properties,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- velocity.properties 2000/10/22 21:44:13 1.2
+++ velocity.properties 2000/11/02 15:28:37 1.3
@@ -1,41 +1,40 @@
-######################################################
+##
# Properties for the template loader
-# Assume that properties are case sensitive
-#
+##
-# What loader to use (plug-in your custom template loader here)
-template.loader=org.apache.velocity.runtime.FileTemplateLoader
+runtime.log = velocity.log
-# Time in milliseconds to wait before checking templates for modification
-# default: 2
-template.modificationCheckInterval=2
+template.loader=org.apache.velocity.runtime.loader.FileTemplateLoader
-# Path to templates
-# default: .
+template.modificationCheckInterval = 2
+
+# #
+# Path to templates.
+# Default=current directory
+##
template.path=.
-# Whether to cache templates
-# possible values: true | false
-# default: false
+# #
+# Cache the templates?
+# #
template.cache=false
-
-# Which AST processor to use
-template.processor=org.apache.velocity.processor.javacc.JavaCCProcessor
-# Template syntax to use
-# possible values: velocity | webmacro
-# default: velocity
-template.syntax=velocity
-
-# possible values: Injector | NodeView | SinglePass
-# default: SinglePass
-template.mode=SinglePass
-
-# FastWriter encoding
-# default: UTF8
-template.encoding=UTF8
-
-# FastWriter ASCII mode
-# possible values: true | false
-# default: false
-template.asciihack=false
+# Options for the FastWriter
+template.encoding = UTF8
+template.asciihack = false
+
+
+# This is for #foreach loops. You can
+# retrieve the current number of the item
+# being operated on by using the special
+# $velocityCount variable. Here you can specify
+# what to start the counter at 0 or 1.
+counter.initial.value = 1
+
+# You can name what the counter
+# variable can be accessed as.
+counter.name = velocityCount
+
+# The value for the default content type to return
+# in VelocityServlet
+default.contentType=text/html