QuickGuide

lsegal edited this page Sep 14, 2010 · 9 revisions
Clone this wiki locally

Create a Template

Creating a template is literally as easy as 1-2-3:

1. 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 mytemplate:

templates/ mytemplate/ setup.rb section1.erb section2.haml copyright.html

2. Setup the “table of contents” of your sections in the setup.rb:

def init super sections ‘section1’, ‘section2’, ‘copyright’ end

Your sections can include another template (directory). This will call whatever
sections were part of that other template.

A directory does not require a section.rb. If it is not supplied, it will inherit
the setup file from its parent (including its sections).

3. Register the `templates` path as your root template directory and run the template:

require ‘tadpole’ Tadpole.register_template_path ‘path/to/templates’ Tadpole(‘mytemplate’).run

Override a Template

You can override templates by simply registering another template_path and creating
a template of the same name in the new path. Using the mytemplate example from above
we can now make a directory:

custom_templates/ mytemplate/ setup.rb header.erb

This template will inherit from the template above. Our setup.rb will therefore
contain:

def init super sections.unshift ‘header’ end

And to run this file all we need to do is:

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
  1. Running our template will now add our ‘header’ file to the output
    Tadpole(‘mytemplate’).run

Heirarchical Sections

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 sections call:

sections ‘header’, [‘section1’, ‘section2’, ‘copyright’]

You can then call these from your `header.erb` file as simple yields. Each yield renders
one section in the sub-list:




Section 1


<%= yield %>

Section 2

<%= yield %>

Copyright

<%= yield %>

Alternatively you can yield all sub-sections with the convenience call all_sections
(in the following [Haml](http://haml.hamptoncatlin.com) example, yield param ‘s’
contains the section name which would serve as the li’s id attribute):

%html
%body
%ol
– all_sections do |s|
%li{:id => s}= yield