
| TWiki TemplatesDefinition of the templates used to render all HTML pages displayed in TWikiOn this page:  
 OverviewTemplates are plain text with embedded template directives that tell TWiki how to compose blocks of text together, to create something new. There are two types of template:
  Tip: TWiki:TWiki.TWikiTemplatesSupplement  on TWiki.org has supplemental documentation on TWiki templates. Master TemplatesTWiki uses master templates when composing the output from 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. Master templates are also used in the definition of TWikiSkins. Master templates are stored as text files with the extension.tmpl.
They are usually HTML with embedded template directives.
The directives are expanded when TWiki wants to generate a user interface screen.How Template Directives Work
 %TMPL:DEF{"x"}% x%P%z%TMPL:END%then%TMPL:P{"x" P="y"}%will expand toxyz.
Note that parameters can simply be ignored; for example,%TMPL:P{"x"}%will expand to x%P%z.
Any alphanumeric characters can be used in parameter names.
You are highly recommended to use parameter names that cannot be confused with TWikiVariables.
Note that three parameter names,context,thenandelseare reserved.
They are used to support a limited form of "if" condition that you can use to select which of two templates to use, based on a context identifier:
%TMPL:DEF{"link_inactive"}%<input type="button" disabled value="Link>%TMPL:END%
%TMPL:DEF{"link_active"}%<input type="button" onclick="link()" value="Link" />%TMPL:END%
%TMPL:P{context="inactive" then="inactive_link" else="active_link"}% for %CONTEXT%
When the "inactive" context is set, then this will expand the "link_inactive" template; otherwise it will expand the "link_active" template.
See IfStatements for details of supported context identifiers.Finding TemplatesThe master templates shipped with a twiki release are stored in the twiki/templates directory. As an example,twiki/templates/view.tmplis the default template file for thetwiki/bin/viewscript. 
You can save templates in other directories as long as they are listed in the{TemplatePath}configuration setting.
The{TemplatePath}is defined in the Miscellaneous section of the configure page.
You can also save templates in user topics (IF there is no possible template match in thetemplatesdirectory).
The{TemplatePath}configuration setting defines which topics will be accepted as templates.
Templates that are included with an explicit'.tmpl'extension are looked for only in thetemplates/directory.
For instance%TMPL:INCLUDE{"example.tmpl"}%will only returntemplates/example.tmpl, regardless of{TemplatePath}and SKIN settings.
The out-of-the-box setting of{TemplatePath}supports the following search order to determine which template file or topic to use for a particular script or%TMPL:INCLUDE{"script"}%statement.
The skin path is set as described in TWikiSkins.
 
 exampletemplate file will be searched for in the following places, when the current web isThisweband the skin path isprint,pattern:
 viewandeditscripts, for example when a topic-specific template is required. Two preference variables can be used to override the templates used:
 viewandeditrespectively. The template search order is as specified above.TMPL:INCLUDE recursion for piecewise customisation, or mixing in new featuresIf there is recursion in the TMPL:INCLUDE chain (eg twiki.classic.tmpl contains%TMPL:INCLUDE{"twiki"}%, the templating system will include the next twiki.SKIN in the skin path. 
For example, to create a customisation of pattern skin, where you only want to over-ride the breadcrumbs for the view script, you can create only a view.yourlocal.tmpl:
%TMPL:INCLUDE{"view"}%
%TMPL:DEF{"breadcrumb"}% We don't want any crumbs %TMPL:END%
and then set SKIN=yourlocal,pattern
The default{TemplatePath}will not give you the desired result if you put these statements in the topicThisweb.YourlocalSkinViewTemplate. The default{TemplatePath}will resolve the request to thetemplate/view.pattern.tmpl, before it gets to theThisweb.YourlocalSkinViewTemplateresolution. You can make it work by prefixing the{TemplatePath}with:$web.YourlocalSkin$nameTemplate.Default master templatetwiki.tmplis the default master template. It defines the following sections.
 Template TopicsThe second type of template in TWiki are template topics. Template topics define the default text for new topics. There are four types of template topic:
 editscript, TWiki locates a topic to use as a content template according to the following search order:
 Variable ExpansionTWikiVariables located in template topics get expanded as follows when a new topic is created.1. Default variable expansionThe following variables used in a template topic automatically get expanded when new topic is created based on it:
 2. Preventing variable expansionIn a template topic, embed text that you do not want expanded inside a%STARTSECTION{type="templateonly"}% ... %ENDSECTION{type="templateonly"}%section. For example, you might want to write this in the template topic:
%STARTSECTION{type="templateonly"}%
This template can only be changed by:
   * Set ALLOWTOPICCHANGE = Main.TWikiAdminGroup
%ENDSECTION{type="templateonly"}%
This will restrict who can edit the template topic, but will be removed when a new topic based on that template topic is created.%NOP%can be used to prevent expansion of TWiki variables that would otherwise be expanded during topic creation. For example, escape%SERVERTIME%with%SER%NOP%VERTIME%.3. Control over variable expansionYou can forcefully expand TWikiVariables by placing them inside atype="expandvariables"section in the template topic, such as:
%STARTSECTION{ type="expandvariables" }% ... %ENDSECTION{ type="expandvariables" }% 
Example:
If you have the following content in a template topic:
%STARTSECTION{ type="expandvariables" }% | |||||||||||||||||||||||||||||||||||||||||||||||
| Changed: | |||||||||||||||||||||||||||||||||||||||||||||||
| < < | 
 | ||||||||||||||||||||||||||||||||||||||||||||||
| > > | 
 | ||||||||||||||||||||||||||||||||||||||||||||||
| 
 | |||||||||||||||||||||||||||||||||||||||||||||||
| Changed: | |||||||||||||||||||||||||||||||||||||||||||||||
| < < | 
 | ||||||||||||||||||||||||||||||||||||||||||||||
| > > | 
 | ||||||||||||||||||||||||||||||||||||||||||||||
| 
 Specifying a FormWhen you create a new topic based on a template, you often want the new topic to have a form attached to it. You can attach a form to the template topic, in which case it will be copied into the new topic. Sometimes this isn't quite what you want, as it copies all the existing data from the template topic into the new topic. To avoid this and use the default values specified in the form definition instead, you can use theformtemplateCGI parameter to theeditscript to specify the name of a form to attach.
See TWikiScripts for information about all the other parameters toedit.Automatically Generated Topic NamesFor TWiki applications it is useful to be able to automatically generate unique topicnames, such as BugID0001, BugID0002, etc. You can addAUTOINC<n>to the topic name in the edit and save scripts, and it will be replaced with an auto-incremented number on topic save.<n>is a number starting from 0, and may include leading zeros. Leading zeros are used to zero-pad numbers so that auto-incremented topic names can sort properly. Deleted topics are not re-used to ensure uniqueness of topic names. That is, the auto-incremented number is always higher than the existing ones, even if there are gaps in the number sequence.
Examples:
 
[[%SCRIPTURLPATH{edit}%/%WEB%/BugIDAUTOINC00001?templatetopic=BugTemplate;topicparent=%TOPIC%;t=%SERVERTIME{"$day$hour$min$sec"}%][Create new item]]
Template Topics in ActionHere is an example for creating new topics (in the Sandbox web) based on a specific template topic and form: The above form asks for a topic name. A hidden input tag namedtemplatetopicspecifies ExampleTopicTemplate as the template topic to use. Here is the HTML source of the form:
<form name="new" action="%SCRIPTURLPATH{edit}%/Sandbox/">
   * New example topic: 
     <input type="text" name="topic" value="ExampleTopicAUTOINC0001" size="30" />
     <input type="hidden" name="templatetopic" value="ExampleTopicTemplate" />
     <input type="hidden" name="topicparent" value="%TOPIC%" />
     <input type="hidden" name="onlywikiname" value="on" />
     <input type="hidden" name="onlynewtopic" value="on" />
     <input type="submit" class="twikiSubmit" value="Create" />
</form>
 Note: You can create a topic in one step, without going through the edit screen. To do that, specify the savescript instead of theeditscript in the form action. When you specify the save script you have to use the "post" method. Example:
<form name="new" action="%SCRIPTURLPATH{save}%/Sandbox/" method="post">
    ...
</form>
 The editandsavescripts understand many more parameters, see TWikiScripts#edit and TWikiScripts#save for details. 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:-- %WIKIUSERNAME% - %DATE%Using Absolute vs Relative URLs in TemplatesWhen you use TWikiVariables such as %PUBURL% and %PUBURLPATH% in templates you should be aware that using %PUBURL% instead of %PUBURLPATH% puts absolute URLs in the produced HTML. This means that when a user saves a TWiki page in HTML and emails the file to someone outside a company firewall, the receiver has a severe problem viewing it. It is therefore recommended always to use the %PUBURLPATH% to refer to images, CSS, Javascript files etc so links become relative. This way browsers just give up right away and show a usable html file. Related Topics: TWikiSkins, TWikiForms, TWikiScripts, DeveloperDocumentationCategory, AdminDocumentationCategory -- Contributors: TWiki:Main.PeterThoeny , TWiki:Main.MikeMannix  , TWiki:Main.DavidLeBlanc  , TWiki:Main.CrawfordCurrie  , TWiki:Main.SopanShewale  | |||||||||||||||||||||||||||||||||||||||||||||||
 
  Copyright © 1999-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Copyright © 1999-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.