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. Templates are used to define [[page layout|Main/WebHome#HtmlTemplates]], and also to supply[[default content|Main/WebHome#TemplateTopics]] for new pages.
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="HtmlTemplates"></a>
103
104 ### <a name="HTML Page Templates"></a> HTML Page Templates
105
106 %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.
107
108 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:
109
110 1. <code>**twiki/templates/$webName/$scriptName.tmpl**</code>
111 2. <code>**twiki/templates/$scriptName.tmpl**</code>
112
113 **_Note:_** `$webName` is the name of the web (ex: `Main`), and <code>**$scriptName**</code> is the script (ex: `view`).
114
115 **_Note:_** [[TWikiSkins]] can be defined to overload the standard templates.
116
117 Special variables are used in templates, especially in `view`, to display [[meta data|Main/TWikiMetaData#MetaDataRendering]].
118
119 <a name="TemplateTopics"></a>
120
121 ### <a name="Template Topics"></a> Template Topics
122
123 Template topics define the default text for new topics. There are three types of template topics:
124
125 > <table border="1" cellpadding="0" cellspacing="0">
126 >   <tr>
127 >     <th bgcolor="#99CCCC"><strong> Topic Name: </strong></th>
128 >     <th bgcolor="#99CCCC"><strong> What it is: </strong></th>
129 >   </tr>
130 >   <tr>
131 >     <td>[[Main/WebTopicViewTemplate]]</td>
132 >     <td> Help text shown when you view a non existing topic. </td>
133 >   </tr>
134 >   <tr>
135 >     <td>[[Main/WebTopicNonWikiTemplate]]</td>
136 >     <td> Help text shown when you view a non existing topic that has not a [[Main/WikiName]]. </td>
137 >   </tr>
138 >   <tr>
139 >     <td>[[Main/WebTopicEditTemplate]]</td>
140 >     <td> Default text shown when you create a new topic. </td>
141 >   </tr>
142 > </table>
143
144 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:
145
146 1. The topic name specified by the `templatetopic` CGI parameter.
147 2. WebTopicEditTemplate in the current web.
148 3. WebTopicEditTemplate in the TWiki web.
149
150 #### <a name="Template Topics in Action"></a> Template Topics in Action
151
152 Here is an example for creating new topics based on a specific template topic:
153
154 <form action="http://www.dementia.org/twiki//edit/%WEB%/" name="new">
155   <ul>
156     <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>
157   </ul>
158 </form>
159
160 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:
161
162     <form name="new" action="%SCRIPTURLPATH%/edit%SCRIPTSUFFIX%/%WEB%/">
163         * New example topic:
164           <input type="text" name="topic" value="ExampleTopic%SERVERTIME{$yearx$mox$day}%" size="23" />
165           <input type="hidden" name="templatetopic" value="ExampleTopicTemplate" />
166           <input type="hidden" name="onlywikiname" value="on" />
167           <input type="submit" value="Create" />
168           (date format is <nop>YYYYxMMxDD)
169     </form>
170
171 The "onlywikiname" parameter enforces [[WikiWords]] for topic names.
172
173 **_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%`
174
175 ## <a name="Templates by Example"></a> Templates by Example
176
177 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.
178
179 ### <a name="Base template oopsbase.tmpl"></a> Base template oopsbase.tmpl
180
181 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"}%`
182
183 > <table bgcolor="#f5f5f5" border="1" cellpadding="1" cellspacing="0">
184 >   <tr>
185 >     <td><pre>
186 > %TMPL:DEF{"sep"}% | %TMPL:END%
187 > &lt;html&gt;
188 > &lt;head&gt;
189 >   &lt;title&gt; %WIKITOOLNAME% . %WEB% . %TOPIC% %.TMPL:P{"titleaction"}%&lt;/title&gt;
190 >   &lt;base href="%SCRIPTURL%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%"&gt;
191 >   &lt;meta name="robots" content="noindex"&gt;
192 > &lt;/head&gt;
193 > &lt;body bgcolor="#FFFFFF"&gt;
194 > &lt;table width="100%" border="0" cellpadding="3" cellspacing="0"&gt;
195 >   &lt;tr&gt;
196 >        &lt;td bgcolor="%WEBBGCOLOR%" rowspan="2" valign="top" width="1%"&gt;
197 >               &lt;a href="%WIKIHOMEURL%"&gt;
198 >               &lt;img src="%PUBURLPATH%/wikiHome.gif" border="0"&gt;&lt;/a&gt;
199 >        &lt;/td&gt;
200 >        &lt;td&gt;
201 >               &lt;b&gt;%WIKITOOLNAME% . %WEB% . &lt;/b&gt;&lt;font size="+2"&gt;
202 >               &lt;B&gt;%TOPIC%&lt;/b&gt; %TMPL:P{"titleaction"}%&lt;/font&gt;
203 >        &lt;/td&gt;
204 >   &lt;/tr&gt;
205 >   &lt;tr bgcolor="%WEBBGCOLOR%"&gt;
206 >        &lt;td colspan="2"&gt;
207 >               %TMPL:P{"webaction"}%
208 >        &lt;/td&gt;
209 >   &lt;/tr&gt;
210 > &lt;/table&gt;
211 > --- ++ %TMPL:P{"heading"}%
212 > %TMPL:P{"message"}%
213 > &lt;table width="100%" border="0" cellpadding="3" cellspacing="0"&gt;
214 >   &lt;tr bgcolor="%WEBBGCOLOR%"&gt;
215 >        &lt;td valign="top"&gt;
216 >               Topic &lt;b&gt;%TOPIC%&lt;/b&gt; . {
217 >                 %TMPL:P{"topicaction"}%
218 >               }
219 >        &lt;/td&gt;
220 >   &lt;/tr&gt;
221 > &lt;/table&gt;
222 > &lt;/body&gt;
223 > </pre></td>
224 >   </tr>
225 > </table>
226
227 ### <a name="Test template oopstest.tmpl"></a> Test template oopstest.tmpl
228
229 Each oops template basically just defines some variables and includes the base template that does the layout work.
230
231 > <table bgcolor="#f5f5f5" border="1" cellpadding="1" cellspacing="0">
232 >   <tr>
233 >     <td><pre>
234 > %TMPL:DEF{"titleaction"}% (test =titleaction=) %TMPL:END%
235 > %TMPL:DEF{"webaction"}% test =webaction= %TMPL:END%
236 > %TMPL:DEF{"heading"}%
237 > Test heading %TMPL:END%
238 > %TMPL:DEF{"message"}%
239 > Test =message=. Blah blah blah blah blah blah blah blah blah blah blah...
240 >
241 >       * Some more blah blah blah blah blah blah blah blah blah blah...
242 >       * Param1: %PARAM1%
243 >       * Param2: %PARAM2%
244 >       * Param3: %PARAM3%
245 >       * Param4: %PARAM4%
246 > %TMPL:END%
247 > %TMPL:DEF{"topicaction"}%
248 > Test =topicaction=:
249 > [[%WEB%.%TOPIC%][OK]] %TMPL:P{"sep"}%
250 > [[%TWIKIWEB%.TWikiRegistration][Register]] %TMPL:END%
251 > %TMPL:INCLUDE{"oopsbase"}%
252 > </pre></td>
253 >   </tr>
254 >   <tr>
255 >     <td> &lt;/table &gt; </td>
256 >   </tr>
257 > </table>
258
259 ### <a name="Sample screen shot of oopstest.t"></a> Sample screen shot of oopstest.tmpl
260
261 With URL: <code>**.../bin/oops/Sandbox/TestTopic2?template=oopstest&amp;param1=WebHome&amp;param2=WebNotify**</code>
262
263 > <table border="1" cellpadding="0" cellspacing="0">
264 >   <tr>
265 >     <td><img alt="testscreen.gif" height="304" src="http://www.dementia.org/twiki//view/testscreen.gif" width="589" /></td>
266 >   </tr>
267 > </table>
268
269 ## <a name="Known Issues"></a> Known Issues
270
271 - 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.
272
273 -- [[PeterThoeny]] - 23 Jul 2001 <br /> -- [[MikeMannix]] - 14 Sep 2001 <br />