summaryrefslogtreecommitdiff
path: root/TWiki/TWikiTemplates.mdwn
blob: 37c648f9c317439e3b4bae3c8842d7885fbfc498 (plain)
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
%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 [[TWikiSkins]].

## <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:&lt;key&gt;%</code> and <code>%TMPL:&lt;key&gt;\{"attr"\}%</code>.
* Directives:
  * <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`).
  * <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.
* 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 [[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.
* **_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>

> <table border="1" cellpadding="1" cellspacing="0">
>   <tr>
>     <td><pre>
> %<nop>TMPL:DEF{"sep"}% | %<nop>TMPL:END%
> &amp;lt;html&amp;gt;
> &amp;lt;head&amp;gt;
>   &amp;lt;title&amp;gt; %<nop>WIKITOOLNAME% . %<nop>WEB% . %<nop>TOPIC% %.TMPL:P{"titleaction"}%&amp;lt;/title&amp;gt;
>   &amp;lt;base href<nop>="%<nop>SCRIPTURL%/view%<nop>SCRIPTSUFFIX%/%<nop>WEB%/%<nop>TOPIC%"&amp;gt;
>   &amp;lt;meta name="robots" content="noindex"&amp;gt;
> &amp;lt;/head&amp;gt;
> &amp;lt;body bgcolor="#FFFFFF"&amp;gt;
> &amp;lt;table width="100%" border="0" cellpadding="3" cellspacing="0"&amp;gt;
>   &amp;lt;tr&amp;gt;
>     &amp;lt;td bgcolor="%<nop>WEBBGCOLOR%" rowspan="2" valign="top" width="1%"&amp;gt;
>       &amp;lt;a href<nop>="%<nop>WIKIHOMEURL%"&amp;gt;
>       &amp;lt;img src="%<nop>PUBURLPATH%/wikiHome.gif" border="0"&amp;gt;&amp;lt;/a&amp;gt;
>     &amp;lt;/td&amp;gt;
>     &amp;lt;td&amp;gt;
>       &amp;lt;b&amp;gt;%<nop>WIKITOOLNAME% . %<nop>WEB% . &amp;lt;/b&amp;gt;&amp;lt;font size="+2"&amp;gt;
>       &amp;lt;B&amp;gt;%<nop>TOPIC%&amp;lt;/b&amp;gt; %<nop>TMPL:P{"titleaction"}%&amp;lt;/font&amp;gt;
>     &amp;lt;/td&amp;gt;
>   &amp;lt;/tr&amp;gt;
>   &amp;lt;tr bgcolor="%<nop>WEBBGCOLOR%"&amp;gt;
>     &amp;lt;td colspan="2"&amp;gt;
>       %<nop>TMPL:P{"webaction"}%
>     &amp;lt;/td&amp;gt;
>   &amp;lt;/tr&amp;gt;
> &amp;lt;/table&amp;gt;
> --- ++ %<nop>TMPL:P{"heading"}%
> %<nop>TMPL:P{"message"}%
> &amp;lt;table width="100%" border="0" cellpadding="3" cellspacing="0"&amp;gt;
>   &amp;lt;tr bgcolor="%<nop>WEBBGCOLOR%"&amp;gt;
>     &amp;lt;td valign="top"&amp;gt;
>       Topic &amp;lt;b&amp;gt;%<nop>TOPIC%&amp;lt;/b&amp;gt; . {
>         %<nop>TMPL:P{"topicaction"}%
>       }
>     &amp;lt;/td&amp;gt;
>   &amp;lt;/tr&amp;gt;
> &amp;lt;/table&amp;gt;
> &amp;lt;/body&amp;gt;
> </nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></pre></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>
> %<nop>TMPL:DEF{"titleaction"}% (test =titleaction=) %<nop>TMPL:END%
> %<nop>TMPL:DEF{"webaction"}% test =webaction= %<nop>TMPL:END%
> %<nop>TMPL:DEF{"heading"}%
> Test heading %<nop>TMPL:END%
> %<nop>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: %<nop>PARAM1%
>    * Param2: %<nop>PARAM2%
>    * Param3: %<nop>PARAM3%
>    * Param4: %<nop>PARAM4%
> %<nop>TMPL:END%
> %<nop>TMPL:DEF{"topicaction"}%
> Test =topicaction=:
> [<nop>[%<nop>WEB%.%<nop>TOPIC%][OK]] %<nop>TMPL:P{"sep"}%
> [<nop>[%<nop>TWIKIWEB%.TWikiRegistration][Register]] %<nop>TMPL:END%
> %<nop>TMPL:INCLUDE{"oopsbase"}%
> </nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></nop></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&amp;param1=WebHome&amp;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>

## <a name="TWiki_master_template"> TWiki master template </a>

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: `oopspreview.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.

-- [[PeterThoeny]] - 23 Jul 2001 <br /> -- [[MikeMannix]] - 30 Aug 2001