-
Get a monthly update on best practices for delivering successful software.
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:
<r:children:each> <p> <r:snippet name="whatever"/> </p></r:children each>
<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>
<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:
Topics: Radiant CMS