none
[openafs-wiki.git] / TWiki / TWikiTemplatesDotPm.mdwn
1 # <a name="Package &lt;code&gt;TWiki::Templates="></a> Package =TWiki::Templates
2
3 Support for the TWiki template language.
4
5 <div>
6   <ul>
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) -&gt; $boolean</tt></a></li>
10         <li><a href="#ObjectMethod <strong>expandTemplate</strong> ($"> ObjectMethod expandTemplate <tt>($params) -&gt; $string</tt></a></li>
11       </ul>
12     </li>
13   </ul>
14 </div>
15
16 The following tokens are supported by this language:
17
18 <table border="1" cellpadding="0" cellspacing="0">
19   <tr>
20     <td> %TMPL:P% </td>
21     <td> Instantiates a previously defined template </td>
22   </tr>
23   <tr>
24     <td> %TMPL:DEF% </td>
25     <td> Opens a template definition </td>
26   </tr>
27   <tr>
28     <td> %TMPL:END% </td>
29     <td> Closes a template definition </td>
30   </tr>
31   <tr>
32     <td> %TMPL:INCLUDE% </td>
33     <td> Includes another file of templates </td>
34   </tr>
35 </table>
36
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.
38
39 This is to all intents and purposes a singleton object. It could easily be coverted into a true singleton (template manager).
40
41 ## <a name="ClassMethod &lt;strong&gt;new&lt;/strong&gt; ($session)"></a> [[ClassMethod]] **new** `($session)`
42
43 Constructor. Creates a new template database object.
44
45 - $session - session (TWiki) object
46
47 ## <a name="ObjectMethod &lt;strong&gt;haveTemplate&lt;/strong&gt; ($na"></a> [[ObjectMethod]] **haveTemplate** `($name) -> $boolean`
48
49 Return true if the template exists and is loaded into the cache
50
51 ## <a name="ObjectMethod &lt;strong&gt;expandTemplate&lt;/strong&gt; ($"></a> [[ObjectMethod]] **expandTemplate** `($params) -> $string`
52
53 Expand the template specified in the parameter string using `tmplP`.
54
55 Examples:
56
57     $tmpls->expandTemplate('"blah");
58     $tmpls->expandTemplate('context="view" then="sigh" else="humph"');
59
60     ---++ ObjectMethod *tmplP* <tt>($attrs) -> $string</tt>
61
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.
65
66     Recursively expands any contained TMPL:P tags.
67
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.
73
74     ---++ ObjectMethod *readTemplate* <tt>($name,$skins,$web) -> $text</tt>
75
76     Return value: expanded template text
77
78     Reads a template, constructing a candidate name for the template thus
79        0 looks for file =$name.$skin.tmpl= (for each skin)
80           0 in =templates/$web=
81           0 in =templates=, look for
82        0 looks for file =$name.tmpl=
83           0 in =templates/$web=
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 ''.
95
96     =$skin=, =$web= and =$name= are forced to an upper-case first character
97     when composing user topic names.
98
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.