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="#Functional Specifications"> Functional Specifications</a></li>
7         <li><a href="#TWiki Master Template"> TWiki Master Template</a></li>
8         <li><a href="#Types of Template"> Types of Template</a><ul>
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="#Template Topics in Action"> Template Topics in Action</a></li>
12               </ul>
13             </li>
14           </ul>
15         </li>
16         <li><a href="#Templates by Example"> Templates by Example</a><ul>
17             <li><a href="#Base template oopsbase.tmpl"> Base template oopsbase.tmpl</a></li>
18             <li><a href="#Test template oopstest.tmpl"> Test template oopstest.tmpl</a></li>
19             <li><a href="#Sample screen shot of oopstest.t"> Sample screen shot of oopstest.tmpl</a></li>
20           </ul>
21         </li>
22         <li><a href="#Known Issues"> Known Issues</a></li>
23       </ul>
24     </li>
25   </ul>
26 </div>
27
28 # <a name="TWiki Templates"></a> TWiki Templates
29
30 _Definition of the templates used to render all HTML pages displayed in TWiki_
31
32 ## <a name="Overview"></a> Overview
33
34 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 mark-up for template-specific content.
35
36 ## <a name="Major changes from the previous"></a><a name="Major changes from the previous "></a> Major changes from the previous template system
37
38 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:
39
40 - separates a set of common template parts into a base template that is included by all of the related templates;
41 - defines common variables, like a standard separator (ex: "|"), in the base template;
42 - defines variable text in the individual templates and passes it back to the base template.
43
44 ## <a name="Functional Specifications"></a> Functional Specifications
45
46 - Special template directives (or preprocessor commands) are embedded in normal templates.
47 - Use of template directives is optional, templates work without them.
48 - All template preprocessing is done in `&TWiki::Store::readTemplate()` so that the caller simply gets an expanded template file (the same as before).
49 - Directives are of the form `%TMPL:<key>%` and `%TMPL:<key>{"attr"}%`.
50 - Directives:
51   - `%TMPL:INCLUDE{"file"}%`: Includes a template file. The template directory of the current web is searched first, then the templates root (`twiki/templates`).
52   - `%TMPL:DEF{"var"}%`: Define a variable. Text between this and the END directive is not returned, but put into a hash for later use.
53   - `%TMPL:END%`: Ends variable definition.
54   - `%TMPL:P{"var"}%`: Prints a previously defined variable.
55 - Variables are live in a global name space, there is no parameter passing.
56 - Two-pass processing, so that you can use a variable before declaring it or after.
57 - Templates and [[TWikiSkins]] work transparently and interchangeably. For example, you can create a skin that overloads just the `twiki.tmpl`, like `twiki.print.tmpl`, that redefines the header and footer.
58 - **_NOTE:_** The template directives work only for templates, they do not get processed in topic text.
59
60 ## <a name="TWiki Master Template"></a> TWiki Master Template
61
62 All common parts are defined in a master template, <code>**twiki.tmpl**</code>, that all other templates use.
63
64 > <table border="1" cellpadding="0" cellspacing="0">
65 >   <tr>
66 >     <th bgcolor="#99CCCC"><strong> Template variable: </strong></th>
67 >     <th bgcolor="#99CCCC"><strong> Defines: </strong></th>
68 >   </tr>
69 >   <tr>
70 >     <td> %TMPL:DEF{"sep"}% </td>
71 >     <td> "|" separator </td>
72 >   </tr>
73 >   <tr>
74 >     <td> %TMPL:DEF{"htmldoctype"}% </td>
75 >     <td> Start of all HTML pages </td>
76 >   </tr>
77 >   <tr>
78 >     <td> %TMPL:DEF{"standardheader"}% </td>
79 >     <td> Standard header (ex: view, index, seach) </td>
80 >   </tr>
81 >   <tr>
82 >     <td> %TMPL:DEF{"simpleheader"}% </td>
83 >     <td> Simple header with reduced links (ex: edit, attach, oops) </td>
84 >   </tr>
85 >   <tr>
86 >     <td> %TMPL:DEF{"standardfooter"}% </td>
87 >     <td> Footer, excluding revision and copyright parts </td>
88 >   </tr>
89 >   <tr>
90 >     <td> %TMPL:DEF{"oops"}% </td>
91 >     <td> Skeleton of oops dialog </td>
92 >   </tr>
93 > </table>
94
95 ## <a name="Types of Template"></a> Types of Template
96
97 There are two types of templates:
98
99 - **HTML Page Templates**: Defines layout of %WIKITOOLNAME% pages
100 - **Template Topics**: Defines default text when you create a new topic
101
102 ### <a name="HTML Page Templates"></a> HTML Page Templates
103
104 %WIKITOOLNAME% uses HTML template files for all actions like topic view, edit, preview and so on. This allows you to change the look and feel of all pages by editing just some template files.
105
106 The template files are in the <code>**twiki/templates**</code> directory. As an example, <code>**twiki/templates/view.tmpl**</code> is the template file for the <code>**twiki/bin/view**</code> script. Templates can be overloaded per web. The following search order applies:
107
108 1. <code>**twiki/templates/$webName/$scriptName.tmpl**</code>
109 2. <code>**twiki/templates/$scriptName.tmpl**</code>
110
111 **_Note:_** `$webName` is the name of the web (ex: `Main`), and <code>**$scriptName**</code> is the script (ex: `view`).
112
113 **_Note:_** [[TWikiSkins]] can be defined to overload the standard templates.
114
115 Special variables are used in templates, especially in `view`, to display [[meta data|Main/TWikiMetaData#MetaDataRendering]].
116
117 ### <a name="Template Topics"></a> Template Topics
118
119 Template topics define the default text for new topics. There are three types of template topics:
120
121 > <table border="1" cellpadding="0" cellspacing="0">
122 >   <tr>
123 >     <th bgcolor="#99CCCC"><strong> Topic Name: </strong></th>
124 >     <th bgcolor="#99CCCC"><strong> What it is: </strong></th>
125 >   </tr>
126 >   <tr>
127 >     <td>[[Main/WebTopicViewTemplate]]</td>
128 >     <td> Help text shown when you view a non existing topic. </td>
129 >   </tr>
130 >   <tr>
131 >     <td>[[Main/WebTopicNonWikiTemplate]]</td>
132 >     <td> Help text shown when you view a non existing topic that has not a [[Main/WikiName]]. </td>
133 >   </tr>
134 >   <tr>
135 >     <td>[[Main/WebTopicEditTemplate]]</td>
136 >     <td> Default text shown when you create a new topic. </td>
137 >   </tr>
138 > </table>
139
140 All template topics are located in the TWiki web. The [[WebTopicEditTemplate]] can be overloaded. The following search order applies when you create a new topic:
141
142 1. The topic name specified by the `templatetopic` CGI parameter.
143 2. WebTopicEditTemplate in the current web.
144 3. WebTopicEditTemplate in the TWiki web.
145
146 #### <a name="Template Topics in Action"></a> Template Topics in Action
147
148 Here is an example for creating new topics based on a specific template topic:
149
150 <form action="http://www.dementia.org/twiki//edit/%WEB%/" name="new">
151   <ul>
152     <li> New example topic: <input name="topic" size="23" type="text" value="ExampleTopic2010x06x29" /> <input name="templatetopic" type="hidden" value="ExampleTopicTemplate" /> <input type="submit" value="Create" /> (date format is YYYYxMMxDD) </li>
153   </ul>
154 </form>
155
156 Above form asks for a topic name. A hidden input tag of name "templatetopic" specifies the [[ExampleTopicTemplate]] as the template topic. Here is the HTML source of the form:
157
158     <form name="new" action="%SCRIPTURLPATH%/edit%SCRIPTSUFFIX%/%WEB%/">
159         * New example topic:
160           <input type="text" name="topic" value="ExampleTopic%SERVERTIME{$yearx$mox$day}%" size="23" />
161           <input type="hidden" name="templatetopic" value="ExampleTopicTemplate" />
162           <input type="hidden" name="onlywikiname" value="on" />
163           <input type="submit" value="Create" />
164           (date format is <nop>YYYYxMMxDD)
165     </form>
166
167 The "onlywikiname" parameter enforces [[WikiWords]] for topic names.
168
169 **_Note:_** Use can use the `%WIKIUSERNAME%` and `%DATE%` variables in your topic templates as the signature; those variables are expanded when a new topic is created. The standard topic signature is: <br />`-- %WIKIUSERNAME% - %DATE%`
170
171 ## <a name="Templates by Example"></a> Templates by Example
172
173 Attached is an example of an oops base template `oopsbase.tmpl` and a example oops dialog `oopstest.tmpl` which is based on the base template. **_NOTE:_** This isn't the release version, just a quick, simple demo.
174
175 ### <a name="Base template oopsbase.tmpl"></a> Base template oopsbase.tmpl
176
177 The first line declares the delimiter variable called "sep", used to separate multiple link items. The variable can be called anywhere by writing `%TMPL:P{"sep"}%`
178
179 > <table bgcolor="#f5f5f5" border="1" cellpadding="1" cellspacing="0">
180 >   <tr>
181 >     <td><pre>
182 > %TMPL:DEF{"sep"}% | %TMPL:END%
183 > &lt;html&gt;
184 > &lt;head&gt;
185 >   &lt;title&gt; %WIKITOOLNAME% . %WEB% . %TOPIC% %.TMPL:P{"titleaction"}%&lt;/title&gt;
186 >   &lt;base href="%SCRIPTURL%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%"&gt;
187 >   &lt;meta name="robots" content="noindex"&gt;
188 > &lt;/head&gt;
189 > &lt;body bgcolor="#FFFFFF"&gt;
190 > &lt;table width="100%" border="0" cellpadding="3" cellspacing="0"&gt;
191 >   &lt;tr&gt;
192 >        &lt;td bgcolor="%WEBBGCOLOR%" rowspan="2" valign="top" width="1%"&gt;
193 >               &lt;a href="%WIKIHOMEURL%"&gt;
194 >               &lt;img src="%PUBURLPATH%/wikiHome.gif" border="0"&gt;&lt;/a&gt;
195 >        &lt;/td&gt;
196 >        &lt;td&gt;
197 >               &lt;b&gt;%WIKITOOLNAME% . %WEB% . &lt;/b&gt;&lt;font size="+2"&gt;
198 >               &lt;B&gt;%TOPIC%&lt;/b&gt; %TMPL:P{"titleaction"}%&lt;/font&gt;
199 >        &lt;/td&gt;
200 >   &lt;/tr&gt;
201 >   &lt;tr bgcolor="%WEBBGCOLOR%"&gt;
202 >        &lt;td colspan="2"&gt;
203 >               %TMPL:P{"webaction"}%
204 >        &lt;/td&gt;
205 >   &lt;/tr&gt;
206 > &lt;/table&gt;
207 > --- ++ %TMPL:P{"heading"}%
208 > %TMPL:P{"message"}%
209 > &lt;table width="100%" border="0" cellpadding="3" cellspacing="0"&gt;
210 >   &lt;tr bgcolor="%WEBBGCOLOR%"&gt;
211 >        &lt;td valign="top"&gt;
212 >               Topic &lt;b&gt;%TOPIC%&lt;/b&gt; . {
213 >                 %TMPL:P{"topicaction"}%
214 >               }
215 >        &lt;/td&gt;
216 >   &lt;/tr&gt;
217 > &lt;/table&gt;
218 > &lt;/body&gt;
219 > </pre></td>
220 >   </tr>
221 > </table>
222
223 ### <a name="Test template oopstest.tmpl"></a> Test template oopstest.tmpl
224
225 Each oops template basically just defines some variables and includes the base template that does the layout work.
226
227 > <table bgcolor="#f5f5f5" border="1" cellpadding="1" cellspacing="0">
228 >   <tr>
229 >     <td><pre>
230 > %TMPL:DEF{"titleaction"}% (test =titleaction=) %TMPL:END%
231 > %TMPL:DEF{"webaction"}% test =webaction= %TMPL:END%
232 > %TMPL:DEF{"heading"}%
233 > Test heading %TMPL:END%
234 > %TMPL:DEF{"message"}%
235 > Test =message=. Blah blah blah blah blah blah blah blah blah blah blah...
236 >
237 >       * Some more blah blah blah blah blah blah blah blah blah blah...
238 >       * Param1: %PARAM1%
239 >       * Param2: %PARAM2%
240 >       * Param3: %PARAM3%
241 >       * Param4: %PARAM4%
242 > %TMPL:END%
243 > %TMPL:DEF{"topicaction"}%
244 > Test =topicaction=:
245 > [[%WEB%.%TOPIC%][OK]] %TMPL:P{"sep"}%
246 > [[%TWIKIWEB%.TWikiRegistration][Register]] %TMPL:END%
247 > %TMPL:INCLUDE{"oopsbase"}%
248 > </pre></td>
249 >   </tr>
250 >   <tr>
251 >     <td> &lt;/table &gt; </td>
252 >   </tr>
253 > </table>
254
255 ### <a name="Sample screen shot of oopstest.t"></a> Sample screen shot of oopstest.tmpl
256
257 With URL: <code>**.../bin/oops/Test/TestTopic2?template=oopstest&amp;param1=WebHome&amp;param2=WebNotify**</code>
258
259 > <table border="1" cellpadding="0" cellspacing="0">
260 >   <tr>
261 >     <td><img alt="testscreen.gif" height="304" src="http://www.dementia.org/twiki//view/testscreen.gif" width="554" /></td>
262 >   </tr>
263 > </table>
264
265 ## <a name="Known Issues"></a> Known Issues
266
267 - 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 is a structurally complete HTML document with a `.tmpl` filename extension - it contains unresolved `%VARIABLES%`, but can still be previewed directly in a browser.
268
269 -- [[PeterThoeny]] - 23 Jul 2001 <br /> -- [[MikeMannix]] - 14 Sep 2001 <br />