Thursday, 22 March 2007
Hill O' Beans
« Driving | Main | W48 »I figured I could also knock off being able to write templates for blojsom in multiple scripting languages by implementing a BSF dispatcher. This would be similar to what Roller did with a BSFRenderer. blojsom has had the ability to use multiple presentation technologies since blojsom 1.0, so it wasn't a big deal to plunk down a few lines of code to get this working.
Now you can use any of the 20 some-odd scripting languages supported in the Bean Scripting Framework to write templates for blojsom. This in addition to dispatchers for JSP, Velocity, FreeMarker, WebMacro, and Groovy.
I did also play with the JRuby engine behind the BSF dispatcher to use Ruby's ERB for templates. The only minor gotcha was having to include the JRUBY_HOME on the CLASSPATH so that the application server could find the erb.rb file. The JRuby wiki page on require and load behavior was helpful.
After that it was just a matter of configuring the BSF dispatcher for "ruby".
<bean id="ruby" class="org.blojsom.dispatcher.bsf.BSFDispatcher" init-method="init">
<property name="blojsomProperties">
<ref bean="defaultProperties"/>
</property>
<property name="servletConfig">
<ref bean="servletConfigFactoryBean"/>
</property>
</bean>
And writing a template.
require 'erb'
template = ERB.new <<-EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= $BLOJSOM_BLOG.getBlogLanguage %>">
<head>
<title><%= $BLOJSOM_BLOG.getBlogName %></title>
</head>
<body>
<%
$BLOJSOM_ENTRIES.each{|entry|
%>
<p>
<%= entry.getTitle %> <br/> <%= entry.getDescription %>
</p>
<%
}
%>
</body>
</html>
EOF
$out.println template.result(binding)
And that's it!
P.S. You can declare the BSF dispatcher multiple times in the blojsom configuration to support multiple template types. Say, for Beanshell as well.
<bean id="beanshell" class="org.blojsom.dispatcher.bsf.BSFDispatcher" init-method="init">
<property name="blojsomProperties">
<ref bean="defaultProperties"/>
</property>
<property name="servletConfig">
<ref bean="servletConfigFactoryBean"/>
</property>
</bean>
Technorati Tags: blojsom bean scripting framework jruby erb templates
[Trackback URL for this entry]

cool!