extra 5 minutes of fun; JRuby and Velocity
As promised, this time I am showing you a very simple way to play with Velocity templating language in JRuby.
jvel.rb# A simple JRuby Velocity integration demo
# Notes:
# - save this script as 'jvel.rb'.
# - in the folder from where you're executing this simple script, you'll
# have a subfolder: 'lib', containing the Velocity library:
# lib/velocity-1.6.2-dep.jar
# This jar is intended to be used when you do standalone development
# with Velocity. And assuming that you have jruby in the path, you can
# run the script:
#
# $ jruby jvel.rb World
#
# You'll see: Hello World!
# -----------
require 'java'
Dir["lib/*.jar"].each { |jar| require jar }
class JRubyVelocity
include_class 'java.io.StringWriter'
include_class 'org.apache.velocity.app.Velocity'
include_class 'org.apache.velocity.VelocityContext'
def render ( arg)
Velocity.init
writer = StringWriter.new
ctx = VelocityContext.new( {'name' => arg})
Velocity.evaluate( ctx, writer, "log", "Hello ${name}!")
return writer.getBuffer
end
end
puts JRubyVelocity.new().render( ARGV.pop)
Good luck!
