Posted by John W. Long on Monday, May 01, 2006 | |

Today, I released Radius 0.5.0. Radius is the tag-based templating language that is the real muscle behind Radiant. The Radius documentation contains a Quick Start guide that I would highly recommend reading if you are interested in creating your own tags. Radiant has a special plugin that allows you to add custom tags to a page. It's called a behavior. Here's a small example:

# Subclass the Behavior::Base behavior
class HelloWorldBehavior < Behavior::Base
  # Register the behavior so that it will appear in the behavior
  # combo box of the edit page screen.
  register "Hello World"

  # Detail what this behavior does
  description "The Hello World behavior adds the <r:hello /> tag to a page."
  # Define any number of tags in a +define_tags+ block
  define_tags do
    # Define the "hello" tag
    tag "hello" do |tag|
      # Set the name variable from the attributes hash or
      # default to 'World' and return the salutation
      name = tag.attr['name'] || 'World'
      "Hello #{name}!"

If you were to drop the above code into a file called hello_world_behavior.rb and put it in the app/behaviors directory you would then be able to assign the "Hello World" behavior to a page using the admin interface. This would allow you to use the <r:hello /> tag in any of that page’s the page parts. If you were to put <r:hello name="John" /> in a page part it would output Hello John!. If you simply put <r:hello /> into a page part it would output Hello World!.

Adding custom tags is just one of the things that you can do with behaviors. You can also modify page headers, access the request and response objects, and completely customize page rendering.

So what are you waiting for? Get out there and create your own behavior!