Tadpole: A Small but Extensible Templating Engine for Ruby
<p><em>Created by <a href="http://www.gnuu.org">Loren Segal</a> in 2008</em></p>
</div>
<div id='content'>
<ol>
<li id='#<Tadpole::SectionProviders::TemplateProvider:0x5bba6c>'>
<h2>Quick How-To's</h2>
<h3>Create a Template</h3>
<p>Creating a template is literally as easy as 1-2-3:</p>
<ol>
<li><p>Create your templates in a directory. All directories are templates and all templates
are directories. The directory name will be the (or part of) the name of your template.
Example for template <code>mytemplate</code>:</p>
<pre><code>templates/
 mytemplate/
 setup.rb
 section1.erb
 section2.haml
 copyright.html
</code></pre></li>
<li><p>Setup the "<em>table of contents</em>" of your sections in the <code>setup.rb</code>:</p>
<pre><code>def init
 super
 sections 'section1', 'section2', 'copyright'
end
</code></pre>
<p>Your sections can include another template (directory). This will call whatever
sections were part of that other template.</p>
<p>A directory does not <em>require</em> a <code>section.rb</code>. If it is not supplied, it will inherit
the setup file from its parent (including its sections).</p></li>
<li><p>Register the <code>templates</code> path as your root template directory and run the template:</p>
<pre><code>require 'tadpole'
Tadpole.register_template_path 'path/to/templates'
Tadpole('mytemplate').run
</code></pre></li>
</ol>
<h3>Override a Template</h3>
<p>You can override templates by simply registering another template_path and creating
a template of the same name in the new path. Using the <code>mytemplate</code> example from above
we can now make a directory:</p>
<pre><code>custom_templates/
 mytemplate/
 setup.rb
 header.erb
</code></pre>
<p>This template will <em>inherit</em> from the template above. Our <code>setup.rb</code> will therefore
contain:</p>
<pre><code>def init
 super
 sections.unshift 'header'
end
</code></pre>
<p>And to run this file all we need to do is:</p>
<pre><code>require 'tadpole'
Tadpole.register_template_path 'path/to/templates' # Register base template path
Tadpole.register_template_path 'path/to/custom_templates' # Register overridden template path

# Running our template will now add our 'header' file to the output
Tadpole('mytemplate').run
</code></pre>
<h3>Heirarchical Sections</h3>
<p>Sometimes you may need to encapsulate the output of some sections inside another one. An HTML
template, for example, will usually contain the page body inside the body tag of a more general
"header" template. To set this up, you use the following <code>sections</code> call:</p>
<pre><code>sections 'header', ['section1', 'section2', 'copyright']
</code></pre>
<p>You can then call these from your <code>header.erb</code> file as simple yields. Each yield renders
one section in the sub-list:</p>
<pre><code><html>
 <body>
 <h1>Section 1</h1>
 <%= yield %>

 <h1>Section 2</h1>
 <%= yield %>

 <h1>Copyright</h1>
 <%= yield %>
 </body>
</html>
</code></pre>
<p>Alternatively you can yield all sub-sections with the convenience call <code>all_sections</code>
(in the following <a href="http://haml.hamptoncatlin.com">Haml</a> example, yield param 's'
contains the section name which would serve as the li's id attribute):</p>
<pre><code>%html
 %body
 %ol
 - all_sections do |s|
 %li{:id => s}= yield
</code></pre>
</li>
<li id='#<Tadpole::SectionProviders::FileProvider:0x5a7f1c>'>
<h2>What is Tadpole?</h2>
<p><strong>Tadpole</strong> is a small templating engine that attempts to solve a problem that
no other templating engine does: <em>extensibility</em>. When dealing with templates,
most engines focus on the formatting of the output content while forgetting about
the important task a developer faces of hooking all these 'views' together. While
it may seem trivial and worth ignoring, in reality, many templates become plagued
with complexity and coupling due to the lack of support beyond the mere presentation
of a single file.</p>
<p><strong>Tadpole</strong> deals not with the formatting or translation or markup, but rather
with the organization of the data as it is outputted. In fact, <strong>Tadpole</strong> is not
markup at all, nor does it care what markup you use, having out of the box support
for the obvious template languages in Ruby (ERB, <a href="http://www.haml.hamptoncatlin.com">Haml</a>,
<a href="http://code.whytheluckystiff.net/markaby">Markaby</a>, <a href="http://builder.rubyforge.org">Builder</a>)
and the ability to add more. <strong>Tadpole is all about information organization, not formatting</strong>.</p>
<p>If you need a good visualization of what <strong>Tadpole</strong> is, think of it as <em>the table of</em>
<em>contents for your views</em>. Just as it is important to designing each view and partial of
your template, it is important to decide in what order these views will ultimately be
organized. <strong>Tadpole</strong>'s job is to store nothing but your table of contents, and then
spit it out when you're ready to show it to the world. This technique becomes very
powerful in some potentially familiar scenarios. (<em>See the "Real World Examples"</em>)</p>
</li>
<li id='#<Tadpole::SectionProviders::FileProvider:0x5a76c0>'>
<h2>Why Tadpole?</h2>
<p>Sufficed to say, you might be wondering what the <em>big deal</em> is. I mean, you're
probably getting along just fine without this new concept of tables of contents
...<em>or so you think</em>. The truth is that there are a lot of real-world scenarios
where the old-style template production simply doesn't cut it. I can say this
because <strong>Tadpole</strong> was <a href="http://www.github.com/lsegal/yard">born from one of them</a>.</p>
<p>I'll be honest, <strong>Tadpole</strong> does not meet every use-case scenario, and it probably
never will. But if you're writing a <em>blog app</em>, <em>CMS</em>, customizable <em>forum software</em> or
anything that will eventually support <em>customizable templates</em> or <em>theming</em>,
<em><strong>Tadpole</strong> was made for you</em>. Even if you're just dealing with a lot of <em>template</em>
<em>coupling</em> or <em>internationalization code</em>, there's a good chance you're looking at the
right tool as well.</p>
<h3>Good With Customizable Themes, You Say?</h3>
<p>Anyone who writes customizable software knows that it requires a lot of de-coupled code.
While templates are sometimes considered support files rather than actual software, the
same law holds true for them. Coupled templates are bad for theming because your users
can't get at the data they want.</p>
<p>The standard solution to this problem is to split your templates up into many 'partials'.
That way, any user can just go into the right partial and easily edit what they need
without copying <em>all</em> of the template data, right? <strong>Wrong</strong>. The problem starts when a
user wants to start adding or removing partials altogether. In fact, it's actually the
smallest changes that cause the biggest problems. Everytime they add one line to every
new partial, they pull in another entire file. Once <em>you</em> update that file, the changes
no longer sync to the user. Fixed a typo in your template? Your user may never get the
memo if he pulled in the file you touched. However, because <strong>Tadpole</strong> never actually
deals with template <em>data</em>, the same setup in <strong>Tadpole</strong> would not be problematic.
Using <strong>Tadpole</strong>, the user would never even have to touch your templates given a good
set of insertion points.</p>
<p>The lesson is, when it comes to customization, there is no partial that is partial enough.
<strong>Tadpole</strong> inevitably suffers from this problem as well. However, once you start thinking
in terms of template organization you'll find that it's much harder to decide what part
of a view deserves a 'partial' than it is to simply split your template up into a series
of cohesive "<em>sections</em>".</p>
</li>
<li id='#<Tadpole::SectionProviders::FileProvider:0x5a7094>'>
<h2>Some <em>Theoretically</em>-Real-World Examples of Tadpole in Action</h2>
<ol>
<li><p><em>Bob</em> made a Blogging application called <em>"Boblog"</em> and distributed it under the
MIT license over the internet. <em>Janet</em> found this application and decided to
use it to power her upcoming "100 Carrot Recipes" blog. She was mostly happy with
the provided themes but wanted to customize the look of the sidebar by adding a
"Favourite Recipes" links section and writing a tidbit about herself. Now "Boblog"
was a simplistic blog tool and did not support a multitude of plugins, but did use
<strong>Tadpole</strong> for theming. Janet read about the way customization was done using "Boblog"
and got to work making her changes. Janet went into her custom templates directory and
created her own new template <code>janet</code> because she had a bad feeling about directly playing
with the existing template files (<em>and "Boblog" docs said she didn't need to</em>). Inside
that directory she created her new files <code>fav_recipes.html</code> and <code>about.html</code> where she
inserted her links to various world renowned Carrot Chefs and a story about her dreams
of one day meeting them. Now, she wanted her about section to go at the top of the sidebar,
but she wanted her own links section to go beneath the regular links section (already
provided by "Boblog"). So, as per the docs, she continued to add a <code>setup.rb</code> file which
would connect her new files together with the template. In this file, she simply wrote:</p>
<pre><code>inherits 'default_theme'


def init
 super
 sections.unshift 'about'
 sections.place('fav_recipes').before('links')
end
</code></pre>
<p>She then went into "Boblog"'s administration interface and selected the new <code>janet</code>
theme. Voila, her dream of tasty success would finally come true.</p>
<p>Three days later, <em>Bob</em> got word from an anonymous tipster of a nasty bug in his software
that could potentially lead to harmful attacks if left unfixed. Guess what, that bug was
in the sidebar template that Janet was using! He quickly patched the bug and released a
fix, notifying all of his users of the changes (Janet was on his mailing list). Because
Janet never edited any of the files belonging to "Boblog", all she had to do was download
the patch and restart the application without ever having to remake all of her ever-so-
important theming changes to her blog.</p></li>
<li><p>Midget Inc. is working on a colourful new site redesign for their mobile widget business.
They sell mobile widgets to customers all across the globe and have very strict legal
procedures they need to follow when advertising their mobile widgets. In one specific
region, they are required by law to show a disclaimer above any advertising images they
display. Rather than place region specific logic inside a partial view, they decide to
use <strong>Tadpole</strong> to handle their templating system. They decide to use a folder structure
of the following to display their advertising page:</p>
<pre><code>advertising/
 setup.rb
 content.erb
 images.erb
 fr/
 setup.rb
 disclaimer.erb
</code></pre>
<p><code>advertising/setup.rb</code> contains the following:</p>
<pre><code>def init; super; sections 'content', 'images' end
</code></pre>
<p>The <code>fr/</code> subdirectory contains the specific content they need for the law-requiring region
and the logic to render this template only in that region is controlled by the controller.
Because the <code>fr/</code> template automatically inherits its sections from its parent, all they need
to do to set up this new logic is put the following in the <code>fr/setup.rb</code>:</p>
<pre><code>def init
 super
 sections.place('disclaimer').before('images')
end
</code></pre>
<p>And the templates can be rendered with:</p>
<pre><code>Tadpole('advertising').run
Tadpole('advertising/fr').run
</code></pre>
<p>Respectively.</p></li>
</ol>
</li>
<li id='#<Tadpole::SectionProviders::FileProvider:0x5a6aa4>'>
<h2>You Should Also Know</h2>
<p>That this <code>README</code> was generated by <strong>Tadpole</strong>. Try it:</p>
<pre><code>ruby examples/example2/run.rb markdown/readme
</code></pre>
</li>
</ol>
</div>
<div id='copyright'>
<h2>Copyright & Licensing Information</h2>
<p><em>Copyright 2008 Loren Segal.</em>
<em>All code licensed under the MIT License.</em></p>
</div>
</div>