Blog Closed

This blog has moved to Github. This page will not be updated and is not open for comments. Please go to the new site for updated content.

Saturday, May 23, 2009

Wikibooks Templates

I've created a handful of new books in my time at Wikibooks, and I find that there are some processes that are common to each: We need to create a table of contents, we need to create various templates and categories, etc. Being a programmer and therefore very lazy, I try to find ways to automate these kinds of processes as well as I am able. To this end, I've created a series of what I call meta-templates, or templates that are used to create other templates. The goal of these meta templates is two-fold: First, to make it easy to create new templates quickly, and to hide a lot of the "dirty" formatting stuff that new users shouldn't have to worry about. For new and inexperienced users this is a huge boon. They can substitute one meta-template and get a full-featured header template to use in their new book. Certainly beats having to make a template from scratch, especially since our documentation in this area is still far from complete.

Let's look at some of the things that a good header template will want to have:
  1. The name of the book, which will probably double as a hyperlink to the TOC
  2. "Next" and "Back" links, for easy navigation
  3. Categories. The template should put all pages where it is transcluded into a category for all pages in the book.
  4. Some documentation, to explain to users how to use the template.
A lot of these items should only appear conditionally: Next and Back links should only appear if needed. There is no "Back" on the first page of the book, and no "Next" on the last page. Documentation about the template should appear on the template page but should not be transcluded with the template into every page in the book. The entire template will also not want to be displayed in a printed version, for various reasons. Let's look at how to implement that last requirement in wikitext:

<includeonly><no</includeonly>include>[[Category:Exclude in print]]</includeonly></no<includeonly>include></includeonly>

This is quite a mess! But that's the purpose of the meta template, I write this mess once and nobody else ever has to do it again. When we substitute the meta template to create a new navigation template, we get this:

<noinclude>[[Category:Exclude in print]]</noinclude>

So that makes more sense to see the result. We have to do all the mess above because if we didn't break up the noinclude tags in the meta template, they would disappear when we substituted into the navigation template.

Let's look at the related, but more complicated, problem of having next and back links that appear only when values are provided. Here's the code that I have to use in the meta-template:

<includeonly>{{</includeonly>#if: {{{last|}}} | ← [[{{{book}}}/{{{last}}}|{{{last}}}]] | }}

Again, without the includeonly tags put in those weird places, the {{#if:}} tag would disappear when we substituted. Now when we substitute, the includeonly tags disappear, leaving a perfectly-formed tag in their place:

{{#if: {{{last|}}} | ← [[{{{book}}}/{{{last}}}|{{{last}}}]] | }}

This string of stuff isn't exactly clean and isn't easy to read either, but at least the new users don't have to write it themselves. Anything we can do to decrease the barrier to entry for aspiring new book authors is always a good thing.

1 comment:

  1. i like ur website. thanks for this comment posting...
    more templates easy to download

    ReplyDelete

Note: Only a member of this blog may post a comment.