5 minutes of fun: JRuby and StringTemplate
If you like to explore some stuff on templating, and I highly recommend StringTemplate, this is how you can do it from JRuby.
jrst.rb
# A simple JRuby StringTemplate integration demo
# Notes:
# - save this script as 'jrst.rb'.
# - in the folder from where you're executing this simple script, you'll
# have a subfolder: 'lib', containing the StringTemplate libraries:
# lib/antlr-2.7.7.jar
# lib/stringtemplate-3.2.1.jar
# run the script
# $ jruby jrst.rb World
#
# You'll see: Hello World!
# -----------
require 'java'
Dir["lib/*.jar"].each { |jar| require jar }
class JRubySringTemplate
include_class 'java.io.StringWriter'
include_class 'org.antlr.stringtemplate.StringTemplate'
def render ( arg)
st = StringTemplate.new("Hello $name$!")
st.set_attributes( {'name' => arg});
writer = StringWriter.new
writer.write( st.toString)
return writer.getBuffer
end
end
puts JRubySringTemplate.new().render( ARGV[0])
My 5 minutes of fun in a lunch break. Next time I'll show you how to use Velocity in the same way :)
Have fun!
