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