New Flexible Features in Standard Tags

Posted by Jim Gay on Friday, August 01, 2008 | |

There are some great new features added to the 0.6.9 “Etch” release of Radiant. I’ll be writing about some of the additions of the latest release in the coming days. More info about who I am down below, but the new features I’ll discuss in this post will help you feel as flexible as Mary Lou Retton (but writing layouts instead of swinging on the uneven bars).

The if_content and unless_content tags now accept a list of multiple parts, and have an inherit attribute just like the content tag. The if_self and if_ancestor_or_self now have unless counterparts, and the random tag now accepts dynamically generated options.

Read on for your radius gymnastics lesson…

Flexibility and the New if_content / unless_content Features

For a while, I’ve had the need for a few changes in the way Radiant manages to display (or not display) it’s content. With the release of 0.6.9, new and simple solutions are available.

inherit=“true”

A great new addition is the inherit attribute for if_content and unless_content. It’s used just like it is on content. Let’s take a look…

In previous versions of Radiant there was no simple way to conditionally output an HTML container for inherited content. This code would always render an empty container:


<div id="extras">
  <h2>Extras</h2>
  <r:content part="extras" inherit="true" />
</div>

No matter what, if the part “extras” exists in the page tree or not, that div and header will be displayed. Well no longer must you worry about that…


<r:if_content part="extras" inherit="true">
  <div id="extras">
    <h2>Extras</h2>
    <r:content part="extras" inherit="true" />
  </div>
<r:if_content>

Thanks go out to Mark A. Yoon for correcting the default inherit attribute on unless_content. In 0.6.8 it was incorrectly set to true but 0.6.9 fixed that.

List Multiple Parts and Use find=“any|all”

The if_content and unless_content tags now allow you to list multiple parts. This means that you can output content dependent upon the presence of more than one part.


<r:if_content part="testimonial, project">
  <div class="project_details">
    <r:content part="testimonial" />
    <r:content part="project" />
  </div>
<r:if_content>

You can change the default settings of this condition by adjusting the find attribute to your needs. If you would like to output some content when using if_content but would like for it to only display if your testimonial and project_description exist for the current page then you may use find="all". This, by the way, is the default.

You might use find="any" when you want to conditionally create a container in HTML to display content, but allow the content to differ depending on the parts.


<r:if_content part="announcement, resources, related" find="any">
  <div id="additional_info">
    <r:content part="announcement" />
    <r:content part="resources" />
    <r:content part="related" />
  </div>
<r:if_content>

So what will that do for you? If any of those parts exist the additional_info div will be generated, and if the specified content within exists, it will be displayed. If any one of the r:content blocks specifies a non-existent part, the behavior is that nothing will be rendered. And of course, if non of the parts exist, then the r:if_content tag will never be expanded.

Or Maybe You’re Flexible Like Nadia Comaneci

With the options that we have with the inherit attribute, multiple parts and the find attribute on content, if_content and unless_content tags you can be an Olympic gymnast of conditional tag writing.

I wrote the code (and the specs that define them) to add these features to Radiant. I’m excited about using them in our projects and I hope you’ll like them.

Tim Gosset added unless_ancestor_or_self and unless_self to complement the if counterparts of those tags. They’re great additions to the set of standard tags that Radiant provides.

And the random tag can now be generated with a list of dynamically created options. That means that you can finally use page children as options to create a random link to a pages children:


<r:random>
  <r:children:each:option:link />
<r:random>

Or perhaps you’ll find other ways to use that but any way you slice it, 0.6.9 Etch has some great new features (and bug fixes for 0.6.8).

I’m Jim Gay, founder and partner at Saturn Flyer where we do graphic and user interface design, Ruby on Rails development and see a bright future for Radiant. We’ll be working hard to add more simple and helpful features to everybody’s favorite Rails CMS. If you have any questions, please join the active and helpful mailing list or shop around the #raidantcms IRC at irc.freenode.net.