RSS feed
<< Previous | Home | Next >>

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!

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!

java web frameworks? back to Rails?? :(

obviously, as a JPublish maintainer, I am biased, but if you wonder why Java is not popular among the web designers and developers, is because we: the java developers are not designing web frameworks for non-Java web developers, IMHO.

I was just reading one of the many articles about java web frameworks, example:
http://www.infoq.com/articles/modular-wicket

Everything is nice with such frameworks if .. you know Java. Wicket, and other frameworks too, is great no doubt .... but only if you know Java.

Is this how we want to attract more people to Java? By forcing them to learn the language? Annotating code? c'mon

I have similar issues with JPublish and trying to obscure as much as possible the need for knowing Java, however I am not asking the web Designers to learn Java in order to create a simple web site.

Hey Java Web Frameworks Developers, are we blind??
 
going back to Rails?

:'(
Tags :