velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Lloyd-Newberry <new...@yahoo.com>
Subject Re: Formating data with Velocity
Date Sun, 15 Jul 2001 15:25:18 GMT
--- "Geir Magnusson Jr." <geirm@optonline.net> wrote:
> One Way wrote:
> > 
> > Is there a way to 'compile' a Velocity page (for the purpose of
> > verifying it for errors) without loading the page in to the
> browser?
> > 
> 
> Actually, there is - and we can do something better.  There is a
> class 
> org.apache.velocity.test.misc.Test that takes as a command line

  I have actually implemented something like this for the company I am
working for. I will double check to make sure I can release it the code
but the basics are as follows.

  I wanted a way for the HTML jockies to lay out and design pages
without even having to have the servlets written since everywhere I
have worked there has been a great deal of pain where developers write
servlets and then have to change them multiple times a day because the
interface designers change their mind at a whim.

   I dabbled with using XML as a way to define "beans" that could be
loaded into a velocity context and merged against a template. I soon
gave up and came up with the idea of creating what I call meta-objects.
These objects are basicly backed by a hashtable and take advantage of
the velocity ability (a really cool one) to have $object.name be the
same as $object.getName(). The long and short of it is I created a meta
object and some other objects (which I will try to get to the group)
and put them in the velocity context in a command line app that then
allows me to merge documents on the command line and generate output. I
first merge the "data" file which contains the defs of the meta-objects
and then merge the appropriate templates.

   I am currently using this framework as a code generator since it
allows me to "easily" define the business objects that our system uses
as well as some interfaces between these objects and databases and the
system in general. Velocity makes possible something that has the
potential of not only saving our web developers time down the road but
also releaving stress on our developers and saving us a TON of time.

   I will paste a sample data file and a sample template to view it
below. I basicly have a parser that will merge multiple templates one
after another with the option of popping the last context in between
merges and a couple of utility classes to do the metaobject modeling. I
will t ripple check to make sure that I can release the code (it is
really easy to write if I cant) so that the Velocity community as a
whole can use it. Are people interested?

-Brian

Brian S. Lloyd-Newberry
newbeb@yahoo.com
Vertical Learning Curve Solutions



------------------------------------------------------------------
File Custlist.vdata
------------------------------------------------------------------
#*
 * Set up a velocity dataset for testing.
 * We assume that we have an instance of the ObjectInstance class in
the
 * Velocity context as INSTANCEGEN.
 *#


## *******************
## Data setup
## *******************

## Create a collection to hold customers
#set( $customerList = $INSTANCEGEN.newCollection() )

## Create a customer (don't use a macro this time...)
#set( $customer = $INSTANCEGEN.newInstance( "Customer" ) )
## Setup the customer
$customer.set( "name", "Bob" )
$customer.set( "age", 14 )
$customer.set( "sex", "Male" )
## Add a customer to the collection
$customerList.add( $customer )


## make a macro that makes this all easier
## *******************
## Macro defs
## *******************
#macro( newCustomer $name $age $sex $collection )
  ## Create a customer
  #set( $customer = $INSTANCEGEN.newInstance( "Customer" ) )
  ## Setup the customer
  $customer.set( "name", $name )
  $customer.set( "age", $age )
  $customer.set( "sex", $sex )
  $collection.add( $customer )
#end


## Now create customers using a macro
#newCustomer( "Bill" "34" "Male" $customerList )
#newCustomer( "Pam" "23" "Female" $customerList )

------------------------------------------------------------------
File Custlist.vtmpl
------------------------------------------------------------------
## A "sample" template to show customer information.
Hello!

#foreach( $customer in $customerList )
The customer's name is "$customer.name".<BR>
They are now $customer.age years old.<BR>
<BR clear=all>
#end

Goodbye!

------------------------------------------------------------------
Processor Output
-------------------------------------------------------------------
Hello!

The customer's name is "Bob".<BR>
They are now 14 years old.<BR>
<BR clear=all>
The customer's name is "Bill".<BR>
They are now 34 years old.<BR>
<BR clear=all>
The customer's name is "Pam".<BR>
They are now 23 years old.<BR>
<BR clear=all>

Goodbye!



__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

Mime
View raw message