none
[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><ul>
18                 <li><a href="#1. Default variable expansion"> 1. Default variable expansion</a></li>
19                 <li><a href="#2. Preventing variable expansion"> 2. Preventing variable expansion</a></li>
20                 <li><a href="#3. Control over variable expansi"> 3. Control over variable expansion</a></li>
21               </ul>
22             </li>
23             <li><a href="#Specifying a Form"> Specifying a Form</a></li>
24             <li><a href="#Automatically Generated Topic Na"> Automatically Generated Topic Names</a></li>
25             <li><a href="#Template Topics in Action"> Template Topics in Action</a></li>
26             <li><a href="#Using Absolute vs Relative URLs"> Using Absolute vs Relative URLs in Templates</a></li>
27           </ul>
28         </li>
29       </ul>
30     </li>
31   </ul>
32 </div>
33
34 ## <a name="Overview"></a> Overview
35
36 _Templates_ are plain text with embedded _template directives_ that tell TWiki how to compose blocks of text together, to create something new.
37
38 There are two types of template:
39
40 - **Master Templates**: Define the HTML used to display TWiki pages.
41 - **Template Topics**: Define default text when you create a new topic
42
43 **_%T% Tip:_** TWiki:TWiki.TWikiTemplatesSupplement on TWiki.org has supplemental documentation on TWiki templates.
44
45 <a name="HtmlTemplates"></a>
46
47 ## <a name="Master Templates"></a> Master Templates
48
49 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.
50
51 Master templates are also used in the definition of [[TWikiSkins]].
52
53 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.
54
55 <a name="TemplateVariables"></a>
56
57 ### <a name="How Template Directives Work"></a> How Template Directives Work
58
59 - Directives are of the form <code>**%TMPL:&lt;key&gt;%**</code> and <code>**%TMPL:&lt;key&gt;\{"attr"\}%**</code>.
60 - Directives:
61   - <code>**%TMPL:INCLUDE\{"file"\}%**</code>: Includes a template file. The file is found as described [[below|Main/WebHome#FindingTemplates]].
62   - <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`.
63   - <code>**%TMPL:END%**</code>: Ends a block definition.
64   - <code>**%TMPL:P\{"var"\}%**</code>: Includes a previously defined block.
65   - <code>**%\{...\}%**</code>: is a comment.
66 - Two-pass processing lets you use a variable before or after declaring it.
67 - 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.
68 - %H% Use of template directives is optional: templates work without them.
69 - %X% **NOTE:** Template directives work only for templates: they do not get processed in normal topic text.
70
71 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`.
72
73 Note that parameters can simply be ignored; for example, `%TMPL:P{"x"}%` will expand to x%P%z.
74
75 Any alphanumeric characters can be used in parameter names. You are highly recommended to use parameter names that cannot be confused with [[TWikiVariables]].
76
77 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_:
78
79     %TMPL:DEF{"link_inactive"}%<input type="button" disabled value="Link>%TMPL:END%
80     %TMPL:DEF{"link_active"}%<input type="button" onclick="link()" value="Link" />%TMPL:END%
81     %TMPL:P{context="inactive" then="inactive_link" else="active_link"}% for %CONTEXT%
82
83 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.
84
85 ### <a name="Finding Templates"></a> Finding Templates
86
87 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.
88
89 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.
90
91 You can also save templates in user topics (_IF_ there is no possible template match in the `templates` directory). The `{TemplatePath}` configuration setting defines which topics will be accepted as templates.
92
93 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.
94
95 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]].
96
97 1. templates/%RED%web%ENDCOLOR%/%RED%script%ENDCOLOR%.%RED%skin%ENDCOLOR%.tmpl for each %RED%skin%ENDCOLOR% on the skin path
98   - %X% this usage is supported **for compatibility only** and is **deprecated**. Store web-specific templates in TWiki topics instead.
99 2. templates/%RED%script%ENDCOLOR%.%RED%skin%ENDCOLOR%.tmpl for each %RED%skin%ENDCOLOR% on the skin path
100 3. templates/%RED%web%ENDCOLOR%/%RED%script%ENDCOLOR%.tmpl
101   - %X% this usage is supported **for compatibility only** and is **deprecated**. Store web-specific templates in TWiki topics instead.
102 4. templates/%RED%script%ENDCOLOR%.tmpl
103 5. The TWiki topic aweb.atopic if the template name can be parsed into aweb.atopic
104 6. The TWiki topic %RED%web%ENDCOLOR%.%RED%Skin%ENDCOLOR%Skin%RED%Script%ENDCOLOR%Template for each %RED%skin%ENDCOLOR% on the skin path
105 7. The TWiki topic %RED%web%ENDCOLOR%.%RED%Script%ENDCOLOR%Template
106 8. The TWiki topic %SYSTEMWEB%.%RED%Skin%ENDCOLOR%Skin%RED%Script%ENDCOLOR%Template for each %RED%skin%ENDCOLOR% on the skin path
107 9. The TWiki topic %SYSTEMWEB%.%RED%Script%ENDCOLOR%Template
108
109 **Legend:**
110
111 - %RED%script%ENDCOLOR% refers to the script name, e.g `view`, `edit`
112 - %RED%Script%ENDCOLOR% refers to the same, but with the first character capitalized, e.g `View`
113 - %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.
114 - %RED%Skin%ENDCOLOR% refers to the same, but with the first character capitalized, e.g `Dragon`
115 - %RED%web%ENDCOLOR% refers to the current web
116
117 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`:
118
119 1. `templates/Thisweb/example.print.tmpl` _deprecated; don't rely on it_
120 2. `templates/Thisweb/example.pattern.tmpl` _deprecated; don't rely on it_
121 3. `templates/example.print.tmpl`
122 4. `templates/example.pattern.tmpl`
123 5. `templates/Thisweb/example.tmpl` _deprecated; don't rely on it_
124 6. `templates/example.tmpl`
125 7. `Thisweb.PrintSkinExampleTemplate`
126 8. `Thisweb.PatternSkinExampleTemplate`
127 9. `Thisweb.ExampleTemplate`
128 10. `%SYSTEMWEB%.PrintSkinExampleTemplate`
129 11. `%SYSTEMWEB%.PatternSkinExampleTemplate`
130 12. `%SYSTEMWEB%.ExampleTemplate`
131
132 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:
133
134 - `VIEW_TEMPLATE` sets the template to be used for viewing a topic
135 - `EDIT_TEMPLATE` sets the template for editing a topic.
136
137 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.
138
139 ### <a name="TMPL:INCLUDE recursion for piece"></a> TMPL:INCLUDE recursion for piecewise customisation, or mixing in new features
140
141 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:
142
143     %TMPL:INCLUDE{"view"}%
144     %TMPL:DEF{"breadcrumb"}% We don't want any crumbs %TMPL:END%
145
146 and then set SKIN=yourlocal,pattern
147
148 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`.
149
150 ### <a name="Default master template"></a> Default master template
151
152 <code>**twiki.tmpl**</code> is the default master template. It defines the following sections.
153
154 <table border="1" cellpadding="0" cellspacing="0">
155   <tr>
156     <th bgcolor="#99CCCC"><strong> Template variable: </strong></th>
157     <th bgcolor="#99CCCC"><strong> Defines: </strong></th>
158   </tr>
159   <tr>
160     <td><code>%TMPL:DEF{"sep"}%</code></td>
161     <td> "|" separator </td>
162   </tr>
163   <tr>
164     <td><code>%TMPL:DEF{"htmldoctype"}%</code></td>
165     <td> Start of all HTML pages </td>
166   </tr>
167   <tr>
168     <td><code>%TMPL:DEF{"standardheader"}%</code></td>
169     <td> Standard header (ex: view, index, search) </td>
170   </tr>
171   <tr>
172     <td><code>%TMPL:DEF{"simpleheader"}%</code></td>
173     <td> Simple header with reduced links (ex: edit, attach, oops) </td>
174   </tr>
175   <tr>
176     <td><code>%TMPL:DEF{"standardfooter"}%</code></td>
177     <td> Footer, excluding revision and copyright parts </td>
178   </tr>
179 </table>
180
181 <a name="TemplateTopics"></a>
182
183 ## <a name="Template Topics"></a> Template Topics
184
185 The second type of template in TWiki are template topics. Template topics define the default text for new topics. There are four types of template topic:
186
187 <table border="1" cellpadding="0" cellspacing="0">
188   <tr>
189     <th bgcolor="#99CCCC"><strong> Topic Name: </strong></th>
190     <th bgcolor="#99CCCC"><strong> What it is: </strong></th>
191   </tr>
192   <tr>
193     <td>[[Main/WebTopicViewTemplate]]</td>
194     <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>
195   </tr>
196   <tr>
197     <td>[[Main/WebTopicNonWikiTemplate]]</td>
198     <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>
199   </tr>
200   <tr>
201     <td>[[Main/WebTopicEditTemplate]]</td>
202     <td> Default text used in a new topic. </td>
203   </tr>
204   <tr>
205     <td> &lt;MyCustomNamed&gt;Template </td>
206     <td> Whenever you create a topic ending in the word "Template", it is automatically added to the list of available templates in the "Use Template" drop down field on the WebCreateNewTopic page. </td>
207   </tr>
208 </table>
209
210 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:
211
212 1. A topic name specified by the `templatetopic` CGI parameter
213   - if no web is specified, the current web is searched first and then the %SYSTEMWEB% web
214 2. WebTopicEditTemplate in the current web
215 3. WebTopicEditTemplate in the %SYSTEMWEB% web
216
217 <a name="TemplateTopicsVars"></a>
218
219 ### <a name="Variable Expansion"></a> Variable Expansion
220
221 [[TWikiVariables]] located in template topics get expanded as follows when a new topic is created.
222
223 <a name="DefaultVariableExpansion"></a>
224
225 #### <a name="1. Default variable expansion"></a> 1. Default variable expansion
226
227 The following variables used in a template topic automatically get expanded when new topic is created based on it:
228
229 <table border="1" cellpadding="0" cellspacing="0">
230   <tr>
231     <th bgcolor="#99CCCC"><strong> Variable: </strong></th>
232     <th bgcolor="#99CCCC"><strong> Description: </strong></th>
233   </tr>
234   <tr>
235     <td><code>%DATE%</code></td>
236     <td> Signature format date. See [[Main/VarDATE]]</td>
237   </tr>
238   <tr>
239     <td><code>%GMTIME%</code></td>
240     <td> Date/time. See [[Main/VarGMTIME]]</td>
241   </tr>
242   <tr>
243     <td><code>%GMTIME{...}%</code></td>
244     <td> Formatted date/time. See [[Main/VarGMTIME2]]</td>
245   </tr>
246   <tr>
247     <td><code>%NOP%</code></td>
248     <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>
249   </tr>
250   <tr>
251     <td><code>%STARTSECTION{type="templateonly"}%%BR%...%BR%%ENDSECTION{type="templateonly"}%</code></td>
252     <td> Text that gets removed when a new topic based on the template is created. See notes below. </td>
253   </tr>
254   <tr>
255     <td><code>%SERVERTIME%</code></td>
256     <td> Date/time. See [[Main/VarSERVERTIME]]</td>
257   </tr>
258   <tr>
259     <td><code>%SERVERTIME{...}%</code></td>
260     <td> Formatted date/time. See [[Main/VarSERVERTIME2]]</td>
261   </tr>
262   <tr>
263     <td><code>%USERNAME%</code></td>
264     <td> Login name of user who is instantiating the new topic, e.g. admin </td>
265   </tr>
266   <tr>
267     <td><code>%URLPARAM{"name"}%</code></td>
268     <td> Value of a named URL parameter. See [[Main/VarURLPARAM]]. </td>
269   </tr>
270   <tr>
271     <td><code>%WIKINAME%</code></td>
272     <td>[[Main/WikiName]] of user who is instantiating the new topic, e.g. admin </td>
273   </tr>
274   <tr>
275     <td><code>%WIKIUSERNAME%</code></td>
276     <td> User name of user who is instantiating the new tpoic, e.g. Main.admin </td>
277   </tr>
278 </table>
279
280 <a name="PreventingVariableExpansion"></a>
281
282 #### <a name="2. Preventing variable expansion"></a> 2. Preventing variable expansion
283
284 In a template topic, embed text that you _do not_ want expanded inside a `%STARTSECTION{type="templateonly"}% ... %ENDSECTION{type="templateonly"}%` section. For example, you might want to write this in the template topic:
285
286     %STARTSECTION{type="templateonly"}%
287     This template can only be changed by:
288        * Set ALLOWTOPICCHANGE = %USERSWEB%.TWikiAdminGroup
289     %ENDSECTION{type="templateonly"}%
290
291 This will restrict who can edit the template topic, but will be removed when a new topic based on that template topic is created.
292
293 `%NOP%` can be used to prevent expansion of TWiki variables that would otherwise be expanded during topic creation. For example, escape `%SERVERTIME%` with `%SER%NOP%VERTIME%`.
294
295 <a name="ControlOverVariableExpansion"></a>
296
297 #### <a name="3. Control over variable expansi"></a> 3. Control over variable expansion
298
299 You can forcefully expand [[TWikiVariables]] by placing them inside a `type="expandvariables"` section in the template topic, such as:
300
301     %STARTSECTION{ type="expandvariables" }% ... %ENDSECTION{ type="expandvariables" }%
302
303 Example:
304
305 If you have the following content in a template topic:
306
307     %STARTSECTION{ type="expandvariables" }%
308        * %TWIKIWEB%.ATasteOfTWiki - view a short introductory presentation on TWiki for beginners
309        * %TWIKIWEB%.WelcomeGuest - starting points on TWiki
310        * %TWIKIWEB%.TWikiUsersGuide - complete TWiki documentation
311        * Sandbox.%HOMETOPIC% - try out TWiki on your own
312        * Sandbox.%TOPIC%Sandbox - just for me
313     %ENDSECTION{ type="expandvariables" }%
314
315 you will get this raw text in new topics based on that template topic:
316
317        * TWiki.ATasteOfTWiki - view a short introductory presentation on TWiki for beginners
318        * TWiki.WelcomeGuest - starting points on TWiki
319        * TWiki.TWikiUsersGuide - complete TWiki documentation
320        * Sandbox.WebHome - try out TWiki on your own
321        * Sandbox.JimmyNeutronSandbox - just for me
322
323 <a name="SpecifyingForm"></a>
324
325 ### <a name="Specifying a Form"></a> Specifying a Form
326
327 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.
328
329 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.
330
331 See [[TWikiScripts]] for information about all the other parameters to `edit`.
332
333 <a name="AutomaticallyGeneratedTopicname"></a>
334
335 ### <a name="Automatically Generated Topic Na"></a> Automatically Generated Topic Names
336
337 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.
338
339 **Examples:**
340
341 - `BugAUTOINC0` - creates topic names `Bug0`, `Bug1`, `Bug2`, ... (does not sort properly)
342 - `ItemAUTOINC0000` - creates topic names `Item0000`, `Item0001`, `Item0002`, ... (sorts properly up to 9999)
343 - `DocIDAUTOINC10001` - start with `DocID10001`, `DocID10002`, ... (sorts properly up to 99999; auto-links)
344
345 Example link to create a new topic:%BR%
346
347     [[%SCRIPTURLPATH{edit}%/%WEB%/BugIDAUTOINC00001?templatetopic=BugTemplate;topicparent=%TOPIC%;t=%SERVERTIME{"$day$hour$min$sec"}%][Create new item]]
348
349 ### <a name="Template Topics in Action"></a> Template Topics in Action
350
351 Here is an example for creating new topics (in the Sandbox web) based on a specific template topic and form:
352
353 <form action="http://www.dementia.org/twiki/edit/Sandbox/" name="new">
354   <ul>
355     <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>
356   </ul>
357 </form>
358
359 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:
360
361     <form name="new" action="%SCRIPTURLPATH{edit}%/Sandbox/">
362        * New example topic:
363          <input type="text" name="topic" value="ExampleTopicAUTOINC0001" size="30" />
364          <input type="hidden" name="templatetopic" value="ExampleTopicTemplate" />
365          <input type="hidden" name="topicparent" value="%TOPIC%" />
366          <input type="hidden" name="onlywikiname" value="on" />
367          <input type="hidden" name="onlynewtopic" value="on" />
368          <input type="submit"  value="Create" />
369     </form>
370
371 See [[TWikiScripts#edit]] for details of the parameters that the `edit` script understands.
372
373 %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>
374
375 ### <a name="Using Absolute vs Relative URLs"></a><a name="Using Absolute vs Relative URLs "></a> Using Absolute vs Relative URLs in Templates
376
377 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.
378
379 **_Related Topics:_** [[TWikiSkins]], [[DeveloperDocumentationCategory]], [[AdminDocumentationCategory]]
380
381 -- **_Contributors:_** TWiki:Main.PeterThoeny, TWiki:Main.MikeMannix, TWiki:Main.DavidLeBlanc, TWiki:Main.CrawfordCurrie, TWiki:Main.SopanShewale