Skin Templates
Definition of the templates used to render output
Overview
Skin Templates are plain text with embedded
template macros that describe how to compose blocks of text together, to create something new.
Skin templates are used composing the output from all actions, like view, edit, and preview. This allows you to change the look and feel of all pages by editing just a few template files.
Skin templates are usually stored as text files with the extension
.tmpl
, though can also come from topic text in some limited circumstances. They are usually HTML with embedded
template macros. The macros are expanded when we want to generate output, such as a user interface screen.
How Template Directives Work
- Directives are of the form
%TMPL:<key>%
and %TMPL:<key>{"attr"}%
.
- Directives:
-
%TMPL:INCLUDE{"file"}%
: Includes a template file. The file is found as described below.
-
%TMPL:DEF{"block"}%
: Define a block. All text between this and the next %TMPL:END%
directive is removed and saved for later use with %TMPL:P%
.
-
%TMPL:END%
: Ends a block definition.
-
%TMPL:P{"var"}%
: Includes a previously defined block.
-
%{...}%
: is a comment.
- Two-pass processing lets you use a defined block before or after declaring it.
- For example, you can create a skin that overloads only the
foswiki.tmpl
master skin template, like foswiki.print.tmpl
, that redefines the header and footer.
- Use of template macros is optional: templates work without them.
- Most template macros work only for templates: they do not get processed in normal topic text. The one exception is
%TMPL:P
.
TMPL:P also supports simple parameters. For example, given the definition
%TMPL:DEF{"x"}% x%P%z%TMPL:END%
then
%TMPL:P{"x" P="y"}%
will expand to
xyz
.
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
macros.
Note that three parameter names,
context
,
then
and
else
are
reserved.
They are used to support a limited form of "if" condition that you can use to select which of two TMPL:DEFs 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" TMPL:DEF; otherwise it will expand the "link_active" TMPL:DEF.
See
IfStatements for details of supported context identifiers.
Finding Skin Templates
The skin templates shipped with a release are stored in the
templates
directory.
As an example,
templates/view.tmpl
is the default skin template file for the
bin/view
script.
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 skin templates in user topics (
IF there is no possible template match in the
templates
directory).
The
{TemplatePath}
configuration setting defines which topics will be accepted as templates.
Skin templates that are included with an explicit
'.tmpl'
extension are looked for only in the
templates/
directory.
For instance
%TMPL:INCLUDE{"example.tmpl"}%
will only return
templates/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
Skins.
- templates/web/script.skin.tmpl for each skin on the skin path
- this usage is supported for compatibility only and is deprecated. Store web-specific templates in topics instead.
- templates/script.skin.tmpl for each skin on the skin path
- for example templates/view.dragon.tmpl
- templates/web/script.tmpl
- this usage is supported for compatibility only and is deprecated. Store web-specific templates in topics instead.
- templates/script.tmpl
- for example templates/view.tmpl
- The topic aweb.atopic if the template name can be parsed into aweb.atopic
- The topic web.SkinSkinScriptTemplate for each skin on the skin path
- for example DragonSkinViewTemplate in the current Web
- The topic web.ScriptTemplate
- The topic %SYSTEMWEB%.SkinSkinScriptTemplate for each skin on the skin path
- for example System.DragonSkinViewTemplate
- The topic %SYSTEMWEB%.ScriptTemplate
Legend:
- script refers to the script name, e.g
view
, edit
- Script refers to the same, but with the first character capitalized, e.g
View
- skin refers to a skin name, e.g
dragon
, pattern
. All skins are checked at each stage, in the order they appear in the skin path.
- Skin refers to the same, but with the first character capitalized, e.g
Dragon
- web refers to the current web
For example, the
example
template file will be searched for in the following places, when the current web is
Thisweb
and the skin path is
print,pattern
:
-
templates/Thisweb/example.print.tmpl
deprecated; don't rely on it
-
templates/Thisweb/example.pattern.tmpl
deprecated; don't rely on it
-
templates/example.print.tmpl
-
templates/example.pattern.tmpl
-
templates/Thisweb/example.tmpl
deprecated; don't rely on it
-
templates/example.tmpl
-
Thisweb.PrintSkinExampleTemplate
-
Thisweb.PatternSkinExampleTemplate
-
Thisweb.ExampleTemplate
-
System.PrintSkinExampleTemplate
-
System.PatternSkinExampleTemplate
-
System.ExampleTemplate
Template names are usually derived from the name of the currently executing script; however it is also possible to override these settings in the
view
and
edit
scripts, for example when a topic-specific template is required. Two
preference settings can be used to override the skin templates used:
-
VIEW_TEMPLATE
sets the template to be used for viewing a topic.
-
EDIT_TEMPLATE
sets the template for editing a topic.
If these preferences are set locally (using
Local instead of
Set) for a topic, in
WebPreferences, in
Main.SitePreferences, or
System.DefaultPreferences (using
Set), the indicated templates will be chosen for
view
and
edit
respectively. The template search order is as specified above.
TMPL:INCLUDE recursion for piecewise customisation, or mixing in new features
If there is recursion in the TMPL:INCLUDE chain (eg view.tmpl contains
%TMPL:INCLUDE{"foswiki"}%
, the templating system will include the next 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 topic
Thisweb.YourlocalSkinViewTemplate
. The default
{TemplatePath}
will resolve the request to the
template/view.pattern.tmpl
, before it gets to the
Thisweb.YourlocalSkinViewTemplate
resolution. You can make it work by prefixing the
{TemplatePath}
with:
$web.YourlocalSkin$nameTemplate
.
Default master template
foswiki.tmpl
is the default master template. It defines the following sections.
Template directive: |
Defines: |
%TMPL:DEF{"sep"}% |
"|" separator |
%TMPL:DEF{"htmldoctype"}% |
Start of all HTML pages |
%TMPL:DEF{"standardheader"}% |
Standard header (ex: view, index, search) |
%TMPL:DEF{"simpleheader"}% |
Simple header with reduced links (ex: edit, attach, oops) |
%TMPL:DEF{"standardfooter"}% |
Footer, excluding revision and copyright parts |
Related Topics: Skins