Sunday, June 21, 2009

Etanni Templating

Etanni has replaced Ezamar as the default templating engine for Ramaze and if anything it is even simpler. There are only a very few lines of code for the entire thing shown here. Etanni really only supports two things. The first is standard Ruby interpolation such as #{@x} which can go in your template and also for sections where the result should not be put into the output stream. We've used both of these in previous posts, especially the interpolation, but we haven't really talked about them too much. I decided that I needed to learn a bit more about it after this series of emails on Etanni (ignore the Subject and especially read Michael's final post on the subject where he explains things pretty well (at least it helped me out quite a bit).

Here's a bit of code that uses both types. We'll start out with our start.rb code:

require 'rubygems'
require 'ramaze'

class MainController < Ramaze::Controller

# Use page.xhtml in the layout directory for layout
layout :page

# You can access it now with http://localhost:7000/
# All we're going to do is set the title in here and
# everything else will come from our view/index.xhtml file.
def index
# The title is used by the layout/page.xhtml file to set the title
# for the page.
@title = "Index Page"

# Some simple text to just place on the page in view/index.xhtml.
@text_1 = "Hello World"

# We'll put this text (text_2) out count (5) times.
@count = 5
@text_2 = "Goodbye World"

# We'll create a list with these elements.
@arr_title = "List Elements"
@arr = [ "some", "list", "elements" ]

# We'll use this boolean to decide what to show.
@bool = true



There are five sections in here. The first sets the title which will be interpolated in the layout/page.xhtml file. We've already seen this usage a number of times. The second piece just sets a text value to "Hello World" and this will be interpolated in the view/index.xhtml file (as will all of the rest of the pieces). In the next part we set two things a count equal to five and another string to "Goodbye World". We'll use these to print out "Goodbye World" five times in the output. Next we set an array title and an array with three elements which we'll use to generate a list in the output. Finally, we have a boolean, bool, set to true which will be used to decide which text to display.

Here's the view/index.xhtml file:

<!-- Put out the text in text_1 -->
#{@text_1} <br/> <br/>

<!-- Use count to put out text_2 count times. -->
<?r 1.upto(@count) do ?>
#{@text_2} <br/>
<?r end ?>
<br/> <br/>

<!-- Put out the array title and then each element in the array. -->
#{@arr_title} <br/>
<?r @arr.each do |a| ?>
<?r end ?>

<!-- If the bool is true then print that out otherwise print out that it was false. -->
<?r if (@bool == true) then ?>
bool was true!
<?r else ?>
bool was false!
<?r end ?>

This is pretty straight forward and mirrors the start.rb from earlier. We first put out the text in text_1. Next we use count to put out text_2 five times and then we put the arr_title and then the arr as a list. Finally, we check bool and print out whether it is true or false.

Here's the layout/page.xhtml:

<!-- view/page.xhtml -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="">
<!-- Use the page.css in the public directory and set title based on
what's set in the associated method.

<link rel="stylesheet" type="text/css" href="/page.css"/>
<div id="header">
<!-- Display the actual content. This will come from the method or the
associated view/*.xhtml file


<!-- Set the footer in the center of the screen. -->
<div id="footer" style="text-align: center;">
<h5> Powered by Ramaze </h5>

You can grab the public/page.css from an earlier post.

I'd definitely recommend reading the emails that started this with Michael's observations and also the actual code (both linked to above).

As always, if you have questions or comments, let me know.


  1. For someone new to Ruby and string interpolation, it might be good to note that the #{...} interpolation can include arbitrary Ruby code, the result of which has to_s called on it. For example:

    <p>Was bool true? #{@bool ? "Oh yes!" : "Nuh uh"}</p>
    <p>I saw #{@dog_count} dog#{:s unless @dog_count==1} at the park</p>

  2. Phrogz, Good call. Maybe I'll take some time and do a follow on post to discuss this.