buildrelease
[openafs-wiki.git] / TWiki / TWikiTemplates.mdwn
1 # <a name="TWiki Templates"></a> TWiki Templates
2
3 _Definition of the templates used to render all HTML pages displayed in TWiki_
4
5 <div>
6   <ul>
7     <li><a href="#TWiki Templates"> TWiki Templates</a><ul>
8         <li><a href="#Overview"> Overview</a></li>
9         <li><a href="#Master Templates"> Master Templates</a><ul>
10             <li><a href="#How Template Directives Work"> How Template Directives Work</a></li>
11             <li><a href="#Finding Templates"> Finding Templates</a></li>
12             <li><a href="#TMPL:INCLUDE recursion for piece"> TMPL:INCLUDE recursion for piecewise customisation, or mixing in new features</a></li>
13             <li><a href="#Default master template"> Default master template</a></li>
14           </ul>
15         </li>
16         <li><a href="#Template Topics"> Template Topics</a><ul>
17             <li><a href="#Variable Expansion"> Variable Expansion</a></li>
18             <li><a href="#Specifying a Form"> Specifying a Form</a></li>
19             <li><a href="#Automatically Generated Topic Na"> Automatically Generated Topic Names</a></li>
20             <li><a href="#Template Topics in Action"> Template Topics in Action</a></li>
21             <li><a href="#Using Absolute vs Relative URLs"> Using Absolute vs Relative URLs in Templates</a></li>
22           </ul>
23         </li>
24       </ul>
25     </li>
26   </ul>
27 </div>
28
29 ## <a name="Overview"></a> Overview
30
31 _Templates_ are plain text with embedded _template directives_ that tell TWiki how to compose blocks of text together, to create something new.
32
33 There are two types of template:
34
35 - **Master Templates**: Define the HTML used to display TWiki pages.
36 - **Template Topics**: Define default text when you create a new topic
37
38 **_%T% Tip:_** TWiki:TWiki.TWikiTemplatesSupplement on TWiki.org has supplemental documentation on TWiki templates.
39
40 <a name="HtmlTemplates"></a>
41
42 ## <a name="Master Templates"></a> Master Templates
43
44 TWiki uses master templates when composing the output from all actions, like topic view, edit, and preview. This allows you to change the look and feel of all pages by editing just a few template files.
45
46 Master templates are also used in the definition of [[TWikiSkins]].
47
48 Master templates are stored as text files with the extension `.tmpl`. They are usually HTML with embedded _template directives_. The directives are expanded when TWiki wants to generate a user interface screen.
49
50 <a name="TemplateVariables"></a>
51
52 ### <a name="How Template Directives Work"></a> How Template Directives Work
53
54 - Directives are of the form <code>**%TMPL:&lt;key&gt;%**</code> and <code>**%TMPL:&lt;key&gt;\{"attr"\}%**</code>.
55 - Directives:
56   - <code>**%TMPL:INCLUDE\{"file"\}%**</code>: Includes a template file. The file is found as described [[below|Main/WebHome#FindingTemplates]].
57   - <code>**%TMPL:DEF\{"block"\}%**</code>: Define a block. **All** text between this and the next `%TMPL:END%` directive is removed and saved for later use with `%TMPL:P`.
58   - <code>**%TMPL:END%**</code>: Ends a block definition.
59   - <code>**%TMPL:P\{"var"\}%**</code>: Includes a previously defined block.
60   - <code>**%\{...\}%**</code>: is a comment.
61 - Two-pass processing lets you use a variable before or after declaring it.
62 - Templates and [[TWikiSkins]] work transparently and interchangeably. For example, you can create a skin that overloads only the `twiki.tmpl` master template, like `twiki.print.tmpl`, that redefines the header and footer.
63 - %H% Use of template directives is optional: templates work without them.
64 - %X% **NOTE:** Template directives work only for templates: they do not get processed in normal topic text.
65
66 TMPL:P also supports simple parameters. For example, given the definition `%TMPL:DEF{"x"}% x%P%z%TMPL:END%` then `%TMPL:P{"x" P="y"}%` will expand to `xyz`.
67
68 Note that parameters can simply be ignored; for example, `%TMPL:P{"x"}%` will expand to x%P%z.
69
70 Any alphanumeric characters can be used in parameter names. You are highly recommended to use parameter names that cannot be confused with [[TWikiVariables]].
71
72 Note that three parameter names, `context`, `then` and `else` are **reserved**. They are used to support a limited form of "if" condition that you can use to select which of two templates to use, based on a _context identifier_:
73
74     %TMPL:DEF{"link_inactive"}%<input type="button" disabled value="Link>%TMPL:END%
75     %TMPL:DEF{"link_active"}%<input type="button" onclick="link()" value="Link" />%TMPL:END%
76     %TMPL:P{context="inactive" then="inactive_link" else="active_link"}% for %CONTEXT%
77
78 When the "inactive" context is set, then this will expand the "link\_inactive" template; otherwise it will expand the "link\_active" template. See [[IfStatements]] for details of supported context identifiers.
79
80 ### <a name="Finding Templates"></a> Finding Templates
81
82 The master templates shipped with a twiki release are stored in the twiki/templates directory. As an example, <code>**twiki/templates/view.tmpl**</code> is the default template file for the <code>**twiki/bin/view**</code> script.
83
84 You can save templates in other directories as long as they are listed in the `{TemplatePath}` configuration setting. The `{TemplatePath}` is defined in the Miscellaneous section of the [configure](http://www.dementia.org/twiki/configure) page.
85
86 You can also save templates in user topics. The `{TemplatePath}` configuration setting defines which topics will be accepted as templates.
87
88 Templates that are included with an explicit `'.tmpl'` extension are looked for only in the `templates/` directory. For instance `%TMPL:INCLUDE{"example.tmpl"}%` will only return `templates/example.tmpl`, regardless of `{TemplatePath}` and SKIN settings.
89
90 The out-of-the-box setting of `{TemplatePath}` supports the following search order to determine which template file or topic to use for a particular script or `%TMPL:INCLUDE{"script"}%` statement. The _skin path_ is set as described in [[TWikiSkins]].
91
92 1. templates/%RED%web%ENDCOLOR%/%RED%script%ENDCOLOR%.%RED%skin%ENDCOLOR%.tmpl for each %RED%skin%ENDCOLOR% on the skin path
93   - %X% this usage is supported **for compatibility only** and is **deprecated**. Store web-specific templates in TWiki topics instead.
94 2. templates/%RED%script%ENDCOLOR%.%RED%skin%ENDCOLOR%.tmpl for each %RED%skin%ENDCOLOR% on the skin path
95 3. templates/%RED%web%ENDCOLOR%/%RED%script%ENDCOLOR%.tmpl
96   - %X% this usage is supported **for compatibility only** and is **deprecated**. Store web-specific templates in TWiki topics instead.
97 4. templates/%RED%script%ENDCOLOR%.tmpl
98 5. The TWiki topic aweb.atopic if the template name can be parsed into aweb.atopic
99 6. The TWiki topic %RED%web%ENDCOLOR%.%RED%Skin%ENDCOLOR%Skin%RED%Script%ENDCOLOR%Template for each %RED%skin%ENDCOLOR% on the skin path
100 7. The TWiki topic %RED%web%ENDCOLOR%.%RED%Script%ENDCOLOR%Template
101 8. The TWiki topic %SYSTEMWEB%.%RED%Skin%ENDCOLOR%Skin%RED%Script%ENDCOLOR%Template for each %RED%skin%ENDCOLOR% on the skin path
102 9. The TWiki topic %SYSTEMWEB%.%RED%Script%ENDCOLOR%Template
103
104 **Legend:**
105
106 - %RED%script%ENDCOLOR% refers to the script name, e.g `view`, `edit`
107 - %RED%Script%ENDCOLOR% refers to the same, but with the first character capitalized, e.g `View`
108 - %RED%skin%ENDCOLOR% refers to a skin name, e.g `dragon`, `pattern`. All skins are checked at each stage, in the order they appear in the skin path.
109 - %RED%Skin%ENDCOLOR% refers to the same, but with the first character capitalized, e.g `Dragon`
110 - %RED%web%ENDCOLOR% refers to the current web
111
112 For example, the `example` template file will be searched for in the following places, when the current web is `Thisweb` and the skin path is `print,pattern`:
113
114 1. `templates/Thisweb/example.print.tmpl` _deprecated; don't rely on it_
115 2. `templates/Thisweb/example.pattern.tmpl` _deprecated; don't rely on it_
116 3. `templates/example.print.tmpl`
117 4. `templates/example.pattern.tmpl`
118 5. `templates/Thisweb/example.tmpl` _deprecated; don't rely on it_
119 6. `templates/example.tmpl`
120 7. `Thisweb.PrintSkinExampleTemplate`
121 8. `Thisweb.PatternSkinExampleTemplate`
122 9. `Thisweb.ExampleTemplate`
123 10. `%SYSTEMWEB%.PrintSkinExampleTemplate`
124 11. `%SYSTEMWEB%.PatternSkinExampleTemplate`
125 12. `%SYSTEMWEB%.ExampleTemplate`
126
127 Template names are usually derived from the name of the currently executing script; however it is also possible to override these settings in the `view` and `edit` scripts, for example when a topic-specific template is required. Two preference variables can be used to override the templates used:
128
129 - `VIEW_TEMPLATE` sets the template to be used for viewing a topic
130 - `EDIT_TEMPLATE` sets the template for editing a topic.
131
132 If these preferences are set locally (using _Local_ instead of _Set_) for a topic, in [[WebPreferences]], in [[Main.TWikiPreferences|Main/TWikiPreferences]], or [[%SYSTEMWEB%.TWikiPreferences|SYSTEMWEB/TWikiPreferences]] (using _Set_), the indicated templates will be chosen for `view` and `edit` respectively. The template search order is as specified above.
133
134 ### <a name="TMPL:INCLUDE recursion for piece"></a> TMPL:INCLUDE recursion for piecewise customisation, or mixing in new features
135
136 If there is recursion in the TMPL:INCLUDE chain (eg twiki.classic.tmpl contains `%TMPL:INCLUDE{"twiki"}%`, the templating system will include the next twiki.SKIN in the skin path. For example, to create a customisation of pattern skin, where you _only_ want to over-ride the breadcrumbs for the view script, you can create only a view.yourlocal.tmpl:
137
138     %TMPL:INCLUDE{"view"}%
139     %TMPL:DEF{"breadcrumb"}% We don't want any crumbs %TMPL:END%
140
141 and then set SKIN=yourlocal,pattern
142
143 The default `{TemplatePath}` will not give you the desired result if you put these statements in the topic `Thisweb.YourlocalSkinViewTemplate`. The default `{TemplatePath}` will resolve the request to the `template/view.pattern.tmpl`, before it gets to the `Thisweb.YourlocalSkinViewTemplate` resolution. You can make it work by prefixing the `{TemplatePath}` with: `$web.YourlocalSkin$nameTemplate`.
144
145 ### <a name="Default master template"></a> Default master template
146
147 <code>**twiki.tmpl**</code> is the default master template. It defines the following sections.
148
149 <table border="1" cellpadding="0" cellspacing="0">
150   <tr>
151     <th bgcolor="#99CCCC"><strong> Template variable: </strong></th>
152     <th bgcolor="#99CCCC"><strong> Defines: </strong></th>
153   </tr>
154   <tr>
155     <td><code>%TMPL:DEF{"sep"}%</code></td>
156     <td> "|" separator </td>
157   </tr>
158   <tr>
159     <td><code>%TMPL:DEF{"htmldoctype"}%</code></td>
160     <td> Start of all HTML pages </td>
161   </tr>
162   <tr>
163     <td><code>%TMPL:DEF{"standardheader"}%</code></td>
164     <td> Standard header (ex: view, index, search) </td>
165   </tr>
166   <tr>
167     <td><code>%TMPL:DEF{"simpleheader"}%</code></td>
168     <td> Simple header with reduced links (ex: edit, attach, oops) </td>
169   </tr>
170   <tr>
171     <td><code>%TMPL:DEF{"standardfooter"}%</code></td>
172     <td> Footer, excluding revision and copyright parts </td>
173   </tr>
174 </table>
175
176 <a name="TemplateTopics"></a>
177
178 ## <a name="Template Topics"></a> Template Topics
179
180 The second type of template in TWiki are template topics. Template topics define the default text for new topics. There are three types of template topic:
181
182 <table border="1" cellpadding="0" cellspacing="0">
183   <tr>
184     <th bgcolor="#99CCCC"><strong> Topic Name: </strong></th>
185     <th bgcolor="#99CCCC"><strong> What it is: </strong></th>
186   </tr>
187   <tr>
188     <td>[[Main/WebTopicViewTemplate]]</td>
189     <td> Alert page shown when you try to view a nonexistent topic. This page is usually used as a prompt to help you create a new topic. </td>
190   </tr>
191   <tr>
192     <td>[[Main/WebTopicNonWikiTemplate]]</td>
193     <td> Alert page shown when you try to view a nonexistent topic with a non-WikiName. Again, this page is used as a prompt to help you create the new topic. </td>
194   </tr>
195   <tr>
196     <td>[[Main/WebTopicEditTemplate]]</td>
197     <td> Default text used in a new topic. </td>
198   </tr>
199 </table>
200
201 When you create a new topic using the `edit` script, TWiki locates a topic to use as a content template according to the following search order:
202
203 1. A topic name specified by the `templatetopic` CGI parameter
204   - if no web is specified, the current web is searched first and then the %SYSTEMWEB% web
205 2. WebTopicEditTemplate in the current web
206 3. WebTopicEditTemplate in the %SYSTEMWEB% web
207
208 <a name="TemplateTopicsVars"></a>
209
210 ### <a name="Variable Expansion"></a> Variable Expansion
211
212 When the following variables are used in a template topic, they automatically get expanded when new topic is created based on it:
213
214 <table border="1" cellpadding="0" cellspacing="0">
215   <tr>
216     <th bgcolor="#99CCCC"><strong> Variable: </strong></th>
217     <th bgcolor="#99CCCC"><strong> Description: </strong></th>
218   </tr>
219   <tr>
220     <td><code>%DATE%</code></td>
221     <td> Signature format date. See [[Main/VarDATE]]</td>
222   </tr>
223   <tr>
224     <td><code>%GMTIME%</code></td>
225     <td> Date/time. See [[Main/VarGMTIME]]</td>
226   </tr>
227   <tr>
228     <td><code>%GMTIME{...}%</code></td>
229     <td> Formatted date/time. See [[Main/VarGMTIME2]]</td>
230   </tr>
231   <tr>
232     <td><code>%NOP%</code></td>
233     <td> A no-operation variable that gets removed. Useful to prevent a SEARCH from hitting an edit template topic; also useful to escape a variable, such as <code>%URLPA%NOP%RAM{...}%</code> escaping URLPARAM </td>
234   </tr>
235   <tr>
236     <td><code>%STARTSECTION{type="templateonly"}%%BR%...%BR%%ENDSECTION{type="templateonly"}%</code></td>
237     <td> Text that gets removed when a new topic based on the template is created. See notes below. </td>
238   </tr>
239   <tr>
240     <td><code>%SERVERTIME%</code></td>
241     <td> Date/time. See [[Main/VarSERVERTIME]]</td>
242   </tr>
243   <tr>
244     <td><code>%SERVERTIME{...}%</code></td>
245     <td> Formatted date/time. See [[Main/VarSERVERTIME2]]</td>
246   </tr>
247   <tr>
248     <td><code>%USERNAME%</code></td>
249     <td> Login name of user who is instantiating the new topic, e.g. admin </td>
250   </tr>
251   <tr>
252     <td><code>%URLPARAM{"name"}%</code></td>
253     <td> Value of a named URL parameter </td>
254   </tr>
255   <tr>
256     <td><code>%WIKINAME%</code></td>
257     <td>[[Main/WikiName]] of user who is instantiating the new topic, e.g. admin </td>
258   </tr>
259   <tr>
260     <td><code>%WIKIUSERNAME%</code></td>
261     <td> User name of user who is instantiating the new tpoic, e.g. Main.admin </td>
262   </tr>
263 </table>
264
265 `%STARTSECTION{type="templateonly"}%%BR%...%BR%%ENDSECTION{type="templateonly"}%` markers are used to embed text that you _do not_ want expanded when a new topic based on the template topic is created. For example, you might want to write in the template topic:
266
267     %STARTSECTION{type="templateonly"}%
268     This template can only be changed by:
269        * Set ALLOWTOPICCHANGE = %USERSWEB%.TWikiAdminGroup
270     %ENDSECTION{type="templateonly"}%
271
272 This will restrict who can edit the template topic, but will get removed when a new topic based on that template topic is created.
273
274 `%NOP%` can be used to prevent expansion of TWiki variables that would otherwise be expanded during topic creation e.g.i escape `%SERVERTIME%` with `%SER%NOP%VERTIME%`.
275
276 All other variables are unchanged, e.g. are carried over "as is" into the new topic.
277
278 ### <a name="Specifying a Form"></a> Specifying a Form
279
280 When you create a new topic based on a template, you often want the new topic to have a form attached to it. You can attach a form to the template topic, in which case it will be copied into the new topic.
281
282 Sometimes this isn't quite what you want, as it copies all the existing data from the template topic into the new topic. To avoid this and use the default values specified in the form definition instead, you can use the `formtemplate` CGI parameter to the `edit` script to specify the name of a form to attach.
283
284 See [[TWikiScripts]] for information about all the other parameters to `edit`.
285
286 <a name="AutomaticallyGeneratedTopicname"></a>
287
288 ### <a name="Automatically Generated Topic Na"></a> Automatically Generated Topic Names
289
290 For TWiki applications it is useful to be able to automatically generate unique topicnames, such as BugID0001, BugID0002, etc. You can add `AUTOINC<n>` to the topic name in the edit and save scripts, and it will be replaced with an auto-incremented number on topic save. `<n>` is a number starting from 0, and may include leading zeros. Leading zeros are used to zero-pad numbers so that auto-incremented topic names can sort properly. Deleted topics are not re-used to ensure uniqueness of topic names. That is, the auto-incremented number is always higher than the existing ones, even if there are gaps in the number sequence.
291
292 **Examples:**
293
294 - `BugAUTOINC0` - creates topic names `Bug0`, `Bug1`, `Bug2`, ... (does not sort properly)
295 - `ItemAUTOINC0000` - creates topic names `Item0000`, `Item0001`, `Item0002`, ... (sorts properly up to 9999)
296 - `DocIDAUTOINC10001` - start with `DocID10001`, `DocID10002`, ... (sorts properly up to 99999; auto-links)
297
298 Example link to create a new topic:%BR%
299
300     [[%SCRIPTURLPATH{edit}%/%WEB%/BugIDAUTOINC00001?templatetopic=BugTemplate;topicparent=%TOPIC%;t=%SERVERTIME{"$day$hour$min$sec"}%][Create new item]]
301
302 ### <a name="Template Topics in Action"></a> Template Topics in Action
303
304 Here is an example for creating new topics (in the Sandbox web) based on a specific template topic and form:
305
306 <form action="http://www.dementia.org/twiki/edit/Sandbox/" name="new">
307   <ul>
308     <li> New example topic: <input name="topic" size="30" type="text" value="ExampleTopicAUTOINC0001" /> <input type="submit" value="Create" /> <input name="templatetopic" type="hidden" value="ExampleTopicTemplate" /> <input name="topicparent" type="hidden" value="%TOPIC%" /> <input name="onlywikiname" type="hidden" value="on" /> <input name="onlynewtopic" type="hidden" value="on" /></li>
309   </ul>
310 </form>
311
312 The above form asks for a topic name. A hidden input tag named <code>**templatetopic**</code> specifies [[ExampleTopicTemplate]] as the template topic to use. Here is the HTML source of the form:
313
314     <form name="new" action="%SCRIPTURLPATH{edit}%/Sandbox/">
315        * New example topic:
316          <input type="text" name="topic" value="ExampleTopicAUTOINC0001" size="30" />
317          <input type="hidden" name="templatetopic" value="ExampleTopicTemplate" />
318          <input type="hidden" name="topicparent" value="%TOPIC%" />
319          <input type="hidden" name="onlywikiname" value="on" />
320          <input type="hidden" name="onlynewtopic" value="on" />
321          <input type="submit"  value="Create" />
322     </form>
323
324 See [[TWikiScripts#edit]] for details of the parameters that the `edit` script understands.
325
326 %T% **TIP:** You can use the `%WIKIUSERNAME%` and `%DATE%` variables in your topic templates to include the signature of the person creating a new topic. The variables are expanded into fixed text when a new topic is created. The standard signature is: <br /><code>**-- %WIKIUSERNAME% - %DATE%**</code>
327
328 ### <a name="Using Absolute vs Relative URLs"></a><a name="Using Absolute vs Relative URLs "></a> Using Absolute vs Relative URLs in Templates
329
330 When you use [[TWikiVariables]] such as %PUBURL% and %PUBURLPATH% in templates you should be aware that using %PUBURL% instead of %PUBURLPATH% puts absolute URLs in the produced HTML. This means that when a user saves a TWiki page in HTML and emails the file to someone outside a company firewall, the receiver has a severe problem viewing it. It is therefore recommended always to use the %PUBURLPATH% to refer to images, CSS, Javascript files etc so links become relative. This way browsers just give up right away and show a usable html file.
331
332 **_Related Topics:_** [[TWikiSkins]], [[DeveloperDocumentationCategory]], [[AdminDocumentationCategory]]
333
334 -- **_Contributors:_** TWiki:Main.PeterThoeny, TWiki:Main.MikeMannix, TWiki:Main.DavidLeBlanc, TWiki:Main.CrawfordCurrie