Tuesday, June 23, 2009

Updated 2009-06-24: Ramaze and Etanni Templating (String Interpolation Version)

In a comment in my last post Phrogz suggested that perhaps I should talk a bit more about Ruby string interpolation which is used by Etanni in templating. We've only used it in very simple ways but you can put any Ruby code inside of the #{} and the result will have a to_s() called on it. We can use this to do some interesting things in Etanni without needing the form. Note that the examples I'm giving here are pretty contrived but should give you some ideas on the sorts of things you can (possibly not should) do.

Here's our start.rb:

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"

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

# The number of dogs we've seen.
@dog_count = 3



Pretty simple this time around. We set the title, whether we saw any dogs, and the number of dogs we saw. The dogs came from Phrogz' example and I thought in his honor, we should go ahead and stick with them.

Here's the view/index.xhtml code (where the interesting stuff is going on in this example):

<!-- String interpolation version with if/then/else. -->
#{if @saw_dog then "I saw a dog!" else "I didn't see any dogs!" end}

<!-- Using the ternary operator -->
#{@saw_dog ? "I saw a dog!" : "I didn't see any dogs!" }

<!-- Check the count, if the count is anything but 1 add an "s" at the end. -->
<br/>I saw #{@dog_count} dog#{"s" unless @dog_count==1} at the park

<!-- Put the number of dogs seen -->

<!-- Put the number of dogs seen by creating a list. -->
#{ d=''; 1.upto(@dog_count) do d << "<li>dog</li>" end; d}

<!-- Put the number of dogs seen by creating an array and using join (from ^manveru)-->
#{Array.new(@dog_count){ "<li>dog</li>" }.join}

First off, we use an if/then/else to display whether we saw a dog or not. Through all of these examples, remember that Ruby will return the last thing calculated for the expression. In this case, either the result of the "then" or the "else". Next we have the same thing using the ternary operator. I first saw this in C and it's a pretty handy tool to have lying around. After this, we have Phrogz' example where we put the dog_count and then if it is not 1, we add the "s" on the end of "dog". He used the ":s" form (an identifier) of this, but this may be a bit more readable for the non-experts among us. Next we put out the number of dogs we saw using the "*" operator of a string. In this case we add the br tag to put each of them on a separate line. Finally, we do things the hard way. Instead of using our as we did in the last post, we use only string interpolation to create the list. Like I said, this is not the way I'd recommend doing it, but it may give you some ideas. Update: Even more finally, we create the same list by creating an array and doing a join on it. This comes from ^manveru and will probably be faster and use less memory than the previous version.

The other files, layout/page.xhtml and public/page.css are the same as earlier, so just grab them from our last post.

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

No comments:

Post a Comment