Playing around with Ruby and MT Template Modules

| Tags: , ,

For some time now I have been thinking about changing the way my site is built. The problem was that whenever I wanted to change some general part of the site – the side bar for example – I had to do it in each template that is used in MovableType. That is fairly tedious and error prone work. Now the first idea I had was to use some scripting language. MovableType would generate the script which would then call some external code to generate the side bar, the header, footer etc.

I’m making my living out of software development in Java. So JSPs and servlets are natural choices for me when it comes to web application development. Unfortunately my hoster doesn’t support Java as very few web hosters do. You have to have an own server which hosts your site to be able to use Java on it.

Let’s look what’s available then. Hmm, PHP. Why not? So I got some book about PHP5 and read through it. Well, PHP5 does have some object-oriented aspects. It knows classes and objects - hurray. But somehow those aren’t really first class citizens. It looks like the PHP developers wanted to get a part of the OO hype and just threw in classes and objects into PHP. PHP is still an imperative programming language looking very much like C and borrowing some ideas from Perl.

The next one was Ruby . Ruby is a real object-oriented language. In many aspects it resembles Smalltalk. I haven’t used Smalltalk, but I have seen enough of it to recognize a language that takes a similar path. In Ruby you can write something like this

(1..10).each {|i| puts i}

This will iterate from 1 to 10 and put out the numbers on the console each on its own line. There’s also a for loop for people not feeling comfortable with this very object-oriented loop variant. Ruby is nice and fun to code. It’s dynamically typed which gave me some headaches, but then there’s no scripting language with static typing.

So I started coding my web site in Ruby. There is no really good IDE for Ruby. There’s something called Ruby Development Tools , which is a plugin for Eclipse. Compared to the Java IDE’s like IntelliJ IDEA it’s only little more than a text editor with syntax highlighting.

I was nearly finished with the whole framework for my site and was coding the templates, when I began having some security concerns. Think about the following Ruby code

commentbody =<< 'END'
<$MTCommentBody$>
END

That’s a really good idea at the first sight, but look twice. Ruby will read all after “« ‘END’” until the next occurrence of “END” into the variable commentbody. But what happens if the comment body looks like the following

blah blah
END
`do something nasty`

Fortunately Ruby will report a syntax error on the second END. The worst thing that happens here is that the page containing this code won’t show up in the browser. It won’t execute “do something nasty” (backticks are used in Ruby to enclose commands that are directly executed on the shell). But a page that doesn’t show up because some user wrote some hackish code into a comment isn’t acceptable.

So back in MovableType I saw for the first time the section titled “Template Modules”. I don’t know why I didn’t see it earlier. The documentation concerning template modules is very sparse. It only tells that they can be used to define blocks of HTML code that can be reused in the templates. It doesn’t tell you that you can have MovableType-tags in there and that you can include template modules into other template modules. So in the end, template modules – being a rather simple thing – where what I really needed.

That said, I’m not leaving Ruby behind. Ruby is still a cool language. I have some ideas for things where Ruby can be pretty handy. I’m currently trying to create a new archive index. I’m trying to do it with a MovableType-plugin, but currently have no success with it. If that doesn’t work, I’ll probably take Ruby for this task.