]> sipb.mit.edu Git - ikiwiki.git/blob - doc/ikiwiki/directive/template.mdwn
move template documentation to the template directive
[ikiwiki.git] / doc / ikiwiki / directive / template.mdwn
1 The `template` directive is supplied by the [[!iki plugins/template desc=template]] plugin.
2
3 [[Templates]] are files that can be filled out and inserted into pages in the
4 wiki, by using the template directive. The directive has an `id` parameter
5 that identifies the template to use. The remaining parameters are used to
6 fill out the template.
7
8 ## Example
9
10         \[[!template id=note text="""Here is the text to insert into my note."""]]
11
12 This fills out the `note` template, filling in the `text` field with
13 the specified value, and inserts the result into the page.
14
15 ## Using a template
16
17 Generally, a value can include any markup that would be allowed in the wiki
18 page outside the template. Triple-quoting the value even allows quotes to
19 be included in it. Combined with multi-line quoted values, this allows for
20 large chunks of marked up text to be embedded into a template:
21
22         \[[!template id=foo name="Sally" color="green" age=8 notes="""
23         * \[[Charley]]'s sister.
24         * "I want to be an astronaut when I grow up."
25         * Really 8 and a half.
26         """]]
27
28 ## Creating a template
29
30 The template is a regular wiki page, located in the `templates/`
31 subdirectory inside the source directory of the wiki.
32
33 (Alternatively, templates can be stored in a directory outside the wiki,
34 as files with the extension ".tmpl".
35 By default, these are searched for in `/usr/share/ikiwiki/templates`;
36 the `templatedir` setting can be used to make another directory be searched
37 first.)
38
39 The template uses the syntax used by the [[!cpan HTML::Template]] perl
40 module, which allows for some fairly complex things to be done. Consult its
41 documentation for the full syntax, but all you really need to know are a
42 few things:
43
44 * Each parameter you pass to the template directive will generate a 
45   template variable. There are also some pre-defined variables like PAGE
46   and BASENAME.
47 * To insert the value of a variable, use `<TMPL_VAR variable>`. Wiki markup
48   in the value will first be converted to html.
49 * To insert the raw value of a variable, with wiki markup not yet converted
50   to html, use `<TMPL_VAR raw_variable>`.
51 * To make a block of text conditional on a variable being set use
52   `<TMPL_IF NAME="variable">text</TMPL_IF>`.
53 * To use one block of text if a variable is set and a second if it's not,
54   use `<TMPL_IF NAME="variable">text<TMPL_ELSE>other text</TMPL_IF>`
55
56 Here's a sample template:
57
58         <span class="infobox">
59         Name: \[[<TMPL_VAR raw_name>]]<br />
60         Age: <TMPL_VAR age><br />
61         <TMPL_IF NAME="color">
62         Favorite color: <TMPL_VAR color><br />
63         <TMPL_ELSE>
64         No favorite color.<br />
65         </TMPL_IF>
66         <TMPL_IF NAME="notes">
67         <hr />
68         <TMPL_VAR notes>
69         </TMPL_IF>
70         </span>
71
72 The filled out template will be formatted the same as the rest of the page
73 that contains it, so you can include WikiLinks and all other forms of wiki
74 markup in the template. Note though that such WikiLinks will not show up as
75 backlinks to the page that uses the template.
76
77 Note the use of "raw_name" inside the [[ikiwiki/WikiLink]] generator in the
78 example above. This ensures that if the name contains something that might
79 be mistaken for wiki markup, it's not converted to html before being
80 processed as a [[ikiwiki/WikiLink]].
81
82
83 [[!meta robots="noindex, follow"]]