1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
%TOC% %STARTINCLUDE%
# <a name="TWiki_Template_System"> TWiki Template System </a>
_Definition of the templates used to render all HTML pages displayed in TWiki_
## <a name="Overview"> Overview </a>
The new modular template system is more flexible, efficient, and easily updated than the old set-up, where each template is a complete HTML file. The new master template approach places common templates parts, like headers and footers, in one shared file. This simplifies the conversion of templates into XHTML format, and provides a more versatile solution for templates and for [[TWikiDocumentation]].
## <a name="Major_changes_from_the_previous_"> Major changes from the previous template system </a>
The main difference is that templates are now defined using variables to include template parts. You change one stored instance of a common element to update all occurrences. The new system:
* separates a set of common template parts into a base template that is included by all of the related templates;
* defines common variables, like a standard separator (ex: "|"), in the base template;
* defines variable text in the individual templates and passes it back to the base template.
## <a name="Functional_Specifications"> Functional Specifications </a>
* Special template directives (or preprocessor commands) are embedded in normal templates.
* Use of template directives is optional, templates work without them.
* All template preprocessing is done in `&TWiki::Store::readTemplate()` so that the caller simply gets an expanded template file (the same as before).
* Directives are of the form <code>%TMPL:<key>%</code> and <code>%TMPL:<key>\{"attr"\}%</code>.
* Initial set of directives:
* <code>%TMPL:INCLUDE\{"file"\}%</code>: Includes a template file. The usual search path is applied.
* <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.
* <code>%TMPL:END%</code>: Ends variable definition.
* <code>%TMPL:P\{"var"\}%</code>: Prints a previously defined variable.
* New directives can be added over time when needed, ex: IF-THEN-ELSE.
* Variables are live in a global name space, there is no parameter passing.
* Two-pass processing, so that you can use a variable before declaring it or after.
* Templates and [[TWikiDocumentation]] 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.
* **_Note:_** The template directives work only for templates, they do not get processed in topic text.
## <a name="New_Template_System_by_Example"> New Template System by Example </a>
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.
### <a name="Base_template_oopsbase_tmpl"> Base template oopsbase.tmpl </a>
The first line declares the delimiter variable called "sep", used to separate multiple link items. The variable can be called anywhere by writing <code>%TMPL:P\{"sep"\}%</code>
**_NOTE:_** Added a dot to escape rendering of variables, i.e. read <code>**%.WEB%**</code> as <code>**%WEB%**</code>.
> <table border="1" cellspacing="0" cellpadding="1">
> <tr><td>
> <verbatim>
> %.TMPL:DEF{"sep"}% | %.TMPL:END%
> <html>
> <head>
> <title> %.WIKITOOLNAME% . %.WEB% . %.TOPIC% %.TMPL:P{"titleaction"}%</title>
> <base href="%.SCRIPTURL%/view%.SCRIPTSUFFIX%/%.WEB%/%.TOPIC%">
> <meta name="robots" content="noindex">
> </head>
> <body bgcolor="#FFFFFF">
> <table width="100%" border="0" cellpadding="3" cellspacing="0">
> <tr>
> <td bgcolor="%.WEBBGCOLOR%" rowspan="2" valign="top" width="1%">
> <a href="%.WIKIHOMEURL%">
> <img src="%.PUBURLPATH%/wikiHome.gif" border="0"></a>
> </td>
> <td>
> <b>%.WIKITOOLNAME% . %.WEB% . </b><font size="+2">
> <B>%.TOPIC%</b> %.TMPL:P{"titleaction"}%</font>
> </td>
> </tr>
> <tr bgcolor="%.WEBBGCOLOR%">
> <td colspan="2">
> %.TMPL:P{"webaction"}%
> </td>
> </tr>
> </table>
> --- ++ %.TMPL:P{"heading"}%
> %.TMPL:P{"message"}%
> <table width="100%" border="0" cellpadding="3" cellspacing="0">
> <tr bgcolor="%.WEBBGCOLOR%">
> <td valign="top">
> Topic <b>%TOPIC%</b> . {
> %.TMPL:P{"topicaction"}%
> }
> </td>
> </tr>
> </table>
> </body>
> </verbatim>
> </td></tr>
> </table >
### <a name="Test_template_oopstest_tmpl"> Test template oopstest.tmpl </a>
Each oops template basically just defines some variables and includes the base template that does the layout work.
> <table border="1" cellpadding="1" cellspacing="0">
> <tr>
> <td><pre>
> %.TMPL:DEF{"titleaction"}% (test =titleaction=) %.TMPL:END%
> %.TMPL:DEF{"webaction"}% test =webaction= %.TMPL:END%
> %.TMPL:DEF{"heading"}%
> Test heading %.TMPL:END%
> %.TMPL:DEF{"message"}%
> Test =message=. Blah blah blah blah blah blah blah blah blah blah blah...
>
> * Some more blah blah blah blah blah blah blah blah blah blah...
> * Param1: %PARAM1%
> * Param2: %PARAM2%
> * Param3: %PARAM3%
> * Param4: %PARAM4%
> %.TMPL:END%
> %.TMPL:DEF{"topicaction"}%
> Test =topicaction=:
> [[%.WEB%.%TOPIC%][OK]] %.TMPL:P{"sep"}%
> [[%.TWIKIWEB%.TWikiRegistration][Register]] %.TMPL:END%
> %.TMPL:INCLUDE{"oopsbase"}%
> </pre></td>
> </tr>
> </table>
### <a name="Sample_screen_shot_of_oopstest_t"> Sample screen shot of oopstest.tmpl </a>
With URL: <code>**.../bin/oops/Test/TestTopic2?template=oopstest&param1=WebHome&param2=WebNotify**</code>
> <table border="1" cellpadding="1" cellspacing="0">
> <tr>
> <td><img alt="testscreen.gif" height="304" src="%ATTACHURLPATH%/testscreen.gif" width="554" /></td>
> </tr>
> </table>
All common template parts are defined in one master template, `twiki.tmpl`, that all other templates include.
<table border="1" cellpadding="1" cellspacing="0">
<tr>
<th bgcolor="#99CCCC"><strong>Template variable:</strong></th>
<th bgcolor="#99CCCC"><strong>Defines:</strong></th>
</tr>
<tr>
<td> %<nop>TMPL:DEF{"sep"}% </nop></td>
<td> "|" separator </td>
</tr>
<tr>
<td> %<nop>TMPL:DEF{"htmldoctype"}% </nop></td>
<td> Start of all HTML pages </td>
</tr>
<tr>
<td> %<nop>TMPL:DEF{"standardheader"}% </nop></td>
<td> Standard header (ex: view, index, seach) </td>
</tr>
<tr>
<td> %<nop>TMPL:DEF{"simpleheader"}% </nop></td>
<td> Simple header with reduced links (ex: edit, attach, oops) </td>
</tr>
<tr>
<td> %<nop>TMPL:DEF{"standardfooter"}% </nop></td>
<td> Footer, excluding revision and copyright parts </td>
</tr>
<tr>
<td> %<nop>TMPL:DEF{"oops"}% </nop></td>
<td> Skeleton of oops dialog </td>
</tr>
</table>
> **Example: `preview.tmpl` template**
>
> <pre>
> %<nop>TMPL:INCLUDE{"twiki"}%
> %<nop>TMPL:DEF{"titleaction"}% (oops) %<nop>TMPL:END%
> %<nop>TMPL:DEF{"webaction"}% *Attention* %<nop>TMPL:END%
> %<nop>TMPL:DEF{"heading"}% Topic is not saved yet %<nop>TMPL:END%
> %<nop>TMPL:DEF{"message"}% Please go back in your browser and save the topic. %<nop>TMPL:END%
> %<nop>TMPL:DEF{"topicaction"}%
> %<nop>TMPL:END%
> %<nop>TMPL:P{"oops"}%
> </nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></pre>
## <a name="Known_Issues"> Known Issues </a>
* 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.
-- [[Main/PeterThoeny]] - 23 Jul 2001 <br /> -- [[MikeMannix]] - 30 Aug 2001
|