Blogging Roller

Dave Johnson on open web technologies, social software and software development

JRuby on Roller

This is just a quick follow-up to my previous post on Pluggable renderers and scripting languages in Roller. It took me a while, but I finally made JRuby code work inside a Roller page template. Here's an example JRuby page template that displays most recent blog titles and text in HTML format.

$out.println "<html><head>"
$out.println "<title>#{$model.weblog.name}</title>"
$out.println "</head><body>"
$out.println "<h1>#{$model.weblog.name}</h1>"
$model.weblogEntriesPager.entries.keySet().each {|day|
   $model.weblogEntriesPager.entries.get(day).each {|entry|
      $out.println "<h3>#{entry.title}</h3>"
      $out.println "<p>#{entry.text}</p>"
   }
}
$out.println "</body></html>"

Not the most beautiful thing in the world, I must admit. Any JRuby experts reading along? Is there a simple templating solution that will work in JRuby... something like Groovy Templates? And is there a way to map puts output to a java.io.Writer that will work via BSF?
Dave Johnson in Roller • 🕒 05:46PM Mar 20, 2007
Tags: java jruby roller
Comments:

If BSF is constraining you somehow, remove that constraint.

Posted by Patrick Mueller on March 21, 2007 at 03:21 AM EDT #

For a templating solution, have a look at ERb.

As far as getting puts to go to a java.io.Writer, you could assign to $stdout an object that looks like a Ruby IO but forwards/delegates print/write calls to the Java Writer. Or you could construct the JRuby runtime outside of BSF and explicitly set the stdout stream to your writer -- see org.jruby.Ruby.newInstance(InputStream,PrintStream,PrintStream)

Posted by Nick Sieger on March 21, 2007 at 04:09 PM EDT #

Thanks Nick and Patrick. I'll have to investigate hooking in JRuby directly; that's what I had to do for Groovy. And ERb looks like a good start -- I hope it will work in JRuby. Nick: sorry about the delay in posting your comment. The Akismet service thought it was spam.

Posted by Dave Johnson on March 21, 2007 at 05:03 PM EDT #

ERB should work fine in JRuby; it's used extensively by Rails for all its templating and view generation. You might also be interested in HPricot and HAML, two other ways to template/generate HTML that are growing in popularity. Both work on JRuby, though HPricot has a Java/C extension you have to get directly from the source (for now).

Posted by Charles Oliver Nutter on March 22, 2007 at 01:14 AM EDT #

Post a Comment:
  • HTML Syntax: NOT allowed