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="#How Template Variables Work"> How Template Variables Work</a></li>
7 <li><a href="#Types of Template"> Types of Template</a><ul>
8 <li><a href="#Master Templates"> Master Templates</a></li>
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 Templates"></a> TWiki Templates
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 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 markup 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.
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 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:
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.
44 ## <a name="How Template Variables Work"></a> How Template Variables Work
46 - Special template directives (or preprocessor commands) are embedded in normal templates.
47 - All template preprocessing is done in `&TWiki::Store::readTemplate()` so that the caller simply gets an expanded template file (the same as before).
48 - Directives are of the form <code>**%TMPL:<key>%**</code> and <code>**%TMPL:<key>\{"attr"\}%**</code>.
50 - <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`).
51 - <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.
52 - <code>**%TMPL:END%**</code>: Ends variable definition.
53 - <code>**%TMPL:P\{"var"\}%**</code>: Prints a previously defined variable.
54 - Variables live in a global name space: there is no parameter passing.
55 - Two-pass processing lets you use a variable before or after declaring it.
56 - Templates and [[TWikiSkins]] work transparently and interchangeably. For example, you can create a skin that overloads only the `twiki.tmpl` master template, like `twiki.print.tmpl`, that redefines the header and footer.
57 - %H% Use of template directives is optional: templates work without them.
58 - %X% **NOTE:** Template directives work only for templates: they do not get processed in topic text.
60 ## <a name="Types of Template"></a> Types of Template
62 There are three types of template:
64 - **Master Template**: Stores common parts; included by other templates
65 - **HTML Page Templates**: Defines the layout of %WIKITOOLNAME% pages
66 - **Template Topics**: Defines default text when you create a new topic
68 ### <a name="Master Templates"></a> Master Templates
70 Common parts, appearing in two or more templates, can be defined in a master template and then shared by others: <code>**twiki.tmpl**</code> is the default master template.
72 > <table border="1" cellpadding="0" cellspacing="0">
74 > <th bgcolor="#99CCCC"><strong> Template variable: </strong></th>
75 > <th bgcolor="#99CCCC"><strong> Defines: </strong></th>
78 > <td> %TMPL:DEF{"sep"}% </td>
79 > <td> "|" separator </td>
82 > <td> %TMPL:DEF{"htmldoctype"}% </td>
83 > <td> Start of all HTML pages </td>
86 > <td> %TMPL:DEF{"standardheader"}% </td>
87 > <td> Standard header (ex: view, index, search) </td>
90 > <td> %TMPL:DEF{"simpleheader"}% </td>
91 > <td> Simple header with reduced links (ex: edit, attach, oops) </td>
94 > <td> %TMPL:DEF{"standardfooter"}% </td>
95 > <td> Footer, excluding revision and copyright parts </td>
98 > <td> %TMPL:DEF{"oops"}% </td>
99 > <td> Skeleton of oops dialog </td>
103 <a name="HtmlTemplates"></a>
105 ### <a name="HTML Page Templates"></a> HTML Page Templates
107 %WIKITOOLNAME% uses HTML template files for all actions, like topic view, edit, and preview. This allows you to change the look and feel of all pages by editing just a few template files.
109 Templates 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 by individual webs. The following search order applies:
111 1. <code>**twiki/templates/$webName/$scriptName.tmpl**</code>
112 2. <code>**twiki/templates/$scriptName.tmpl**</code>
113 - `$webName` is the name of the web (ex: `Main`)
114 - `$scriptName` is the script (ex: `view`).
116 %H% **NOTE:** [[TWikiSkins]] can be defined to overload the standard templates.
118 Special variables are used in templates, especially in `view`, to display [[meta data|Main/TWikiMetaData#MetaDataRendering]].
120 <a name="TemplateTopics"></a>
122 ### <a name="Template Topics"></a> Template Topics
124 Template topics define the default text for new topics. There are three types of template topic:
126 > <table border="1" cellpadding="0" cellspacing="0">
128 > <th bgcolor="#99CCCC"><strong> Topic Name: </strong></th>
129 > <th bgcolor="#99CCCC"><strong> What it is: </strong></th>
132 > <td>[[Main/WebTopicViewTemplate]]</td>
133 > <td> Error page shown when you try to view a nonexistent topic </td>
136 > <td>[[Main/WebTopicNonWikiTemplate]]</td>
137 > <td> Alert page shown when you try to view a nonexistent topic with a non-WikiName </td>
140 > <td>[[Main/WebTopicEditTemplate]]</td>
141 > <td> Default text shown when you create a new topic. </td>
145 All template topics are located in the TWiki web. The [[WebTopicEditTemplate]] can be overloaded. When you create a new topic, TWiki locates a topic to use as a content template according to the following search order:
147 1. A topic name specified by the `templatetopic` CGI parameter.
148 2. WebTopicEditTemplate in the current web
149 3. WebTopicEditTemplate in the TWiki web
151 #### <a name="Template Topics in Action"></a> Template Topics in Action
153 Here is an example for creating new topics based on a specific template topic:
155 <form action="http://www.dementia.org/twiki//edit/%WEB%/" name="new">
157 <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>
161 The above form asks for a topic name. A hidden input tag named <code>**templatetopic**</code> specifies [[ExampleTopicTemplate]] as the template topic to use. Here is the HTML source of the form:
163 <form name="new" action="%SCRIPTURLPATH%/edit%SCRIPTSUFFIX%/%WEB%/">
165 <input type="text" name="topic" value="ExampleTopic%SERVERTIME{$yearx$mox$day}%" size="23" />
166 <input type="hidden" name="templatetopic" value="ExampleTopicTemplate" />
167 <input type="hidden" name="onlywikiname" value="on" />
168 <input type="submit" value="Create" />
169 (date format is <nop>YYYYxMMxDD)
172 The <code>**onlywikiname**</code> parameter enforces [[WikiWords]] for topic names.
174 %T% **TIP:** You can use the `%WIKIUSERNAME%` and `%DATE%` variables in your topic templates to include the signature of the person creating a new topic. The variables are expanded into fixed text when a new topic is created. The standard signature is: <br /><code>**-- %WIKIUSERNAME% - %DATE%**</code>
176 ## <a name="Templates by Example"></a> Templates by Example
178 Attached is an example of an oops based template `oopsbase.tmpl` and an example oops dialog `oopstest.tmpl` based on the base template. %A% **NOTE:** This isn't the release version, just a quick, simple demo.
180 ### <a name="Base template oopsbase.tmpl"></a> Base template oopsbase.tmpl
182 The first line declares a delimiter variable called "sep", used to separate multiple link items. The variable can be called anywhere by writing `%TMPL:P{"sep"}%`
184 > <table bgcolor="#f5f5f5" border="1" cellpadding="1" cellspacing="0">
187 > %TMPL:DEF{"sep"}% | %TMPL:END%
190 > <title> %WIKITOOLNAME% . %WEB% . %TOPIC% %.TMPL:P{"titleaction"}%</title>
191 > <base href="%SCRIPTURL%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%">
192 > <meta name="robots" content="noindex">
194 > <body bgcolor="#FFFFFF">
195 > <table width="100%" border="0" cellpadding="3" cellspacing="0">
197 > <td bgcolor="%WEBBGCOLOR%" rowspan="2" valign="top" width="1%">
198 > <a href="%WIKIHOMEURL%">
199 > <img src="%PUBURLPATH%/wikiHome.gif" border="0"></a>
202 > <b>%WIKITOOLNAME% . %WEB% . </b><font size="+2">
203 > <B>%TOPIC%</b> %TMPL:P{"titleaction"}%</font>
206 > <tr bgcolor="%WEBBGCOLOR%">
207 > <td colspan="2">
208 > %TMPL:P{"webaction"}%
212 > --- ++ %TMPL:P{"heading"}%
213 > %TMPL:P{"message"}%
214 > <table width="100%" border="0" cellpadding="3" cellspacing="0">
215 > <tr bgcolor="%WEBBGCOLOR%">
216 > <td valign="top">
217 > Topic <b>%TOPIC%</b> . {
218 > %TMPL:P{"topicaction"}%
228 ### <a name="Test template oopstest.tmpl"></a> Test template oopstest.tmpl
230 Each oops template basically just defines some variables and includes the base template that does the layout work.
232 > <table bgcolor="#f5f5f5" border="1" cellpadding="1" cellspacing="0">
235 > %TMPL:DEF{"titleaction"}% (test =titleaction=) %TMPL:END%
236 > %TMPL:DEF{"webaction"}% test =webaction= %TMPL:END%
237 > %TMPL:DEF{"heading"}%
238 > Test heading %TMPL:END%
239 > %TMPL:DEF{"message"}%
240 > Test =message=. Blah blah blah blah blah blah blah blah blah blah blah...
242 > * Some more blah blah blah blah blah blah blah blah blah blah...
248 > %TMPL:DEF{"topicaction"}%
249 > Test =topicaction=:
250 > [[%WEB%.%TOPIC%][OK]] %TMPL:P{"sep"}%
251 > [[%TWIKIWEB%.TWikiRegistration][Register]] %TMPL:END%
252 > %TMPL:INCLUDE{"oopsbase"}%
256 > <td> </table > </td>
260 ### <a name="Sample screen shot of oopstest.t"></a> Sample screen shot of oopstest.tmpl
262 With URL: <code>**.../bin/oops/Sandbox/TestTopic2?template=oopstest&param1=WebHome&param2=WebNotify**</code>
264 > <table border="1" cellpadding="0" cellspacing="0">
266 > <td><img alt="testscreen.gif" height="304" src="http://www.dementia.org/twiki//view/testscreen.gif" width="589" /></td>
270 ## <a name="Known Issues"></a> Known Issues
272 - 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 was a structurally complete HTML document with a `.tmpl` filename extension - it contained unresolved `%VARIABLES%`, but could still be previewed directly in a browser.
274 -- [[PeterThoeny]] - 23 Jul 2001 <br /> -- [[MikeMannix]] - 14 Sep 2001 <br /> -- TWiki:Main/DavidLeBlanc - 11 Mar 2002