The
class provides easy and quite fast template handling functionality.
Located in /cls_fast_template.php (line 239)
Constructor.
Assign variables
This method assigns values for variables. In order for a variable in a template to be interpolated it must be assigned. There are two forms which have some important differences. The simple form, is to accept an array and copy all the key/value pairs into an array in FastTemplate. There is only one array in FastTemplate, so assigning a value for the same key will overwrite that key.
assign template variables with the same names from array by specfied keys
NOTE: template variables will be in upper case
Clears the internal references that store data passed to parse().
clear() accepts individual references, or array references as arguments.
Note: All of the clear() functions are for use anywhere where your scripts are persistant. They generally aren't needed if you are writing CGI scripts.
Often clear() is at the end of a script:
or
If called with no arguments, removes ALL references that have been set via parse().
Cleans the module of any data, except for the ROOT directory.
This is equivalent to:
- $tpl->clear_define();
- $tpl->clear_href();
- $tpl->clear_tpl();
- $tpl->clear_parse();
Clears all variables set by assign()
Clears the internal list that stores data passed to:
$tpl->define();
Note: The hash that holds the loaded templates is not touched with this method. ( See: clear_tpl() ) Accepts a single file handle, an array of file handles, or nothing as arguments. If no argument is given, it clears ALL file handles.
- $tpl->define( array( MAIN => "main.html",
- BODY => "body.html", FOOT => "foot.html" ));
- (some code here)
- $tpl->clear_define("MAIN");
Strips a dynamic block from a template.
This provides a method to remove the dynamic block definition from the parent macro provided that you haven't already parsed the template. Using our example above:
- $tpl->clear_dynamic("row");
Would completely strip all of the unparsed dynamic blocks named ``row'' from the parent template. This method won't do a thing if the template has already been parsed! (Because the required BEGIN and END lines have been removed through the parsing) This method works well when you are accessing a database, and your ``rows'' may or may not return anything to print to the template. If your database query doesn't return anything, you can now strip out the rows you've set up for the results.
Removes a given reference from the list of refs that is built using:
$tpl->assign(KEY = val);
If it's called with no arguments, it removes all references from the array.
- $tpl->assign( array( MOVIE => "The Avengers", RATE => "BadMovie" ));
- $tpl->clear_href("MOVIE");
Does the same thing as the clear() function
Clears the internal array that stores the contents of the templates (if they have been loaded)
If you are having problems with template changes not being reflected, try adding this method to your script.
The method define() maps a template filename to a (usually shorter) name;
This new name is the name that you will use to refer to the templates. Filenames should not appear in any place other than a define().
- $tpl = new FastTemplate("/path/to/templates");
- $tpl->define( array( main => "main.html", footer => "footer.html" ));
You can define dynamic content within a static template.
Here's an example of define_dynamic();
This tells FastTemplate that buried in the ``table'' template is a dynamic block, named ``row''. In older verions of FastTemplate (pre 0.7) this ``row'' template would have been defined as it's own file. Here's how a dynamic block appears within a template file;
- $tpl = new FastTemplate("./templates");
- $tpl->define( array( main => "main.html",table => "dynamic.html" ));
- $tpl->define_dynamic( "row" , "table" );
- <!-- NAME: dynamic.html -->
- <table>
- <!-- BEGIN DYNAMIC BLOCK: row -->
- <tr>
- <td>{NUMBER}</td>
- <td>{BIG_NUMBER}</td>
- </tr>
- <!-- END DYNAMIC BLOCK: row -->
- </table>
- <!-- END: dynamic.html -->
The syntax of your BEGIN and END lines needs to be VERY exact. It is case sensitive. The code block begins on a new line all by itself. There cannot be ANY OTHER TEXT on the line with the BEGIN or END statement. (although you can have any amount of whitespace before or after) It must be in the format shown:
<!-- BEGIN DYNAMIC BLOCK: handle_name -->
The line must be exact, right down to the spacing of the characters. The same is true for your END line. The BEGIN and END lines cannot span multiple lines. Now when you call the parse() method, FastTemplate will automatically spot the dynamic block, strip it out, and use it exactly as if you had defined it as a stand-alone template. No additional work is required on your part to make it work - just define it, and FastTemplate will do the rest. Included with this archive should have been a file named define_dynamic.phtml which shows a working example of a dynamic block.
There are a few rules when using dynamic blocks - dynamic blocks should not be nested inside other dynamic blocks - strange things WILL occur. You -can- have more than one nested block of code in a page, but of course, no two blocks can share the same defined handle. The error checking for define_dynamic() is miniscule at best. If you define a dynamic block and FastTemplate fails to find it, no errors will be generated, just really weird output. (FastTemplate will not append the dynamic data to the retured output) Since the BEGIN and END lines are stripped out of the parsed results, if you ever see your BEGIN or END line in the parsed output, that means that FastTemplate failed to find that dynamic block.
Try to delete used cached files.
Put an error message and stop (if requested)
Prints parsed template
The method FastPrint() prints the contents of the named variable. If no variable is given, then it prints the last variable that was used in a call to parse() which I find is a reasonable default.
This method is provided for convenience. If you need to print somewhere else (a socket, file handle) you would want to fetch() a reference to the data first:
Output the HTML-Code to a file.
The method FastWrite() write the contents of the named variable into a file.
This method is provided for convenience. If you need to print somewhere else (a socket, file handle) you would want to fetch() a reference to the data first:
To write into a folder, depend on server configuration.
Returns the raw data from a parsed handle.
Example:
Return the value of an assigned variable.
This method will return the value of a variable that has been set via assign(). This allows you to easily pass variables around within functions by using the FastTemplate class to handle ``globalization'' of the variables.
For example:
- $tpl->assign( array( TITLE => $title,
- BGCOLOR => $bgColor, TEXT => $textColor ));
- $bgColor = $tpl->get_assigned(BGCOLOR);
Return value of ROOT templates directory
Grabs a template from the root dir and
reads it into a (potentially REALLY) big string
A quick check of the template file before reading it.
This is -not- a reliable check, mostly due to inconsistencies in the way PHP determines if a file is readable.
Pattern Assign
Pattern Assign - when variables or constants are the same as the template keys, these functions may be used as they are. Using these functions, can help you reduce the number of the assign functions in the php files
Useful for language files where all variables or constants have the same prefix.i.e. $LANG_SOME_VAR or LANG_SOME_CONST
The $pattern is LANG in this case.
Same as multiple_assign(), but for constants (defines)
Turns off warning messages about unresolved template variables.
A call to no_strict() is required to replace unknown variables with an empty string. By default, all instances of FastTemplate behave as is strict() was called. Also, no_strict() must be set for each instance of FastTemplate.
- $tpl = new FastTemplate("/path/to/templates");
- $tpl->no_strict();
This is the main function in FastTemplate
It accepts a new key value pair where the key is the TARGET and the values are the SOURCE templates. There are three forms this can be in:
In the regular version, the template named ``main'' is loaded if it hasn't been already, all the variables are interpolated, and the result is then stored in FastTemplate as the value MAIN. If the variable '{MAIN}' shows up in a later template, it will be interpolated to be the value of the parsed ``main'' template. This allows you to easily nest templates, which brings us to the compound style. The compound style is designed to make it easier to nest templates. The following are equivalent:
is the same as:
this form saves function calls and makes your code cleaner.
- $tpl->parse(MAIN, array("table", "main"));
It is important to note that when you are using the compound form, each template after the first, must contain the variable that you are parsing the results into. In the above example, 'main' must contain the variable '{MAIN}', as that is where the parsed results of 'table' is stored. If 'main' does not contain the variable '{MAIN}' then the parsed results of 'table' will be lost.
The append style allows you to append the parsed results to the target variable. Placing a leading dot . before a defined file handle tells FastTemplate to append the parsed results of this template to the returned results. This is most useful when building tables that have an dynamic number of rows - such as data from a database query.
Parse param string and replace simple variable
Parse template & return it
Sets template root All templates will be loaded from this "root" directory Can be changed in mid-process by re-calling with a new value.
Prints debug info into console
Prints the warnings for unresolved variable references
in template files. Used if STRICT is true
Enable strict templeate checking
When strict() is on (it is on by default) all variables found during template parsing that are unresolved have a warning printed to STDERR;
[FastTemplate] Warning: no value found for variable: SOME_VAR
Also, the variables will be left in the output document. This was done for two reasons: to allow for parsing to be done in stages (i.e. multiple passes), and to make it easier to identify undefined variables since they appear in the parsed output.
Note: STDERR output should be captured and logged by the webserver. With apache (and unix!) you can tail the error log during development to see the results as in:
tail -f /var/log/httpd/error_log
Try to use cached files.
Documentation generated on Tue, 10 Jan 2006 11:21:32 +0200 by phpDocumentor 1.3.0RC4