3 <li><a href="#TWiki Template System"> TWiki Template System</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>
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>
22 <li><a href="#Known Issues"> Known Issues</a></li>
28 # <a name="TWiki Template System"></a> TWiki Template System
30 _Definition of the templates used to render all HTML pages displayed in TWiki_
32 ## <a name="Overview"></a> Overview
34 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]].
36 ## <a name="Major changes from the previous"></a><a name="Major changes from the previous "></a> Major changes from the previous template system
38 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:
40 - separates a set of common template parts into a base template that is included by all of the related templates;
42 - defines common variables, like a standard separator (ex: "|"), in the base template;
44 - defines variable text in the individual templates and passes it back to the base template.
46 ## <a name="Functional Specifications"></a> Functional Specifications
48 - Special template directives (or preprocessor commands) are embedded in normal templates.
49 - Use of template directives is optional, templates work without them.
50 - All template preprocessing is done in `&TWiki::Store::readTemplate()` so that the caller simply gets an expanded template file (the same as before).
51 - Directives are of the form `%TMPL:<key>%` and `%TMPL:<key>{"attr"}%`.
53 - `%TMPL:INCLUDE{"file"}%`: Includes a template file. The template directory of the current web is searched first, then the templates root (`twiki/templates`).
54 - `%TMPL:DEF{"var"}%`: Define a variable. Text between this and the END directive is not returned, but put into a hash for later use.
55 - `%TMPL:END%`: Ends variable definition.
56 - `%TMPL:P{"var"}%`: Prints a previously defined variable.
57 - Variables are live in a global name space, there is no parameter passing.
58 - Two-pass processing, so that you can use a variable before declaring it or after.
59 - 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.
60 - **_Note:_** The template directives work only for templates, they do not get processed in topic text.
62 ## <a name="TWiki Master Template"></a> TWiki Master Template
64 All common parts are defined in a master template, <code>**twiki.tmpl**</code>, that all other templates use.
66 > <table border="1" cellpadding="0" cellspacing="0">
68 > <th bgcolor="#99CCCC"><strong> Template variable: </strong></th>
69 > <th bgcolor="#99CCCC"><strong> Defines: </strong></th>
72 > <td> %TMPL:DEF{"sep"}% </td>
73 > <td> "|" separator </td>
76 > <td> %TMPL:DEF{"htmldoctype"}% </td>
77 > <td> Start of all HTML pages </td>
80 > <td> %TMPL:DEF{"standardheader"}% </td>
81 > <td> Standard header (ex: view, index, seach) </td>
84 > <td> %TMPL:DEF{"simpleheader"}% </td>
85 > <td> Simple header with reduced links (ex: edit, attach, oops) </td>
88 > <td> %TMPL:DEF{"standardfooter"}% </td>
89 > <td> Footer, excluding revision and copyright parts </td>
92 > <td> %TMPL:DEF{"oops"}% </td>
93 > <td> Skeleton of oops dialog </td>
97 ## <a name="Types of Template"></a> Types of Template
99 There are two types of templates:
101 - **HTML Page Templates**: Defines layout of %WIKITOOLNAME% pages
102 - **Template Topics**: Defines default text when you create a new topic
104 ### <a name="HTML Page Templates"></a> HTML Page Templates
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.
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:
110 1. <code>**twiki/templates/$webName/$scriptName.tmpl**</code>
111 2. <code>**twiki/templates/$scriptName.tmpl**</code>
113 **_Note:_** <code>**$webName**</code> is the name of the web ( i.e. <code>**Main**</code> ), and <code>**$scriptName**</code> is the script ( i.e. <code>**view**</code> ).
115 **_Note:_** [[TWikiSkins]] can be defined to overload the standard templates.
117 Some special variables are used in templates ( especially <code>**view**</code> ) to show meta data - see [[Meta Data Rendering|Main/TWikiDocumentation#Meta_Data_Rendering]]
119 ### <a name="Template Topics"></a> Template Topics
121 Template topics define the default text for new topics. There are three types of template topics:
123 > <table border="1" cellpadding="0" cellspacing="0">
125 > <th bgcolor="#99CCCC"><strong> Topic Name: </strong></th>
126 > <th bgcolor="#99CCCC"><strong> What it is: </strong></th>
129 > <td>[[Main/WebTopicViewTemplate]]</td>
130 > <td> Help text shown when you view a non existing topic. </td>
133 > <td>[[Main/WebTopicNonWikiTemplate]]</td>
134 > <td> Help text shown when you view a non existing topic that has not a [[Main/WikiName]]. </td>
137 > <td>[[Main/WebTopicEditTemplate]]</td>
138 > <td> Default text shown when you create a new topic. </td>
142 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:
144 1. The topic name specified by the `templatetopic` parameter.
145 2. WebTopicEditTemplate in the current web.
146 3. WebTopicEditTemplate in the TWiki web.
148 #### <a name="Template Topics in Action"></a> Template Topics in Action
150 Here is an example for creating new topics based on a specific template topic:
152 <form action="http://www.dementia.org/twiki//edit/%WEB%/" name="new">
154 <li> New example topic: <input name="topic" size="22" type="text" value="ExampleTopic20100629" /> <input name="templatetopic" type="hidden" value="ExampleTopicTemplate" /> <input type="submit" value="Create" /> (date format is YYYYMMDD) </li>
158 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:
160 <form name="new" action="%SCRIPTURLPATH%/edit%SCRIPTSUFFIX%/%WEB%/">
162 <input type="text" name="topic" value="ExampleTopic%SERVERTIME{$year$mo$day}%" size="22">
163 <input type="hidden" name="templatetopic" value="ExampleTopicTemplate">
164 <input type="hidden" name="onlywikiname" value="on">
165 <input type="submit" value="Create"> (date format is YYYYMMDD)
168 The "onlywikiname" parameter enforces [[WikiWords]] for topic names.
170 **_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%`
172 ## <a name="Templates by Example"></a> Templates by Example
174 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.
176 ### <a name="Base template oopsbase.tmpl"></a> Base template oopsbase.tmpl
178 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"}%`
180 > <table bgcolor="#f5f5f5" border="1" cellpadding="1" cellspacing="0">
183 > %TMPL:DEF{"sep"}% | %TMPL:END%
186 > <title> %WIKITOOLNAME% . %WEB% . %TOPIC% %.TMPL:P{"titleaction"}%</title>
187 > <base href="%SCRIPTURL%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%">
188 > <meta name="robots" content="noindex">
190 > <body bgcolor="#FFFFFF">
191 > <table width="100%" border="0" cellpadding="3" cellspacing="0">
193 > <td bgcolor="%WEBBGCOLOR%" rowspan="2" valign="top" width="1%">
194 > <a href="%WIKIHOMEURL%">
195 > <img src="%PUBURLPATH%/wikiHome.gif" border="0"></a>
198 > <b>%WIKITOOLNAME% . %WEB% . </b><font size="+2">
199 > <B>%TOPIC%</b> %TMPL:P{"titleaction"}%</font>
202 > <tr bgcolor="%WEBBGCOLOR%">
203 > <td colspan="2">
204 > %TMPL:P{"webaction"}%
208 > --- ++ %TMPL:P{"heading"}%
209 > %TMPL:P{"message"}%
210 > <table width="100%" border="0" cellpadding="3" cellspacing="0">
211 > <tr bgcolor="%WEBBGCOLOR%">
212 > <td valign="top">
213 > Topic <b>%TOPIC%</b> . {
214 > %TMPL:P{"topicaction"}%
224 ### <a name="Test template oopstest.tmpl"></a> Test template oopstest.tmpl
226 Each oops template basically just defines some variables and includes the base template that does the layout work.
228 > <table bgcolor="#f5f5f5" border="1" cellpadding="1" cellspacing="0">
231 > %TMPL:DEF{"titleaction"}% (test =titleaction=) %TMPL:END%
232 > %TMPL:DEF{"webaction"}% test =webaction= %TMPL:END%
233 > %TMPL:DEF{"heading"}%
234 > Test heading %TMPL:END%
235 > %TMPL:DEF{"message"}%
236 > Test =message=. Blah blah blah blah blah blah blah blah blah blah blah...
238 > * Some more blah blah blah blah blah blah blah blah blah blah...
244 > %TMPL:DEF{"topicaction"}%
245 > Test =topicaction=:
246 > [[%WEB%.%TOPIC%][OK]] %TMPL:P{"sep"}%
247 > [[%TWIKIWEB%.TWikiRegistration][Register]] %TMPL:END%
248 > %TMPL:INCLUDE{"oopsbase"}%
252 > <td> </table > </td>
256 ### <a name="Sample screen shot of oopstest.t"></a> Sample screen shot of oopstest.tmpl
258 With URL: <code>**.../bin/oops/Test/TestTopic2?template=oopstest&param1=WebHome&param2=WebNotify**</code>
260 > <table border="1" cellpadding="0" cellspacing="0">
262 > <td><img alt="testscreen.gif" height="304" src="http://www.dementia.org/twiki//view/testscreen.gif" width="554" /></td>
266 ## <a name="Known Issues"></a> Known Issues
268 - 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.
270 -- [[PeterThoeny]] - 23 Jul 2001 <br /> -- [[MikeMannix]] - 14 Sep 2001 <br />