none
[openafs-wiki.git] / TWiki / TWikiTemplates.mdwn
1 <div>
2   <ul>
3     <li><a href="#TWiki Templates"> TWiki Templates</a><ul>
4         <li><a href="#Overview"> Overview</a></li>
5         <li><a href="#Major changes from the previous"> Major changes from the previous template system</a></li>
6         <li><a href="#How Template Variables Work"> How Template Variables Work</a></li>
7         <li><a href="#Types of Template"> Types of Template</a><ul>
8             <li><a href="#Master Templates"> Master Templates</a></li>
9             <li><a href="#HTML Page Templates"> HTML Page Templates</a></li>
10             <li><a href="#Template Topics"> Template Topics</a><ul>
11                 <li><a href="#Edit Template Topics and Variabl"> Edit Template Topics and Variable Expansion</a></li>
12                 <li><a href="#Template Topics in Action"> Template Topics in Action</a></li>
13               </ul>
14             </li>
15           </ul>
16         </li>
17         <li><a href="#Templates by Example"> Templates by Example</a><ul>
18             <li><a href="#Base template oopsbase.tmpl"> Base template oopsbase.tmpl</a></li>
19             <li><a href="#Test template oopstest.tmpl"> Test template oopstest.tmpl</a></li>
20             <li><a href="#Sample screen shot of oopstest.t"> Sample screen shot of oopstest.tmpl</a></li>
21           </ul>
22         </li>
23         <li><a href="#Known Issues"> Known Issues</a></li>
24       </ul>
25     </li>
26   </ul>
27 </div>
28
29 # <a name="TWiki Templates"></a> TWiki Templates
30
31 _Definition of the templates used to render all HTML pages displayed in TWiki_
32
33 ## <a name="Overview"></a> Overview
34
35 The new modular template system offers flexible, easy control over the layout of all TWiki pages. The master template approach groups parts that are shared by several templates - like headers and footers - in a common file. Special variables allow individual layouts to include parts from a master template - variables are mixed with regular HTML markup for template-specific content. Templates are used to define [[page layout|Main/WebHome#HtmlTemplates]], and also to supply [[default content|Main/WebHome#TemplateTopics]] for new pages.
36
37 ## <a name="Major changes from the previous"></a><a name="Major changes from the previous "></a> Major changes from the previous template system
38
39 Where the old templates were each complete HTML documents, the new templates are defined using variables to include template parts from a master file. You can now change one instance of a common element to update all occurrences; previously, every affected template had to be updated. This simplifies the conversion of templates into XHTML format, and provides a more versatile solution for templates and for [[TWikiSkins]]. The new system:
40
41 - separates a set of common template parts into a base template that is included by all of the related templates;
42 - defines common variables, like a standard separator (ex: "|"), in the base template;
43 - defines variable text in the individual templates and passes it back to the base template.
44
45 <a name="TemplateVariables"></a>
46
47 ## <a name="How Template Variables Work"></a> How Template Variables Work
48
49 - Special template directives (or preprocessor commands) are embedded in normal templates.
50 - All template preprocessing is done in `&TWiki::Store::readTemplate()` so that the caller simply gets an expanded template file (the same as before).
51 - Directives are of the form <code>**%TMPL:&lt;key&gt;%**</code> and <code>**%TMPL:&lt;key&gt;\{"attr"\}%**</code>.
52 - Directives:
53   - <code>**%TMPL:INCLUDE\{"file"\}%**</code>: Includes a template file. The template directory of the current web is searched first, then the templates root (`twiki/templates`).
54   - <code>**%TMPL:DEF\{"var"\}%**</code>: Define a variable. Text between this and the END directive is not returned, but put into a hash for later use.
55   - <code>**%TMPL:END%**</code>: Ends variable definition.
56   - <code>**%TMPL:P\{"var"\}%**</code>: Prints a previously defined variable.
57 - Variables live in a global name space: there is no parameter passing.
58 - Two-pass processing lets you use a variable before or after declaring it.
59 - 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.
60 - %H% Use of template directives is optional: templates work without them.
61 - %X% **NOTE:** Template directives work only for templates: they do not get processed in topic text.
62
63 ## <a name="Types of Template"></a> Types of Template
64
65 There are three types of template:
66
67 - **Master Template**: Stores common parts; included by other templates
68 - **HTML Page Templates**: Defines the layout of %WIKITOOLNAME% pages
69 - **Template Topics**: Defines default text when you create a new topic
70
71 ### <a name="Master Templates"></a> Master Templates
72
73 Common parts, appearing in two or more templates, can be defined in a master template and then shared by others: <code>**twiki.tmpl**</code> is the default master template.
74
75 > <table border="1" cellpadding="0" cellspacing="0">
76 >   <tr>
77 >     <th bgcolor="#99CCCC"><strong> Template variable: </strong></th>
78 >     <th bgcolor="#99CCCC"><strong> Defines: </strong></th>
79 >   </tr>
80 >   <tr>
81 >     <td> %TMPL:DEF{"sep"}% </td>
82 >     <td> "|" separator </td>
83 >   </tr>
84 >   <tr>
85 >     <td> %TMPL:DEF{"htmldoctype"}% </td>
86 >     <td> Start of all HTML pages </td>
87 >   </tr>
88 >   <tr>
89 >     <td> %TMPL:DEF{"standardheader"}% </td>
90 >     <td> Standard header (ex: view, index, search) </td>
91 >   </tr>
92 >   <tr>
93 >     <td> %TMPL:DEF{"simpleheader"}% </td>
94 >     <td> Simple header with reduced links (ex: edit, attach, oops) </td>
95 >   </tr>
96 >   <tr>
97 >     <td> %TMPL:DEF{"standardfooter"}% </td>
98 >     <td> Footer, excluding revision and copyright parts </td>
99 >   </tr>
100 >   <tr>
101 >     <td> %TMPL:DEF{"oops"}% </td>
102 >     <td> Skeleton of oops dialog </td>
103 >   </tr>
104 > </table>
105
106 <a name="HtmlTemplates"></a>
107
108 ### <a name="HTML Page Templates"></a> HTML Page Templates
109
110 TWiki uses HTML template files for 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.
111
112 Templates are stored either in the <code>**twiki/templates**</code> directory or in user topics. As an example, <code>**twiki/templates/view.tmpl**</code> is the template file for the <code>**twiki/bin/view**</code> script.
113
114 %H% Templates can be overloaded by individual webs.
115
116 %H% [[TWikiSkins]] can overload the standard templates.
117
118 TWiki uses the following search order to determine which template to use:
119
120 > <table border="1" cellpadding="0" cellspacing="0">
121 >   <tr>
122 >     <th bgcolor="#99CCCC"><strong> If a skin is specified </strong></th>
123 >     <th bgcolor="#99CCCC"><strong> If no skin is specified </strong></th>
124 >   </tr>
125 >   <tr>
126 >     <td><code>templates/%WEB%/%RED%script.%RED%skin%ENDCOLOR%.tmpl</code></td>
127 >     <td><code>templates/%WEB%/%RED%script%ENDCOLOR%.tmpl</code></td>
128 >   </tr>
129 >   <tr>
130 >     <td><code>templates/%RED%script%ENDCOLOR%.%RED%skin%ENDCOLOR%.tmpl</code></td>
131 >     <td><code>templates/%RED%script%ENDCOLOR%.tmpl</code></td>
132 >   </tr>
133 >   <tr>
134 >     <td><code>data/%WEB%/%RED%Skin%ENDCOLOR%Skin%RED%Script%ENDCOLOR%Template.txt</code></td>
135 >     <td><code>data/%WEB%/%RED%Script%ENDCOLOR%Template.txt</code></td>
136 >   </tr>
137 >   <tr>
138 >     <td><code>data/TWiki/%RED%Skin%ENDCOLOR%Skin%RED%Script%ENDCOLOR%Template.txt</code></td>
139 >     <td><code>data/TWiki/%RED%Script%ENDCOLOR%Template.txt</code></td>
140 >   </tr>
141 >   <tr>
142 >     <td colspan="2"><strong>Legend:</strong> %BB% %RED%script%ENDCOLOR% refers to the script name, e.g <code>view</code>, <code>edit</code> %BB% %RED%Script%ENDCOLOR% refers to the same, but with the first character capitalized, e.g <code>View</code> %BB% %RED%skin%ENDCOLOR% refers to the skin name, e.g <code>dragon</code>, <code>pattern</code> %BB% %RED%Skin%ENDCOLOR% refers to the same, but with the first character capitalized, e.g <code>Dragon</code> %BB% <code>%WEB%</code> refers to the current web </td>
143 >   </tr>
144 > </table>
145
146 Additionally (and primarily for use in `%TMPL:INCLUDE{}%`) the template name may be a wiki topic name, specified as `%RED%Web%ENDCOLOR%.%RED%Topic%ENDCOLOR%`, in which case the search is:
147
148 > <table border="1" cellpadding="0" cellspacing="0">
149 >   <tr>
150 >     <th bgcolor="#99CCCC"><strong> If a skin is specified </strong></th>
151 >     <th bgcolor="#99CCCC"><strong> If no skin is specified </strong></th>
152 >   </tr>
153 >   <tr>
154 >     <td><code>templates/%RED%web%ENDCOLOR%/%RED%Web%ENDCOLOR%.%RED%Topic%ENDCOLOR%.%RED%skin%ENDCOLOR%.tmpl</code></td>
155 >     <td><code>templates/%RED%web%ENDCOLOR%/%RED%Web%ENDCOLOR%.%RED%Topic%ENDCOLOR%.tmpl</code></td>
156 >   </tr>
157 >   <tr>
158 >     <td><code>templates/%RED%Web%ENDCOLOR%.%RED%Topic%ENDCOLOR%.%RED%skin%ENDCOLOR%.tmpl</code></td>
159 >     <td><code>templates/%RED%Web%ENDCOLOR%.%RED%Topic%ENDCOLOR%.tmpl</code></td>
160 >   </tr>
161 >   <tr>
162 >     <td align="center" colspan="2"><code>data/%RED%Web%ENDCOLOR%/%RED%Topic%ENDCOLOR%.txt</code></td>
163 >   </tr>
164 > </table>
165
166 If %RED%Web%ENDCOLOR% is not specified in the INCLUDE, it defaults to TWiki, and the search to the first type.
167
168 Special variables are used in templates, especially in `view`, to display [[meta data|Main/TWikiMetaData#MetaDataRendering]].
169
170 <a name="TemplateTopics"></a>
171
172 ### <a name="Template Topics"></a> Template Topics
173
174 Template topics define the default text for new topics. There are three types of template topic:
175
176 > <table border="1" cellpadding="0" cellspacing="0">
177 >   <tr>
178 >     <th bgcolor="#99CCCC"><strong> Topic Name: </strong></th>
179 >     <th bgcolor="#99CCCC"><strong> What it is: </strong></th>
180 >   </tr>
181 >   <tr>
182 >     <td>[[Main/WebTopicViewTemplate]]</td>
183 >     <td> Error page shown when you try to view a nonexistent topic </td>
184 >   </tr>
185 >   <tr>
186 >     <td>[[Main/WebTopicNonWikiTemplate]]</td>
187 >     <td> Alert page shown when you try to view a nonexistent topic with a non-WikiName </td>
188 >   </tr>
189 >   <tr>
190 >     <td>[[Main/WebTopicEditTemplate]]</td>
191 >     <td> Default text shown when you create a new topic. </td>
192 >   </tr>
193 > </table>
194
195 All template topics are located in the TWiki web. The [[WebTopicEditTemplate]] can be overloaded. When you create a new topic, TWiki locates a topic to use as a content template according to the following search order:
196
197 1. A topic name specified by the `templatetopic` CGI parameter.
198 2. WebTopicEditTemplate in the current web
199 3. WebTopicEditTemplate in the TWiki web
200
201 #### <a name="Edit Template Topics and Variabl"></a> Edit Template Topics and Variable Expansion
202
203 The following variables get expanded when a user creates a new topic based on a template topic:
204
205 > <table border="1" cellpadding="0" cellspacing="0">
206 >   <tr>
207 >     <th bgcolor="#99CCCC"><strong> Variable: </strong></th>
208 >     <th bgcolor="#99CCCC"><strong> Description: </strong></th>
209 >   </tr>
210 >   <tr>
211 >     <td><code>%DATE%</code></td>
212 >     <td> Current date, e.g. <code>29 Jun 2010</code></td>
213 >   </tr>
214 >   <tr>
215 >     <td><code>%USERNAME%</code></td>
216 >     <td> Login name, e.g. <code>jsmith</code></td>
217 >   </tr>
218 >   <tr>
219 >     <td><code>%WIKINAME%</code></td>
220 >     <td>[[Main/WikiName]] of user, e.g. <code>JohnSmith</code></td>
221 >   </tr>
222 >   <tr>
223 >     <td><code>%WIKIUSERNAME%</code></td>
224 >     <td> User name, e.g. <code>Main.JohnSmith</code></td>
225 >   </tr>
226 >   <tr>
227 >     <td><code>%URLPARAM{"name"}%</code></td>
228 >     <td> Value of a named URL parameter </td>
229 >   </tr>
230 >   <tr>
231 >     <td><code>%NOP%</code></td>
232 >     <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 like <code>%URLPARAM%NOP%{...}%</code></td>
233 >   </tr>
234 >   <tr>
235 >     <td><code>%NOP{ ... }%</code></td>
236 >     <td> A no-operation text that gets removed. Useful to write-protect an edit template topic, but not the topics based this template topic. See notes below. Example:%BR% <code>%NOP{%BR%   * Set ALLOWTOPICCHANGE = Main.TWikiAdminGroup%BR% }%</code></td>
237 >   </tr>
238 > </table>
239
240 **_Notes:_**
241
242 - Unlike other variables, `%NOP{ ... }%` can span multiple lines.
243 - The scan for the closing `}%` pattern is "non-greedy", that is, it stops at the first occurance. That means, you need to escape variables with parameters located inside `%NOP{ ... }%`: Insert a `%NOP%` between `}` and `%`. Silly example: `%NOP{ %GMTIME{"$year"}%NOP%% }%`.
244
245 All other variables are unchanged, e.g. are carried over "as is" into the new topic.
246
247 #### <a name="Template Topics in Action"></a> Template Topics in Action
248
249 Here is an example for creating new topics based on a specific template topic:
250
251 <form action="http://www.dementia.org/twiki//edit/%WEB%/" name="new">
252   <ul>
253     <li> New example topic: <input name="topic" size="23" type="text" value="ExampleTopic2010x06x29" /> <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" /> <input type="submit" value="Create" /> (date format is YYYYxMMxDD) </li>
254   </ul>
255 </form>
256
257 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:
258
259     <form name="new" action="%SCRIPTURLPATH%/edit%SCRIPTSUFFIX%/%INTURLENCODE{"%WEB%"}%/">
260         * New example topic:
261           <input type="text" name="topic" value="ExampleTopic%SERVERTIME{$yearx$mox$day}%" size="23" />
262           <input type="hidden" name="templatetopic" value="ExampleTopicTemplate" />
263           <input type="hidden" name="topicparent" value="%TOPIC%" />
264           <input type="hidden" name="onlywikiname" value="on" />
265           <input type="hidden" name="onlynewtopic" value="on" />
266           <input type="submit" value="Create" />
267           (date format is <nop>YYYYxMMxDD)
268     </form>
269
270 The `edit` scipt understands the following parameters, typically supplied by HTML input fields:
271
272 > <table border="1" cellpadding="0" cellspacing="0">
273 >   <tr>
274 >     <th bgcolor="#99CCCC"><strong> Parameter: </strong></th>
275 >     <th bgcolor="#99CCCC"><strong> Description: </strong></th>
276 >   </tr>
277 >   <tr>
278 >     <td><code>topic</code></td>
279 >     <td> Name of topic to create. Can be set in a text field, or is set programmatically (e.g. with a sequential number) </td>
280 >   </tr>
281 >   <tr>
282 >     <td><code>onlywikiname</code></td>
283 >     <td> If set, TWiki will complain if the topic name is not a [[Main/WikiWord]]</td>
284 >   </tr>
285 >   <tr>
286 >     <td><code>onlynewtopic</code></td>
287 >     <td> If set, TWiki will complain if a topic of the same name already exists </td>
288 >   </tr>
289 >   <tr>
290 >     <td><code>templatetopic</code></td>
291 >     <td> The name of the template topic, e.g. topic used to copy the initial content </td>
292 >   </tr>
293 >   <tr>
294 >     <td><code>topicparent</code></td>
295 >     <td> Sets the parent topic </td>
296 >   </tr>
297 >   <tr>
298 >     <td><code>TopicClassification</code></td>
299 >     <td> Assuming the template topic has a form with a field called "TopicClassification", it will set the value of the field </td>
300 >   </tr>
301 >   <tr>
302 >     <td><code>contenttype</code></td>
303 >     <td> Optional parameter that defines the application type to write into the CGI header. Defaults to <code>text/html</code>. May be used to invoke alternative client applications </td>
304 >   </tr>
305 >   <tr>
306 >     <td><code>anyname</code></td>
307 >     <td> Any parameter can passed to the new topic; if the template topic contains <code>%URLPARAM{"anyname"}%</code>, it will be replaced by its value </td>
308 >   </tr>
309 > </table>
310
311 %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>
312
313 ## <a name="Templates by Example"></a> Templates by Example
314
315 Attached is an example of an oops based template `oopsbase.tmpl` and an example oops dialog `oopstest.tmpl` based on the base template. %A% **NOTE:** This isn't the release version, just a quick, simple demo.
316
317 ### <a name="Base template oopsbase.tmpl"></a> Base template oopsbase.tmpl
318
319 The first line declares a delimiter variable called "sep", used to separate multiple link items. The variable can be called anywhere by writing `%TMPL:P{"sep"}%`
320
321 > <table bgcolor="#f5f5f5" border="1" cellpadding="1" cellspacing="0">
322 >   <tr>
323 >     <td><pre>
324 > %TMPL:DEF{"sep"}% | %TMPL:END%
325 > &lt;html&gt;
326 > &lt;head&gt;
327 >   &lt;title&gt; %WIKITOOLNAME% . %WEB% . %TOPIC% %.TMPL:P{"titleaction"}%&lt;/title&gt;
328 >   &lt;base href="%SCRIPTURL%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%"&gt;
329 >   &lt;meta name="robots" content="noindex"&gt;
330 > &lt;/head&gt;
331 > &lt;body bgcolor="#FFFFFF"&gt;
332 > &lt;table width="100%" border="0" cellpadding="3" cellspacing="0"&gt;
333 >   &lt;tr&gt;
334 >        &lt;td bgcolor="%WEBBGCOLOR%" rowspan="2" valign="top" width="1%"&gt;
335 >               &lt;a href="%WIKIHOMEURL%"&gt;
336 >               &lt;img src="%PUBURLPATH%/wikiHome.gif" border="0"&gt;&lt;/a&gt;
337 >        &lt;/td&gt;
338 >        &lt;td&gt;
339 >               &lt;b&gt;%WIKITOOLNAME% . %WEB% . &lt;/b&gt;&lt;font size="+2"&gt;
340 >               &lt;B&gt;%TOPIC%&lt;/b&gt; %TMPL:P{"titleaction"}%&lt;/font&gt;
341 >        &lt;/td&gt;
342 >   &lt;/tr&gt;
343 >   &lt;tr bgcolor="%WEBBGCOLOR%"&gt;
344 >        &lt;td colspan="2"&gt;
345 >               %TMPL:P{"webaction"}%
346 >        &lt;/td&gt;
347 >   &lt;/tr&gt;
348 > &lt;/table&gt;
349 > --- ++ %TMPL:P{"heading"}%
350 > %TMPL:P{"message"}%
351 > &lt;table width="100%" border="0" cellpadding="3" cellspacing="0"&gt;
352 >   &lt;tr bgcolor="%WEBBGCOLOR%"&gt;
353 >        &lt;td valign="top"&gt;
354 >               Topic &lt;b&gt;%TOPIC%&lt;/b&gt; . {
355 >                 %TMPL:P{"topicaction"}%
356 >               }
357 >        &lt;/td&gt;
358 >   &lt;/tr&gt;
359 > &lt;/table&gt;
360 > &lt;/body&gt;
361 > </pre></td>
362 >   </tr>
363 > </table>
364
365 ### <a name="Test template oopstest.tmpl"></a> Test template oopstest.tmpl
366
367 Each oops template basically just defines some variables and includes the base template that does the layout work.
368
369 > <table bgcolor="#f5f5f5" border="1" cellpadding="1" cellspacing="0">
370 >   <tr>
371 >     <td><pre>
372 > %TMPL:DEF{"titleaction"}% (test =titleaction=) %TMPL:END%
373 > %TMPL:DEF{"webaction"}% test =webaction= %TMPL:END%
374 > %TMPL:DEF{"heading"}%
375 > Test heading %TMPL:END%
376 > %TMPL:DEF{"message"}%
377 > Test =message=. Blah blah blah blah blah blah blah blah blah blah blah...
378 >
379 >       * Some more blah blah blah blah blah blah blah blah blah blah...
380 >       * Param1: %PARAM1%
381 >       * Param2: %PARAM2%
382 >       * Param3: %PARAM3%
383 >       * Param4: %PARAM4%
384 > %TMPL:END%
385 > %TMPL:DEF{"topicaction"}%
386 > Test =topicaction=:
387 > [[%WEB%.%TOPIC%][OK]] %TMPL:P{"sep"}%
388 > [[%TWIKIWEB%.TWikiRegistration][Register]] %TMPL:END%
389 > %TMPL:INCLUDE{"oopsbase"}%
390 > </pre></td>
391 >   </tr>
392 >   <tr>
393 >     <td> &lt;/table &gt; </td>
394 >   </tr>
395 > </table>
396
397 ### <a name="Sample screen shot of oopstest.t"></a> Sample screen shot of oopstest.tmpl
398
399 With URL: <code>**.../bin/oops/Sandbox/TestTopic2?template=oopstest&amp;param1=WebHome&amp;param2=WebNotify**</code>
400
401 > <table border="1" cellpadding="0" cellspacing="0">
402 >   <tr>
403 >     <td><img alt="testscreen.gif" height="304" src="http://www.dementia.org/twiki//view/testscreen.gif" width="589" /></td>
404 >   </tr>
405 > </table>
406
407 ## <a name="Known Issues"></a> Known Issues
408
409 - A drawback of referring to a master template is that you can only test a template from within TWiki, where the include variables are resolved. In the previous system, each template was a structurally complete HTML document with a `.tmpl` filename extension - it contained unresolved `%VARIABLES%`, but could still be previewed directly in a browser.
410
411 -- TWiki:Main.CrawfordCurrie - 30 Jun 2004 %BR% -- TWiki:Main.PeterThoeny - 15 Aug 2004 %BR% -- TWiki:Main.MikeMannix - 14 Sep 2001 %BR% -- TWiki:Main.DavidLeBlanc - 11 Mar 2002