Class FastTemplate

Description

The

  1. FastTemplate
class provides easy and quite fast template handling functionality.

  • author: CDI cdi@thewebmasters.net
  • author: Jason Moore jmoore@sober.com.
  • author: Artyem V. Shkondin aka AiK artvs@clubpro.spb.ru
  • author: Allyson Francisco de Paula Reis ragen@oquerola.com
  • author: GraFX Software Solutions webmaster@grafxsoftware.com
  • author: Wilfried Trinkl wisl@gmx.at projects based on Fast Templates at www.grafxsoftware.com

Located in /cls_fast_template.php (line 239)


	
			
Method Summary
FastTemplate FastTemplate ([template $pathToTemplates = ""])
void assign (mixed $ft_array, [mixed $trailer = ""])
void assign_from_array (mixed $Arr, mixed $Keys)
void clear ([mixed $ReturnVar = ""])
void clear_all ()
void clear_assign ()
void clear_define ([mixed $FileTag = ""])
void clear_dynamic ([input $Macro = ""])
void clear_href (mixed $href)
void clear_parse ([mixed $ReturnVar = ""])
void clear_tpl ([mixed $fileHandle = ""])
void define (mixed $fileList, [mixed $value = null])
void define_dynamic (input $Macro, input $ParentName)
void DELETE_CACHE ()
void error (string $errorMsg, bool $die)
void FastPrint ([$template $template = ""], [mixed $return = ""])
void FastWrite ([input $template = ""], outfile $outputfile)
string fetch ([mixed $template = ""])
void get_assigned ([mixed $ft_name = ""])
root get_root ()
string get_template ($template $template)
boolean is_safe (mixed $filename)
void multiple_assign (mixed $pattern)
void multiple_assign_define (mixed $pattern)
void no_strict ()
void parse (mixed $ReturnVar, mixed $FileTags)
string parseParamString (string $string)
string parse_and_return ($tpl_name $tpl_name)
void set_root ($root $root)
void showDebugInfo ([mixed $Debug_type = null])
void show_unknowns ($Line $Line)
void strict ()
void USE_CACHE ([string $fname = ""])
Methods
Constructor FastTemplate (line 355)

Constructor.

  • return: FastTemplate object
FastTemplate FastTemplate ([template $pathToTemplates = ""])
  • template $pathToTemplates: root.
assign (line 1696)

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.

  1. $tpl->assign(TITLE => "king kong");
  2. $tpl->assign(TITLE => "gozilla");

void assign (mixed $ft_array, [mixed $trailer = ""])
assign_from_array (line 1678)

assign template variables with the same names from array by specfied keys

NOTE: template variables will be in upper case

void assign_from_array (mixed $Arr, mixed $Keys)
clear (line 1459)

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:

  1. $tpl->FastPrint("MAIN");
  2. $tpl->clear("MAIN");
or
  1. $tpl->FastPrint("MAIN");
  2. $tpl->FastPrint("CONTENT");
  3. $tpl->clear(array("MAIN","CONTENT"));
If called with no arguments, removes ALL references that have been set via parse().

void clear ([mixed $ReturnVar = ""])
clear_all (line 1504)

Cleans the module of any data, except for the ROOT directory.

This is equivalent to:

  1. $tpl->clear_define();
  2. $tpl->clear_href();
  3. $tpl->clear_tpl();
  4. $tpl->clear_parse();

void clear_all ()
clear_assign (line 1621)

Clears all variables set by assign()

void clear_assign ()
clear_define (line 1587)

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.

  1. $tpl->define( array( MAIN => "main.html",
  2. BODY => "body.html", FOOT => "foot.html" ));
  3. (some code here)
  4. $tpl->clear_define("MAIN");

void clear_define ([mixed $FileTag = ""])
clear_dynamic (line 1346)

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:

  1. $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.

void clear_dynamic ([input $Macro = ""])
  • input $Macro: Macro name
clear_href (line 1646)

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.

  1. $tpl->assign( array( MOVIE => "The Avengers", RATE => "BadMovie" ));
  2. $tpl->clear_href("MOVIE");

void clear_href (mixed $href)
clear_parse (line 1432)

Does the same thing as the clear() function

void clear_parse ([mixed $ReturnVar = ""])
clear_tpl (line 1525)

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.

  1. $tpl->define(MAIN,"main.html" );
  2. ( assign(), parse() etc etc...)
  3. $tpl->clear_tpl(MAIN);

void clear_tpl ([mixed $fileHandle = ""])
define (line 1417)

The method define() maps a template filename to a (usually shorter) name;

  1. $tpl = new FastTemplate("/path/to/templates");
  2. $tpl->define( array( main => "main.html", footer => "footer.html" ));
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().
(Note: This is a required step! This may seem like an annoying extra step when you are dealing with a trivial example like the one above, but when you are dealing with dozens of templates, it is very handy to refer to templates with names that are indepandant of filenames.)
TIP: Since define() does not actually load the templates, it is faster and more legible to define all the templates with one call to define().

void define (mixed $fileList, [mixed $value = null])
define_dynamic (line 1252)

You can define dynamic content within a static template.

Here's an example of define_dynamic();

  1. $tpl = new FastTemplate("./templates");
  2. $tpl->define( array( main => "main.html",table => "dynamic.html" ));
  3. $tpl->define_dynamic( "row" , "table" );
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;

  1. <!-- NAME: dynamic.html -->
  2. <table>
  3. <!-- BEGIN DYNAMIC BLOCK: row -->
  4. <tr>
  5. <td>{NUMBER}</td>
  6. <td>{BIG_NUMBER}</td>
  7. </tr>
  8. <!-- END DYNAMIC BLOCK: row -->
  9. </table>
  10. <!-- 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.

void define_dynamic (input $Macro, input $ParentName)
  • input $Macro: Macro Name
  • input $ParentName: Parent Name
DELETE_CACHE (line 1027)

Try to delete used cached files.

void DELETE_CACHE ()
error (line 1758)

Put an error message and stop (if requested)

void error (string $errorMsg, bool $die)
  • string $errorMsg
  • bool $die
FastPrint (line 979)

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.

  1. $tpl->FastPrint();
  2. // print the value of MAIN
  3. $tpl->FastPrint("MAIN");

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:

  1. $data = $tpl->fetch("MAIN");
  2. fwrite($fd, $data);

void FastPrint ([$template $template = ""], [mixed $return = ""])
  • $template $template: template handler
FastWrite (line 947)

Output the HTML-Code to a file.

The method FastWrite() write the contents of the named variable into a file.

  1. $tpl->FastWrite("output.html"); // continuing from the last example, would
  2. $tpl->FastWrite("MAIN","output.html");

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:

  1. $data = $tpl->fetch("MAIN");
  2. fwrite($fd, $data);
To write into a folder, depend on server configuration.

void FastWrite ([input $template = ""], outfile $outputfile)
  • input $template: template
  • outfile $outputfile
fetch (line 1203)

Returns the raw data from a parsed handle.

Example:

  1. $tpl->parse(CONTENT, "main");
  2. $content = $tpl->fetch("CONTENT");
  3. print $content; // print to STDOUT
  4. fwrite($fd, $content);

  • return: rawdata
string fetch ([mixed $template = ""])
get_assigned (line 1739)

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:

  1. $tpl->assign( array( TITLE => $title,
  2. BGCOLOR => $bgColor, TEXT => $textColor ));
  3. $bgColor = $tpl->get_assigned(BGCOLOR);

void get_assigned ([mixed $ft_name = ""])
get_root (line 419)

Return value of ROOT templates directory

  • return: dir value with trailing slash by Voituk Vadim
root get_root ()
get_template (line 510)

Grabs a template from the root dir and

reads it into a (potentially REALLY) big string

string get_template ($template $template)
  • $template $template: template name
is_safe (line 494)

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.

boolean is_safe (mixed $filename)
multiple_assign (line 1785)

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.

void multiple_assign (mixed $pattern)
multiple_assign_define (line 1799)

Same as multiple_assign(), but for constants (defines)

void multiple_assign_define (mixed $pattern)
no_strict (line 483)

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.

  1. $tpl = new FastTemplate("/path/to/templates");
  2. $tpl->no_strict();

void no_strict ()
parse (line 819)

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:

  1. $tpl->parse(MAIN, "main"); // regular
  2. $tpl->parse(MAIN, array ( "table", "main") ); // compound
  3. $tpl->parse(MAIN, ".row");

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:

  1. $tpl->parse(MAIN, "table");
  2. $tpl->parse(MAIN, ".main");
is the same as:
  1. $tpl->parse(MAIN, array("table", "main"));
this form saves function calls and makes your code cleaner.

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.

void parse (mixed $ReturnVar, mixed $FileTags)
parseParamString (line 570)

Parse param string and replace simple variable

string parseParamString (string $string)
  • string $string
parse_and_return (line 373)

Parse template & return it

string parse_and_return ($tpl_name $tpl_name)
  • $tpl_name $tpl_name: - name of template to parse
set_root (line 390)

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.

void set_root ($root $root)
  • $root $root: path to templates dir
showDebugInfo (line 1818)

Prints debug info into console

  • since: 1.1.1 modified by GRAFX, added 2 Levels of debugging. Level 1 is showing all info + added WARNINGS Level 2 will popup the window only if WARNINGS are present, very helpfull only when you want to see BUGS on your page
  • author: AiK
void showDebugInfo ([mixed $Debug_type = null])
show_unknowns (line 548)

Prints the warnings for unresolved variable references

in template files. Used if STRICT is true

void show_unknowns ($Line $Line)
  • $Line $Line: string for variable references checking
strict (line 467)

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

void strict ()
USE_CACHE (line 1005)

Try to use cached files.

void USE_CACHE ([string $fname = ""])
  • string $fname: forldername

Documentation generated on Tue, 10 Jan 2006 11:21:32 +0200 by phpDocumentor 1.3.0RC4