1 The `template` directive is supplied by the [[!iki plugins/template desc=template]] plugin.
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.
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.
14 \[[!template id=note text="""Here is the text to insert into my note."""]]
16 This fills out the `note` template, filling in the `text` field with
17 the specified value, and inserts the result into the page.
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:
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.
32 ## Creating a template
34 The template is in a regular wiki page, located in the `templates/`
35 subdirectory inside the source directory of the wiki.
36 The contents of the [[templatebody]] directive are used as the
37 template. Anything outside that directive is not included in the template,
38 and is usually used as documentation describing the template.
40 If the template does not contain a [[templatebody]] directive, the entire
41 source of the page is used for the template. This is deprecated, because
42 it leads to the template markup being interpreted as ordinary
43 page source when the page is built, as well as being used as the template.
45 Alternatively, templates can be stored in a directory outside the wiki,
46 as files with the extension ".tmpl".
47 By default, these are searched for in `/usr/share/ikiwiki/templates`,
48 the `templatedir` setting can be used to make another directory be searched
49 first. When referring to templates outside the wiki source directory, the "id"
50 parameter is not interpreted as a pagespec, you must include the full filename
51 of the template page including the ".tmpl" extension,
52 and the templatebody directive is not used. E.g.:
54 \[[!template id=blogpost.tmpl]]
56 The template uses the syntax used by the [[!cpan HTML::Template]] perl
57 module, which allows for some fairly complex things to be done. Consult its
58 documentation for the full syntax, but all you really need to know are a
61 * Each parameter you pass to the template directive will generate a
62 template variable. There are also some pre-defined variables like PAGE
64 * To insert the value of a variable, use `<TMPL_VAR variable>`. Wiki markup
65 in the value will first be converted to html.
66 * To insert the raw value of a variable, with wiki markup not yet converted
67 to html, use `<TMPL_VAR raw_variable>`.
68 * To make a block of text conditional on a variable being set use
69 `<TMPL_IF variable>text</TMPL_IF>`.
70 * To use one block of text if a variable is set and a second if it's not,
71 use `<TMPL_IF variable>text<TMPL_ELSE>other text</TMPL_IF>`
73 Here's a sample template:
75 \[[!templatebody <<ENDBODY
76 <span class="infobox">
77 Name: \[[<TMPL_VAR raw_name>]]<br />
78 Age: <TMPL_VAR age><br />
80 Favorite color: <TMPL_VAR color><br />
82 No favorite color.<br />
91 This template describes a person. Parameters: name, age,
92 color (favorite color, optional), notes (optional).
94 The filled out template will be formatted the same as the rest of the page
95 that contains it, so you can include WikiLinks and all other forms of wiki
96 markup in the template. Note though that such WikiLinks will not show up as
97 backlinks to the page that uses the template.
99 Note the use of "raw_name" inside the [[ikiwiki/WikiLink]] generator in the
100 example above. This ensures that if the name contains something that might
101 be mistaken for wiki markup, it's not converted to html before being
102 processed as a [[ikiwiki/WikiLink]].
105 [[!meta robots="noindex, follow"]]