How to Create Templates

Overview

Templates are simply ERB files that have a few special variables and functions in their environment. If you're not familiar with Ruby, ERB is a library that allows you to embed Ruby code in arbitrary text files. A template is simply a regular text file where:

Template files are literally passed to ERB unchanged, so anything that is legal in ERB is OK in a template file.

Here is a simple example of a template file. This template will output the year, authors, and title of every paper in the bibliography, embedded within an HTML ordered list. The entries will be sorted by year.

<ol>
  <% bib.sort_by { |e| e.year }.reverse.each do |entry| %>
      <li><%= entry.year %>. <%= entry.author.pretty %>. <%= entry.title %></li>
  <% end %>
</ol>

Notice how the end in the fourth line closes the do two lines above. The variable bib as well as the functions year, author, pretty, and title are all provided by BibOut, and are described in the next section.

Special functions available to BibOut templates

The Ruby code that lives in a template is run within the context of an object of type ErbBinding. This class provides variables and methods that are useful within templates.

Additionally, the BibOut package is mostly just a thin wrapper over another package which is called bibtex-ruby. You'll need to know about several of the packages from bibtex-ruby in order to create your own templates.

The main two members that you need to know from the ErbBinding are:

Both of these are available within the binding that is used to evaluate your template. What this means is that you can just use the identifier bib within your template code, and it will work.

There are also two classes that you need to know about, both of them inherited from the bibtex-ruby package. These are described in the next two sections.

Bibliography

The bib local variable is a variable of type Bibliography, which is a bibtex-ruby class that contains all of the information in your bibliography. One important thing that it lets you access all of the entries in the bibliography, and search for particular entries of interest.

The Bibliography object also contains a rich query language that allows you to access subset of the bibliography. For example,

bib["@entry[year=1983]"]

returns a list of all entries in the bibliography whose year is 1983. If the year you want lives in a variable called y, then you can just use Ruby string interpolation syntax like this:

bib["@entry[year=#{y}]"]

For more examples, see the bibtex-ruby readme and the documentation for the bibtex-ruby query function.

Entry

Each entry that you retreive from a Bibliography object will be an object of type BibTeX::Entry. This is another class from bibtex-ruby. This class will let you access the information of each field within a BibTeX entry. The methods of the Entry class that you will be most interested in are:

Examples of Templates

Here are some examples to get you started.