Yet another trick for Radiant CMS: Reusable snippets

In my endless quest to get DRY with Radiant, the Rails-based CMS for the new Pathfinder website, I made another exciting discovery last week: Snippets are basically just server-side includes that get interpolated into your pages before Radiant's custom tags are parsed. This means that you can create a snippet to, say, display a bunch of information about a page and then plug that snippet into any number of different contexts.

Consider the following three examples, all of which assume you've created a snippet called "whatever" that displays a few named page parts and the publish-date of a specific page within Radiant:

  • Here, our snippet is executed for each child of the current page. The result for each page appears within a paragraph.
    <r:children:each>  <p>    <r:snippet name="whatever"/>  </p></r:children each>
  • Here, our snippet is executed for each child of the current page and, if they exist, each grandchild. The display this time is as a series of nested unordered lists.
    <r:children:each>  <div>    <r:if_children>      <ul>        <r:children:each>          <li>            <r:snippet name="whatever"/>          </li>        </r:children:each>      </ul>    </r:if_children>  </div></r:children:each>
  • Here, our snippet is executed for each child of a specific page within the site and the display is driven by a block-quotation wrapper.
    <r:find url="/whatever/">  <r:children:each>    <blockquote>      <r:snippet name="whatever"/>    </blockquote>  </r:children:each></r:find>

The upshot is that as long as the code that calls a snippet provides that snippet with an appropriate context, the snippet will execute as expected. This allows for a lot of smart, DRY patterns in your system of layouts, pages and snippets. Depending on whether it lives in an unordered list, a div, a paragraph or a blockquote, you can even style your snippet differently using CSS. The use of specific CSS classes would provide even more fine-grained control.

The new, Radiant-backed Pathfinder website is just days away from launching. We look forward to sharing it with you.

Related posts:

  1. Getting semantic and DRY with microformats and Radiant CMS
  2. Radiant CMS: Some tradeoffs, but they’re worth it
  3. Radiant CMS: It’s all about the extensions
  4. Radiant CMS and the DRY principle
  5. Writing reusable jQuery modules: Make everything a plugin

Topics:

Leave a comment

Powered by WP Hashcash

Launch: Pathfinder Newsletter

    Get a monthly update on best practices for delivering successful software.

    Subscribe via email


    Subscribe via RSS      RSS icon

Topics

Search

WordPress

Comments about this site: info@pathf.com