bodington template development

Thu, Nov 24, 2005

As it’s not possible to use JSP in Bodington, you have to do things differently if you want dynamic functionality in your templates. This little page will show you how to display “Hello World”, dynamically, from a Bodington template and also have it localised, so if you’re logged in to Bodington and your preferred language is Gaelic, you should get “F?†ilte dhan t-saoghal” instead!

There are a couple of things you’ll need:

At the moment, the class can only be org.bodington.servlet.facilities.Facility, i.e. there’s no way for a Bodington template to call a utility class that you might implement and bung in the CLASSPATH. So, you’ll have to bung new code into Facility. More code in Facility? Surely not? Is there any room left in there?

Now, pick a template, any template. In fact, let’s create one from scratch. Let’s call it helloworld.html and stick it in templates/style_default/default. All it will do is display a localised “Hello World” message, no style sheets, nothing fancy, just a template and a message. Here it is:

<?xml version=“1.0” encoding=“UTF-8” standalone=“no”?> <!DOCTYPE template PUBLIC “xhtmltemplate1-strict.dtd” “http://bodington.org/dtd/xhtmltemplate1-strict.dtd">

<template facilityclass=“org.bodington.servlet.facilities.Facility” resources=“res.templates.style_default.default.left,res.minerva.minerva_strings”>   <html>     <body>

    </body>   </html> </template>

The stuff in bold is the stuff that we’re dealing with. The rest is just standard template stuff. So let’s explain how the stuff in bold works.

<call> This is the top level node which holds all the info Bodington will need to connect the template to the class that does the output for the template.

<target> This tells Bodington about the target of the call. The “method” attribute is the method name in Facility that you want to call.

<parameters> This tells Bodington what parameters to pass to the method defined in <target>. We’re passing two parameters in this case, “request” and “writer”. These are translated to their Bodington equivalents: “request” = org.bodington.servlet.Request “writer” = java.io.PrintWriter

That’s all we need in the template. Now we’ll have to write some code in Facility to output the message to the browser. Let’s call the method sayHelloWorld:

package org.bodington.servlet.facilities;

public class Facility extends java.lang.Object {

  // Method that helloworld.html will call   public void sayHelloWorld(Request req, PrintWriter writer) {     // As the message will be localised, we need to know who the user is…     User currentUser = (User)BuildingContext.getContext().getUser();

    // …and load the correct language strings     Localiser strings = new Localiser(“res.hello_world.strings”, currentUser.getLanguage());

    // Now output a localised “Hello World” to the browser     writer.println(strings.getString(“hello.world”));   } It’s as simple as that!

comments powered by Disqus