1 # <a name="Package <code>TWiki::Templates="></a> Package =TWiki::Templates
3 Support for the TWiki template language.
7 <li><a href="#Package =TWiki::Templates="> Package TWiki::Templates</a><ul>
8 <li><a href="#ClassMethod <strong>new</strong> ($session)"> ClassMethod new <tt>($session)</tt></a></li>
9 <li><a href="#ObjectMethod <strong>haveTemplate</strong> ($na"> ObjectMethod haveTemplate <tt>($name) -> $boolean</tt></a></li>
10 <li><a href="#ObjectMethod <strong>expandTemplate</strong> ($"> ObjectMethod expandTemplate <tt>($params) -> $string</tt></a></li>
16 The following tokens are supported by this language:
18 <table border="1" cellpadding="0" cellspacing="0">
21 <td> Instantiates a previously defined template </td>
25 <td> Opens a template definition </td>
29 <td> Closes a template definition </td>
32 <td> %TMPL:INCLUDE% </td>
33 <td> Includes another file of templates </td>
37 Note; the template cache does not get reset during initialisation, so the haveTemplate test will return true if a template was loaded during a previous run when used with mod\_perl or speedycgi. Frustrating for the template author, but they just have to switch off the accelerators during development.
39 This is to all intents and purposes a singleton object. It could easily be coverted into a true singleton (template manager).
41 ## <a name="ClassMethod <strong>new</strong> ($session)"></a> [[ClassMethod]] **new** `($session)`
43 Constructor. Creates a new template database object.
45 - $session - session (TWiki) object
47 ## <a name="ObjectMethod <strong>haveTemplate</strong> ($na"></a> [[ObjectMethod]] **haveTemplate** `($name) -> $boolean`
49 Return true if the template exists and is loaded into the cache
51 ## <a name="ObjectMethod <strong>expandTemplate</strong> ($"></a> [[ObjectMethod]] **expandTemplate** `($params) -> $string`
53 Expand the template specified in the parameter string using `tmplP`.
57 $tmpls->expandTemplate('"blah");
58 $tmpls->expandTemplate('context="view" then="sigh" else="humph"');
60 ---++ ObjectMethod *tmplP* <tt>($attrs) -> $string</tt>
62 Return value expanded text of the template, as found from looking
63 in the register of template definitions. The attrs can contain a template
64 name in _DEFAULT, and / or =context=, =then= and =else= values.
66 Recursively expands any contained TMPL:P tags.
68 Note that it would be trivial to add template parameters to this,
69 simply by iterating over the other parameters (other than _DEFAULT, context,
70 then and else) and doing a s/// in the template for that parameter value. This
71 would add considerably to the power of templates. There is already code
72 to do this in the MacrosPlugin.
74 ---++ ObjectMethod *readTemplate* <tt>($name,$skins,$web) -> $text</tt>
76 Return value: expanded template text
78 Reads a template, constructing a candidate name for the template thus
79 0 looks for file =$name.$skin.tmpl= (for each skin)
81 0 in =templates=, look for
82 0 looks for file =$name.tmpl=
84 0 in =templates=, look for
85 0 if a template is not found, tries in this order
86 0 parse =$name= into a web name (default to $web) and a topic name and looks for this topic
87 0 looks for topic =${skin}Skin${name}Template=
88 0 in $web (for each skin)
89 0 in =TWiki::cfg{SystemWebName}= (for each skin)
90 0 looks for topic =${name}Template=
91 0 in $web (for each skin)
92 0 in =TWiki::cfg{SystemWebName}= (for each skin)
93 In the event that the read fails (template not found, access permissions fail)
94 returns the empty string ''.
96 =$skin=, =$web= and =$name= are forced to an upper-case first character
97 when composing user topic names.
99 If template text is found, extracts include statements and fully expands them.
100 Also extracts template definitions and adds them to the
101 list of loaded templates, overwriting any previous definition.