Class: TemplateTransformation
- Package: saf.Template
- Author: John Luxford <lux@simian.ca>
- Copyright: Copyright (C) 2001-2003, Simian Systems Inc.
- License: http://www.sitellite.org/index/license Simian Open Software License
- Version: 1.2, 2002-03-24, $Id: Transformation.php,v 1.3 2007/10/06 00:06:30 lux Exp $
- Access: public
TemplateTransformation is the class that Template uses internally
to handle the manipulation of values before they are substituted
into a template. It is often valuable to be able to call a function
or a regular expression on a value before it is shown. For instance,
when you are retrieving a date value straight from your database to
the template (using the [SQL: ...] directive), it is nice to be able
to format it how you like, instead of how the database decides it
should look.
The format of a transformation is as follows:
Tag name:Transformation type:Transformation rule
The tag name corresponds to the latter part of the name if the name
is of the form ##object:property##.
There are currently three types of transformations:
- func : a function call
- regex: a regular expression search and replace
- alternate : alternates the value of the variable between the two
provided (good for alternating background colours of table rows)
New 1.2:
- Added an optional list of variables to import from the global namespace.
Usage Example
<?php
A few basic transformations:
[Transformations:
bgcolor:alternate:#ffffff:#eeeeee
icon:regex:(.+):<img src="sitellite/\1" />
file_extension:func:strtoupper ("##file_extension##")
date:func:Date::format ("##date##", "F j, Y")
]
Class usage:
$transformation = new TemplateTransformation ('func', 'date', 'Date::format ("##date##", "F j, Y")');
echo $transformation->transform ('2001-12-24');
// can also be used like this:
// note: data is the contents of a [Transformations: ...] directive
$transformations =& TemplateTransformation::parse ($data);
foreach ($transformations as $key => $object) {
// do things with $key and $object here
}
?>
Return to Top
Properties
$path
The path to the template directory.
$cache = array ()
A cache for templates read from files, so if they are
called a second or third time, we don't have to read them from
the file system again.
$delim = array (
array ('##', '##'),
array ('\\{', '\\}'),
array ('\\(', '\\)'),
array ('\\.\\.\\.', '\\.\\.\\.'),
array ('<\\!-- put: ', ' -->'),
array ('<spt>', '<\\/spt>'),
array ('<% ', ' %>'),
)
List of predefined delimiters. A delimiter is a
2-element array with an opening and a closing delimiter
string (must be valid as a regular expression, no escaping
is done for you). Predefined delimiters are: double-pound
(ie. ##tag##), curly braces (ie. {tag}), round braces
(ie. (tag)), and triple-dots (ie. ...tag...).
$delim_literal = array (
array ('##', '##'),
array ('{', '}'),
array ('(', ')'),
array ('...', '...'),
array ('<!-- put: ', ' -->'),
array ('<spt>', '</spt>'),
array ('<% ', ' %>'),
)
List of predefined delimiters, represented minus
the slashes in place for insertion into regular expressions,
as the $delim list is.
$use_delim
Points to which delimiters to use (from the $delim
array).
$filter = 'htmlentities_compat'
Default filter for inserted vars is htmlentities_compat.
$_ignoreUntilEndLoop = false
Whether to ignore output until an end loop is found.
$_bufferUntilEndLoop = false
Whether to buffer output until an end loop is found.
$_ignoreUntilEndIf = false
Whether to ignore output until an end if is found.
$_loopList = array ()
List of loops.
$_loopBuffer = ''
Loop buffer.
$_structCount = 0
Count of structs (ifs and loops).
$type
The type of transformation. Can be 'regex', 'func', or 'alternate'.
$key
The name of the element to which the transformation belongs.
$rule
The actual transformation rule. See the examples above for format information.
Return to Top
Methods
TemplateTransformation ($type, $key, $rule)
Constructor method.
transform ($value, $import = '')
- Access: public
- Return: string
Performs the transformation on a given value.
&parse ($data = '')
- Access: public
- Return: associative array
Parses a block of transformations into an associative array of TemplateTransformation
objects (usually taken from a [Transformations: ...] directive).
Return to Top