root/branches/3.1/docs/extending-pattemplate.txt

Revision 97, 8.1 kB (checked in by schst, 5 years ago)

updated docs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 Extending patTemplate:
2 ----------------------
3 $Id$
4
5 1. Variable Modifiers
6 ---------------------
7 Variable modifiers will be applied to the value of a variable when
8 parsing the template. There two types of modfifiers:
9
10 1.a) You may use any PHP function as a variable modifier.
11 The modifier must accept only a string as a parameter and also return
12 a string. Perfect examples for variable modifiers are nl2br() or
13 htmlentities().
14
15 2.b) You may create custom modifiers. To do this, create a new file in
16
17   PATTEMPLATE_ROOT/patTemplate/Modifier
18  
19 or any of its sub-folders. In this file, you have to create a new class
20 that's called
21
22   patTemplate_Modifier_FILENAME
23  
24 where FILENAME is the name of your file (and thus also of your modifier).
25 If your modifier is located in a subfolder, you have to include the folder
26 name in the class name, e.g. patTemplate_Modifier_Html_Image.
27
28 Your class should inherit from patTemplate_Modifier and provide the following
29 method:
30
31   string modify( string value, array params )
32  
33 In the method you may modify the value and return it to the templating enginge.
34
35 2. Template Readers
36 -------------------
37 patTemplate 3.0 allows you to read templates from any source,
38 may it be the filesystem, databases or shared memory.
39 To read from a datasource that's not yet supported, you'll have to
40 implement a new template reader. To do this, create a new PHP file in:
41
42   PATTEMPLATE_ROOT/patTemplate/Reader
43  
44 Let's say you want to create a reader that reads data from shared memory.
45 Then you'd create a new file in the mentioned folder called
46
47   SHM.php
48  
49 In this file you place a create a new class that extends patTemplate_Reader
50 and that is called patTemplate_Reader_SHM. There you'll have to implement only
51 one method that reads templates from the source:
52
53 <?PHP
54 class patTemplate_Reader_SHM extends patTemplate_Reader
55 {
56         function readTemplates( $input, $options = array() )
57         {
58                 /**
59                  * somehow access shared memory
60                  */
61                 $content        = shm_get( $shm, $input );
62                 $templates      = $this->parseString( $content );
63                
64                 return  $templates;
65         }
66 }
67
68 Options is currently not used, so this may change until patTemplate 3.0
69 stable is released.
70
71 If your reader should support the parse="off" attribute you'll have to
72 implement a second method loadTemplate(), that accepts exactly the same
73 parameters as readTemplates() and will just load the template content from
74 a datasource and return it as a string.
75
76 In order to use template caches (see 7), you'll have to implement
77 to other methods:
78
79 string  getKey( string input )
80 integer getModificationTime( string input )
81
82 getKey() should return a unique identification key for the template that
83 is referenced by input, a common way to achive this is to use md5().
84 getModificationTime() should return a timestamp of the time the file was
85 modified on, for a lot of readers this is not possible. In that case, you do not
86 need to implement the method, as it's already implemented in the base class.
87
88 You can als use the String reader if you need to parse templates that are
89 read from strange datasources, but you will loose the template caching feature.
90
91 Of course it's also possible to use the file reader and PHP's stream_wrappers.
92
93 3. Custom Template Tags (functions)
94 -----------------------------------
95 Custom Tags allow you to extend patTemplate. You may define classes that
96 are instantiated and evaluated,, when patTemplate finds a tag that is not
97 supported by the engine. Custom functions are evaluated by the template reader.
98 To create your own template function, create a new class that inherits from
99 patTemplate_Function and place it into
100
101   PATTEMPLATE_ROOT/patTemplate/Function
102  
103 In this class you have to implement on function:
104
105   string call( array params, string content )
106  
107 This method will be called on the closing tag with the same name as
108 your file (and class). patTemplate_Reader will pass the attributes
109 and the content of the tag to your method. You will then have to
110 compute the data you want to insert to the template and return the
111 string. This will be inserted instead of your custom tag.
112
113 Custom Template Functions allow to do things like:
114  - Modify parts of the template while reading it
115  - Insert the current date/time
116  - Get request variables and insert them into the template
117  - ...
118
119 Custom template functions (or tags) can also be called Horst. If you want to
120 know, why they are called Horst, you could contact gERD or Argh.
121
122 4. Output Filters
123 -----------------
124 Output filters allow you to modfiy the data that is sent to
125 the browser, after _all_ templates have been parsed. Output filters
126 are applied, when patTemplate::displayParsedTemplate() is called.
127 Examples for output filtering are the removal of all whitespace prior
128 to sending data to the browser or even compressing the data using gzip,
129 if the client supports gzip encoding. In fact these two examples already
130 accompany the patTemplate distributions.
131
132 Of course there are infinite possibilities of what an output filter
133 could to, and that's why I implemented it as a plugin to patTemplate.
134 To build your own output filter you simply have to create a new file in
135
136   PATTEMPLATE_ROOT/patTemplate/OutputFilter
137  
138 Let's say you want to create a filter that obfuscates email addresses in the
139 output. Then you'd create a new file in the mentioned folder called
140
141   EmailObfuscator.php
142  
143 In this file you place a create a new class that extends patTemplate_OutputFilter
144 and that is called patTemplate_OutputFilter_EmailObfuscator. There you'll have to
145 implement only one method that filters the output and this method has to be called
146 apply() and should only accept a string a sole parameter. So your file should look
147 like this:
148
149 <?PHP
150 class patTemplate_OutputFilter_EmailObfuscator extends patTemplate_OutputFilter
151 {
152         function apply( $data )
153         {
154                 /**
155                  * somehow obfuscate emails here
156                  */
157                 return $data;
158         }
159 }
160 ?>
161
162 Now if you want to use this output filter, all you have to do is:
163
164 $tmpl->applyOutputFilter( 'EmailObfuscator' );
165 $tmpl->displayParsedTemplate();
166
167 And patTemplate will do the rest.
168
169 5. Input Filters
170 ----------------
171 Input filters are used to modify the templates _before_ the patTemplate
172 tags are analyzed. This allows you to strip comments and improve performance
173 while parsing the templates.
174 They are implemented in exactly the same way as an output filter, they just
175 have to be place in the InputFilter directory and should extend the
176 patTemplate_InputFilter class.
177 You could use an input filter to:
178  - strip HTML comments
179  - unzip the templates
180  - ...
181
182 6. Dumpers
183 ----------
184 Dumpers are used for debugging purposes. To write your own dumper, create a new
185 class that is inherited from patTemplate_Dump and place it into the
186 patTemplate/Dump folder. The file has to be called exactly like the last
187 portion of the class name.
188 That means if you create a text dumper, call the file
189 /patTemplate/Dump/Text.php and the class patTemplate_Dump_Text.
190
191 You have to create four methods in this class:
192 void displayHeader()
193 void displayFooter()
194 void dumpGlobals( array globals )
195 void dumpTemplates( array templates, array vars )
196
197 In this methods you should only output data.
198 'globals' is an associative array containing all global variables,
199 'templates' is an associative array containing all templates. Each entry
200 is an array that contains all template properties. The 'vars' parameter
201 is an array that contains the variables of the templates.
202
203 7. Template Caches
204 ------------------
205 A template cache is invoked when patTemplate::readTemplatesFromInput() is
206 called. It stores the serialized template so it does not have to be parsed
207 on every request. A template cache needs to implement to methods:
208
209 array load( int key[, array modificationTime] )
210 boolean write( int key, array templates )
211
212 When calling load, the template cache should read and unserialize the templates
213 from the storage container and return the array. The modification time will
214 only be passed, if the reader supports this feature (e.g. there's no modification
215 time when reading from a string).
216
217 The write method has to work just the other way round. It should serialize the
218 templates and write them to the storage container.
219
220 8. Output Caches
221 ----------------
222 Output caches are not implemented, yet.
223 They will be part of patTemplate 3.1.0
Note: See TracBrowser for help on using the browser.