none
[openafs-wiki.git] / TWiki / TWikiTemplates.mdwn
1 # <a name="new topic for the final doc"></a> new topic for the final doc
2
3 Slightly edited down, but waiting for new docs, hopefully.
4
5 see also for ref: orig [[TWikiTemplatingSystem]]
6
7 <div>
8   <ul>
9     <li><a href="#new topic for the final doc"> new topic for the final doc</a></li>
10     <li><a href="#TWiki Template System"> TWiki Template System</a></li>
11     <li><a href="#Overview"> Overview</a><ul>
12         <li><a href="#Needs of the TWiki templating sy"> Needs of the TWiki templating system</a></li>
13         <li><a href="#Functional Spec"> Functional Spec</a></li>
14         <li><a href="#Examples"> Examples</a><ul>
15             <li>
16               <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           </ul>
23         </li>
24         <li><a href="#Comments and feedback"> Comments and feedback</a></li>
25       </ul>
26     </li>
27   </ul>
28 </div>
29
30 # <a name="TWiki Template System"></a> TWiki Template System
31
32 _Define the templates used to render all HTML pages displayed in TWiki_
33
34 # <a name="Overview"></a> Overview
35
36 We have a need for more advanced template handling. As [[JohnTalintyre]] pointed out in [[CommonHeaderFooterTemplate]] it makes sense to separate the header and footer into one file so that it can be easily altered (or even overloaded by a skin). Also the oops dialog messages are all identical except for a few variables like heading, and so on.
37
38 Using external modules like the [[TemplateToolkit]] would be one way to go, but this will add a lot of baggage to TWiki.
39
40 ## <a name="Needs of the TWiki templating sy"></a> Needs of the TWiki templating system
41
42 - Separate common parts into one (or more) base template file(s) and include that from other template files like `view.tmpl`.
43 - Define common variables like a "|" separator in the base template and use them in other template files
44 - Define variable text in templates (i.e. `view.tmpl`) and pass them to the base template
45
46 ## <a name="Functional Spec"></a> Functional Spec
47
48 I tried to define a simple but powerful solution that can be extended over time. Here we go:
49
50 - Special template directives (or preprocessor commands) are embedded in normal templates.
51 - Use of template directives is optional, templates work without them.
52 - All template preprocessing is done in `&TWiki::Store::readTemplate()` so that the caller simply gets an expanded template file (the same as before)
53 - Directives are of form `%TMPL:<key>%` and `%TMPL:<key>{"attr"}%`.
54 - Initial set of directives:
55   - `%TMPL:INCLUDE{"file"}%`: Includes a template file. The usual search path is applied.
56   - `%TMPL:DEF{"var"}%`: Define a variable. Text between this and the END directive is not returned, but put into a hash for later use.
57   - `%TMPL:END%`: Ends variable definition.
58   - `%TMPL:P{"var"}%`: Prints a previously defined variable.
59 - New directives can be added over time when needed, i.e. IF-THEN-ELSE.
60 - Variables live in a global name space, there is no parameter passing.
61 - Two pass processing, so that you can use a variable before declaring it or after.
62 - Templates and skins work transparently and interchangeably. You could for example define a new skin just for the header &amp; footer and keep the other template files unchanged.
63 - **_Note:_** The template directive work only for templates, they do not get processed in topic text.
64
65 ## <a name="Examples"></a> Examples
66
67 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. This is not the version that will go into the release, it is just a quick hack.
68
69 #### <a name="Base template oopsbase.tmpl"></a> Base template oopsbase.tmpl
70
71 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"}%`
72
73 **_Note:_** Added a dot to escape rendering of variables, i.e. read <code>**%.WEB%**</code> as <code>**%WEB%**</code>.
74
75     <table border="1" cellspacing="0" cellpadding="1">
76     <tr><td>
77     <verbatim>
78     %.TMPL:DEF{"sep"}% | %.TMPL:END%
79     <html>
80     <head>
81       <title> %.WIKITOOLNAME% . %.WEB% . %.TOPIC% %.TMPL:P{"titleaction"}%</title>
82       <base href="%.SCRIPTURL%/view%.SCRIPTSUFFIX%/%.WEB%/%.TOPIC%">
83       <meta name="robots" content="noindex">
84     </head>
85     <body bgcolor="#FFFFFF">
86     <table width="100%" border="0" cellpadding="3" cellspacing="0">
87       <tr>
88          <td bgcolor="%.WEBBGCOLOR%" rowspan="2" valign="top" width="1%">
89                 <a href="%.WIKIHOMEURL%">
90                 <img src="%.PUBURLPATH%/wikiHome.gif" border="0"></a>
91          </td>
92          <td>
93                 <b>%.WIKITOOLNAME% . %.WEB% . </b><font size="+2">
94                 <B>%.TOPIC%</b> %.TMPL:P{"titleaction"}%</font>
95          </td>
96       </tr>
97       <tr bgcolor="%.WEBBGCOLOR%">
98          <td colspan="2">
99                 %.TMPL:P{"webaction"}%
100          </td>
101       </tr>
102     </table>
103     --- ++ %.TMPL:P{"heading"}%
104     %.TMPL:P{"message"}%
105     <table width="100%" border="0" cellpadding="3" cellspacing="0">
106       <tr bgcolor="%.WEBBGCOLOR%">
107          <td valign="top">
108                 Topic <b>%TOPIC%</b> . {
109                   %.TMPL:P{"topicaction"}%
110                 }
111          </td>
112       </tr>
113     </table>
114     </body>
115     </verbatim>
116     </td></tr>
117     </table >
118
119 #### <a name="Test template oopstest.tmpl"></a> Test template oopstest.tmpl
120
121 Each oops template basically just defines some variables and includes the base template that does the layout work.
122
123 <table border="1" cellpadding="1" cellspacing="0">
124   <tr>
125     <td><pre>
126 %.TMPL:DEF{"titleaction"}% (test =titleaction=) %.TMPL:END%
127 %.TMPL:DEF{"webaction"}% test =webaction= %.TMPL:END%
128 %.TMPL:DEF{"heading"}%
129 Test heading %.TMPL:END%
130 %.TMPL:DEF{"message"}%
131 Test =message=. Blah blah blah blah blah blah blah blah blah blah blah...
132
133    * Some more blah blah blah blah blah blah blah blah blah blah...
134    * Param1: %PARAM1%
135    * Param2: %PARAM2%
136    * Param3: %PARAM3%
137    * Param4: %PARAM4%
138 %.TMPL:END%
139 %.TMPL:DEF{"topicaction"}%
140 Test =topicaction=:
141 [[%.WEB%.%TOPIC%][OK]] %.TMPL:P{"sep"}%
142 [[%.TWIKIWEB%.TWikiRegistration][Register]] %.TMPL:END%
143 %.TMPL:INCLUDE{"oopsbase"}%
144 </pre></td>
145   </tr>
146   <tr>
147     <td> &lt;/table &gt; <p>
148       </p>
149       <h4><a name="Sample screen shot of oopstest.t"></a> Sample screen shot of oopstest.tmpl </h4>
150       <p> With URL: <code><b>.../bin/oops/Test/TestTopic2?template=oopstest&amp;param1=WebHome&amp;param2=WebNotify</b></code></p>
151       <p>
152       </p>
153       <table border="1" cellpadding="0" cellspacing="0">
154         <tr>
155           <td><img alt="testscreen.gif" height="304" src="http://www.dementia.org/twiki//view/testscreen.gif" width="554" /></td>
156         </tr>
157       </table>
158       <p>
159       </p>
160       <h2><a name="Comments and feedback"></a> Comments and feedback </h2>
161       <p>
162       </p>
163       <ul>
164         <li> The itching factor to put this into the upcoming release is to easy the pending converstion of the templates into XHTML format and to have a more flexible solution for templates and skins. </li>
165         <li> Is the terminology OK? </li>
166         <li> Is the spec OK? </li>
167         <li> Are there any other directives that are needed urgently? </li>
168         <li> One drawback by using the directives is that you can only test a template from within TWiki. This is because you don't have a text that has a linear flow when you use TMPL:DEF, TMPL:P and TMPL:INCLUDE. </li>
169       </ul>
170       <p> -- [[Main/PeterThoeny]] - 21 Jul 2001 <br />
171       </p>
172       <p>
173       </p>
174       <ul>
175         <li> It's probably more readable to enforce variables being defined before use, which would avoid need for slower two-pass processing. <ul>
176             <li> [ [[Main/PeterThoeny]] ] You need two-pass processing because you need to define the "|" separator in the included template onone side, and on the other side define variables for the included template. The processing happens as regex in memory, so there is no mesurable speed penalty. </li>
177           </ul>
178         </li>
179         <li> It would be nice to simplify the syntax a bit, e.g. not using "" around variable names (they don't really need it, unlike filenames), and not using braces - however, this is not a big deal and there is some merit in keeping consistent with the current TWiki syntax. <ul>
180             <li> [ [[Main/PeterThoeny]] ] The templating system uses the standard internal TWiki function to parse variable attributes. The "" can be skipped (is possible but is not documented). </li>
181           </ul>
182         </li>
183       </ul>
184       <p>
185       </p>
186       <p> Please have a look at the latest templates in the [[Main/TWikiAlphaRelease]]. There is now one master template called <code>twiki.tmpl</code> that all other templates include (well, will include when all done). The idea is to define all common parts of the templates in <code>twiki.tmpl</code> and simply use that from all other templates. </p>
187       <p>
188       </p>
189       <table border="1" cellpadding="0" cellspacing="0">
190         <tr>
191           <th bgcolor="#99CCCC"><strong> Template variable: </strong></th>
192           <th bgcolor="#99CCCC"><strong> Defines: </strong></th>
193         </tr>
194         <tr>
195           <td> %TMPL:DEF{"sep"}% </td>
196           <td> %TMPL:END% </td>
197           <td> "|" separator </td>
198         </tr>
199         <tr>
200           <td> %TMPL:DEF{"htmldoctype"}% </td>
201           <td> Start of all HTML pages </td>
202         </tr>
203         <tr>
204           <td> %TMPL:DEF{"standardheader"}% </td>
205           <td> Standard header (for view, rdiff, ... </td>
206         </tr>
207         <tr>
208           <td> %TMPL:DEF{"simpleheader"}% </td>
209           <td> Simple header with reduced links (for edit, attach, oops,...) </td>
210         </tr>
211         <tr>
212           <td> %TMPL:DEF{"standardfooter"}% </td>
213           <td> Footer, excluding revision part and copyright part </td>
214         </tr>
215         <tr>
216           <td> %TMPL:DEF{"oops"}% </td>
217           <td> Skeleton of oops dialog </td>
218         </tr>
219       </table>
220       <p> I.e. the <code>preview.tmpl</code> template is now simply: </p><pre>
221 %TMPL:INCLUDE{"twiki"}%
222 %TMPL:DEF{"titleaction"}% (oops) %TMPL:END%
223 %TMPL:DEF{"webaction"}% *Attention* %TMPL:END%
224 %TMPL:DEF{"heading"}% Topic is not saved yet %TMPL:END%
225 %TMPL:DEF{"message"}%
226   Please go back in your browser and save the topic. %TMPL:END%
227 %TMPL:DEF{"topicaction"}% %TMPL:END%
228 %TMPL:P{"oops"}%
229 </pre><p> With this it should be possible to create a skin that overloads just the <code>twiki.tmpl</code>, i.e. a <code>twiki.print.tmpl</code> that redefines the header and footer. </p>
230       <p> -- [[Main/PeterThoeny]] - 23 Jul 2001 <br /> -- [[Main/MikeMannix]] - 30 Aug 2001 </p>
231       <p>
232       </p>
233     </td>
234   </tr>
235 </table>