\patTemplate_Compiler

Compiler for patTemplate

$Id: Compiler.php 206 2004-05-14 16:49:34Z schst $

WARNING: This is still experimental!

Summary

Methods
Properties
Constants
__construct()
setOption()
getOption()
setBasedir()
setRoot()
getRoot()
setNamespace()
getNamespace()
setDefaultAttribute()
setDefaultAttributes()
getDefaultAttributes()
setType()
setTags()
getStartTag()
getEndTag()
addModuleDir()
setAttribute()
setAttributes()
getAttributes()
getAttribute()
clearAttribute()
prepareTemplate()
addVar()
getVar()
clearVar()
addVars()
clearVars()
addRows()
add_common_jomres_strings()
curPageURL()
addObject()
getObjectVars()
addGlobalVar()
clearGlobalVar()
clearGlobalVars()
addGlobalVars()
getGlobalVars()
exists()
useTemplateCache()
applyOutputFilter()
applyInputFilter()
readTemplatesFromFile()
readTemplatesFromInput()
_loadTemplatesFromCache()
loadTemplateFromInput()
loadTemplate()
loadModule()
moduleExists()
parseTemplate()
_initTemplate()
_parseVariables()
_parseGlobals()
_applyModifers()
_parseDependencies()
_fetchTemplate()
_hasVariables()
_getConditionValue()
_fetchVariables()
_handleUnusedVars()
getParsedTemplate()
output_template_name_details()
displayParsedTemplate()
parseIntoVar()
clearTemplate()
clearAllTemplates()
freeTemplate()
freeAllTemplates()
_getDependencies()
dump()
getIncludePath()
applyInputFilters()
placeholderExists()
__toString()
compile()
compileTemplate()
_compileStandardTemplate()
_compileModuloTemplate()
_compileSimpleConditionTemplate()
_compileConditionTemplate()
_compileBuiltinConditions()
_templateToPHP()
_addToCode()
_getVar()
_prepareCompiledTemplate()
$_systemVars
$_defaultAttributes
$_options
$_startTag
$_endTag
$_modules
$_moduleDirs
$_templateList
$_templates
$_globals
$_vars
$_root
$_outputFilters
$_inputFilters
$_tmplCache
$_discoveredPlaceholders
$_compiledTemplates
$_fp
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Properties

$_systemVars

$_systemVars : array

standard system vars that identify pat tools

Type

array

$_defaultAttributes

$_defaultAttributes : array

default attributes for new templates

Type

array

$_options

$_options : array

options for patTemplate

Currently the following options are implemented:

  • maintainBc defines, whether patTemplate should be backwards compatible. This means, that you may use 'default' and 'empty' for subtemplates.

Type

array

$_startTag

$_startTag : string

start tag

Type

string

$_endTag

$_endTag : string

end tag

Type

string

$_modules

$_modules : array

loaded modules

Modules are:

  • Readers
  • Caches
  • Variable modifiers
  • Filters

Type

array

$_moduleDirs

$_moduleDirs : array

directories, where modules can be stored

Type

array

$_templateList

$_templateList : array

stores all template names

Type

array

$_templates

$_templates : array

stores all template data

Type

array

$_globals

$_globals : array

stores all global variables

Type

array

$_vars

$_vars : array

stores all local variables

Type

array

$_root

$_root : string

stores the name of the first template that has been found

Type

string

$_outputFilters

$_outputFilters : array

output filters that should be used

Type

array

$_inputFilters

$_inputFilters : array

input filters that should be used

Type

array

$_tmplCache

$_tmplCache : array

template cache, that should be used

Type

array

$_discoveredPlaceholders

$_discoveredPlaceholders : array

placeholders, that have been discovered

Type

array

$_compiledTemplates

$_compiledTemplates : \array()

list of all templates that already have been compiled

Type

\array()

$_fp

$_fp : resource

file pointer to the compiled template

Type

resource

Methods

__construct()

__construct(  $type = 'html') 

constructor

Creates a new patTemplate Compiler

Parameters

$type

setOption()

setOption(  $option,   $value) 

sets an option

Currently, the following options are supported

  • maintainBc (true|false)
  • namespace (string)

Parameters

$option
$value

getOption()

getOption(  $option) : mixed

gets an option

Parameters

$option

Returns

mixed —

value of the option

setBasedir()

setBasedir(  $basedir) 

sets name of directory where templates are stored

Parameters

$basedir

setRoot()

setRoot(  $root,   $reader = '__default') 

sets root base for the template

The parameter depends on the reader you are using.

Parameters

$root
$reader

getRoot()

getRoot(  $reader = '__default') : mixed

gets name of root base for the templates

Parameters

$reader

Returns

mixed —

root base

setNamespace()

setNamespace(  $ns) 

sets namespace of patTemplate tags

If you want to use more than one namespace, you may set this to an array. All tags in these namespaces will be treated as patTemplate tags.

Parameters

$ns

getNamespace()

getNamespace() : string|array

gets namespace of patTemplate tags

Returns

string|array —

namespace(s)

setDefaultAttribute()

setDefaultAttribute(  $name,   $value) 

set default attribute

Parameters

$name
$value

setDefaultAttributes()

setDefaultAttributes(  $attributes) 

set default attributes

Parameters

$attributes

getDefaultAttributes()

getDefaultAttributes() : \return

get default attributes

Returns

\return —

default attributes

setType()

setType(  $type) : boolean

set the type for the templates

Parameters

$type

Returns

boolean —

true on success

setTags()

setTags(  $startTag,   $endTag) : boolean

set the start and end tag for variables

Parameters

$startTag
$endTag

Returns

boolean —

true on success

getStartTag()

getStartTag() : string

get start tag for variables

Returns

string —

start tag

getEndTag()

getEndTag() : string

get end tag for variables

Returns

string —

end tag

addModuleDir()

addModuleDir(  $moduleType,   $dir) 

add a directory where patTemplate should search for modules.

You may either pass a string or an array of directories.

patTemplate will be searching for a module in the same order you added them. If the module cannot be found in the custom folders, it will look in patTemplate/$moduleType.

Parameters

$moduleType
$dir

setAttribute()

setAttribute(string  $template, string  $attribute, mixed  $value) 

Sets an attribute of a template

supported attributes: visibilty, loop, parse, unusedvars

Parameters

string $template

name of the template

string $attribute

name of the attribute

mixed $value

value of the attribute

setAttributes()

setAttributes(string  $template, array  $attributes) 

Sets several attribute of a template

$attributes has to be a assotiative arrays containing attribute/value pairs supported attributes: visibilty, loop, parse, unusedvars

Parameters

string $template

name of the template

array $attributes

attribute/value pairs

getAttributes()

getAttributes(  $template) : array

Get all attributes of a template

Parameters

$template

Returns

array —

attributes

getAttribute()

getAttribute(string  $template, string  $attribute) : mixed

Gets an attribute of a template

supported attributes: visibilty, loop, parse, unusedvars

Parameters

string $template

name of the template

string $attribute

name of the attribute

Returns

mixed —

value of the attribute

clearAttribute()

clearAttribute(string  $template, string  $attribute) 

Clears an attribute of a template

supported attributes: visibilty, loop, parse, unusedvars

Parameters

string $template

name of the template

string $attribute

name of the attribute

prepareTemplate()

prepareTemplate(  $name) 

Prepare a template

This can be used if you want to add variables to a template, that has not been loaded yet.

Parameters

$name

addVar()

addVar(string  $template, string  $varname, mixed  $value) 

add a variable to a template

A variable may also be an indexed array, but not an associative array!

Parameters

string $template

name of the template

string $varname

name of the variable

mixed $value

value of the variable

getVar()

getVar(  $template,   $varname) : string

get the value of a variable

Parameters

$template
$varname

Returns

string —

value of the variable, null if the variable is not set

clearVar()

clearVar(  $template,   $varname) : boolean

clear the value of a variable

Parameters

$template
$varname

Returns

boolean

addVars()

addVars(string  $template, array  $variables, string  $prefix = '') 

Adds several variables to a template

Each Template can have an unlimited amount of its own variables $variables has to be an assotiative array containing variable/value pairs

Parameters

string $template

name of the template

array $variables

assotiative array of the variables

string $prefix

prefix for all variable names

clearVars()

clearVars(string  $template) : boolean

Clear all variables in a template

This clears only variables, but does

Parameters

string $template

name of the template

Returns

boolean

addRows()

addRows(string  $template, array  $rows, string  $prefix = '') 

Adds several rows of variables to a template

Each Template can have an unlimited amount of its own variables Can be used to add a database result as variables to a template

Parameters

string $template

name of the template

array $rows

array containing assotiative arrays with variable/value pairs

string $prefix

prefix for all variable names

add_common_jomres_strings()

add_common_jomres_strings() 

curPageURL()

curPageURL() 

addObject()

addObject(  $template,   $object,   $prefix = '',   $ignorePrivate = false) 

Adds an object to a template

All properties of the object will be available as template variables.

Parameters

$template
$object
$prefix
$ignorePrivate

getObjectVars()

getObjectVars(  $obj,   $ignorePrivate = false) : array

get the vars from an object

Parameters

$obj
$ignorePrivate

Returns

array

addGlobalVar()

addGlobalVar(string  $varname, string  $value) : boolean

Adds a global variable

Global variables are valid in all templates of this object. A global variable has to be scalar, it will be converted to a string.

Parameters

string $varname

name of the global variable

string $value

value of the variable

Returns

boolean —

true on success

clearGlobalVar()

clearGlobalVar(string  $varname) : boolean

Clears a global variable

Parameters

string $varname

name of the global variable

Returns

boolean —

true on success

clearGlobalVars()

clearGlobalVars() : boolean

Clears all global variables

Returns

boolean —

true on success

addGlobalVars()

addGlobalVars(array  $variables, string  $prefix = '') : boolean

Adds several global variables

Global variables are valid in all templates of this object.

$variables is an associative array, containing name/value pairs of the variables.

Parameters

array $variables

array containing the variables

string $prefix

prefix for variable names

Returns

boolean —

true on success

getGlobalVars()

getGlobalVars() : array

get all global variables

Returns

array —

global variables

exists()

exists(  $name) : boolean

checks wether a template exists

Parameters

$name

Returns

boolean —

true, if the template exists, false otherwise

useTemplateCache()

useTemplateCache(  $cache,   $params = array()) : boolean

enable a template cache

A template cache will improve performace, as the templates do not have to be read on each request.

Parameters

$cache
$params

Returns

boolean —

true on success, patError otherwise

applyOutputFilter()

applyOutputFilter(  $filter,   $params = array(),   $template = null) : boolean

enable an output filter

Output filters are used to modify the template result before it is sent to the browser.

They are applied, when displayParsedTemplate() is called.

Parameters

$filter
$params
$template

Returns

boolean —

true on success, patError otherwise

applyInputFilter()

applyInputFilter(  $filter,   $params = array()) : boolean

enable an input filter

input filters are used to modify the template stream before it is split into smaller templates-

Parameters

$filter
$params

Returns

boolean —

true on success, patError otherwise

readTemplatesFromFile()

readTemplatesFromFile(  $filename) : \true,

open a file and parse for patTemplate tags

Parameters

$filename

Returns

\true, —

if the template could be parsed

readTemplatesFromInput()

readTemplatesFromInput(  $input,   $reader = 'Jomres',   $options = null,   $parseInto = null) : boolean

open any input and parse for patTemplate tags

Parameters

$input
$reader
$options
$parseInto

Returns

boolean —

true, if the template could be parsed, false otherwise

_loadTemplatesFromCache()

_loadTemplatesFromCache(  $input,   $reader,   $options,   $key) : array|boolean

load from template cache

Parameters

$input
$reader
$options
$key

Returns

array|boolean —

either an array containing the templates, or false

loadTemplateFromInput()

loadTemplateFromInput(  $input,   $reader = 'File',   $options = null,   $parseInto = false) : boolean

open any input and load content into template

Parameters

$input
$reader
$options
$parseInto

Returns

boolean —

true, if the template could be parsed, false otherwise

loadTemplate()

loadTemplate(  $template) : boolean

load a template that had autoload="off"

This is needed, if you change the source of a template and want to load it, after changing the attribute.

Parameters

$template

Returns

boolean —

true, if template could be loaded

loadModule()

loadModule(  $moduleType,   $moduleName,   $params = array(),   $new = false) : object

loads a patTemplate module

Modules are located in the patTemplate folder and include:

  • Readers
  • Caches
  • Variable Modifiers
  • Filters
  • Functions
  • Stats

Parameters

$moduleType
$moduleName
$params
$new

Returns

object

moduleExists()

moduleExists(  $moduleType,   $moduleName) : boolean

checks whether a module exists.

Modules are located in the patTemplate folder and include:

  • Readers
  • Caches
  • Variable Modifiers
  • Filters
  • Functions
  • Stats

Parameters

$moduleType
$moduleName

Returns

boolean

parseTemplate()

parseTemplate(  $template,   $mode = 'w',   $disableAutoClear = false) 

parses a template

Parses a template and stores the parsed content. mode can be "w" for write (delete already parsed content) or "a" for append (appends the new parsed content to the already parsed content)

Parameters

$template
$mode
$disableAutoClear

_initTemplate()

_initTemplate(  $template) : boolean

Initialize a template

This method checks the variable specifications and copys variables from other templates.

Parameters

$template

Returns

boolean —

true on success

_parseVariables()

_parseVariables(  $template) 

parse all variables in a template

Parameters

$template

_parseGlobals()

_parseGlobals(  $template) : boolean

parse global variables in the template

Parameters

$template

Returns

boolean

_applyModifers()

_applyModifers(  $template,   $vars) : boolean

apply variable modifiers

The variables will be passed by reference.

Parameters

$template
$vars

Returns

boolean

_parseDependencies()

_parseDependencies(  $template,   $disableAutoClear = false) 

parse all dependencies in a template

Parameters

$template
$disableAutoClear

_fetchTemplate()

_fetchTemplate(  $template) : boolean

fetch plain template

The template content will be stored in the template configuration so it can be used by other methods.

Parameters

$template

Returns

boolean

_hasVariables()

_hasVariables(  $template) : boolean

check, whether a template contains variables

Parameters

$template

Returns

boolean

_getConditionValue()

_getConditionValue(  $template,   $value,   $isVar = true) 

fetch the value of a condition variable

_fetchVariables() has to be called before this method is being called.

Parameters

$template
$value
$isVar

_fetchVariables()

_fetchVariables(  $template) : boolean

fetch variables for a template

The variables will be stored in the template configuration so they can be used by other methods.

Parameters

$template

Returns

boolean

_handleUnusedVars()

_handleUnusedVars(  $template) 

handle all unused variables in a template

This is influenced by the 'unusedvars' attribute of the template

Parameters

$template

getParsedTemplate()

getParsedTemplate(  $name = null,   $applyFilters = false) : string

returns a parsed Template

If the template already has been parsed, it just returns the parsed template. If the template has not been loaded, it will be loaded.

Parameters

$name
$applyFilters

Returns

string —

Content of the parsed template

output_template_name_details()

output_template_name_details(  $output,   $files) 

Vince added to provide funky output

Parameters

$output
$files

displayParsedTemplate()

displayParsedTemplate(  $name = null) : boolean

display the compiled template

This is a replacement for patTemplate::displayParsedTemplate.

Parameters

$name

Returns

boolean —

true on success

parseIntoVar()

parseIntoVar(  $srcTmpl,   $destTmpl,   $var,   $append = false) : string

parse a template and push the result into a variable of any other template

If the template already has been parsed, it will just be pushed into the variable. If the template has not been loaded, it will be loaded.

Parameters

$srcTmpl
$destTmpl
$var
$append

Returns

string —

Content of the parsed template

clearTemplate()

clearTemplate(  $name,   $recursive = false) : boolean|\patError

clears a parsed Template

Parsed Content, variables and the loop attribute are cleared

If you will not be using this template anymore, then you should call freeTemplate()

Parameters

$name
$recursive

Returns

boolean|\patError

clearAllTemplates()

clearAllTemplates() 

clears all templates

freeTemplate()

freeTemplate(  $name,   $recursive = false) 

frees a template

All memory consumed by the template will be freed.

Parameters

$name
$recursive

freeAllTemplates()

freeAllTemplates() 

frees all templates

All memory consumed by the templates will be freed.

_getDependencies()

_getDependencies(  $template) : array

get _all_ dependencies of a template, regardless of the subtemplates

Parameters

$template

Returns

array —

list of all subtemplates

dump()

dump(  $restrict = null,   $dumper = 'Html') 

Displays useful information about all or named templates

This method breaks BC, as it now awaits an array instead of unlimited parameters.

Parameters

$restrict
$dumper

getIncludePath()

getIncludePath() : string

get the include path

Returns

string

applyInputFilters()

applyInputFilters(  $template) : string

apply input filters that have been set

This is being called by the readers.

Parameters

$template

Returns

string —

filtered templeta

placeholderExists()

placeholderExists(  $placeholder,   $tmpl,   $cached = true) 

checks, whether a placeholder exists in a template

Parameters

$placeholder
$tmpl
$cached

__toString()

__toString() : string

Convert the template to its string representation.

This method allows you to just echo the patTemplate object in order to display the template.

Requires PHP5

$tmpl = new patTemplate(); $tmpl->readTemplatesFromFile( 'myfile.tmpl' ); echo $tmpl;

Returns

string

compile()

compile(  $compileName = null) 

compile the currently loaded templates

Parameters

$compileName

compileTemplate()

compileTemplate(  $template) 

compile a template

Parameters

$template

_compileStandardTemplate()

_compileStandardTemplate(  $template) 

compile a standard template

Parameters

$template

_compileModuloTemplate()

_compileModuloTemplate(  $template) 

compile a modulo template

A modulo template will be compiled into a switch/case statement.

Parameters

$template

_compileSimpleConditionTemplate()

_compileSimpleConditionTemplate(  $template) 

compile a simpleCondition template

A simpleCondition template will be compiled into an 'if' statement.

Parameters

$template

_compileConditionTemplate()

_compileConditionTemplate(  $template) 

compile a condition template

A condition template will be compiled into an 'switch/case' statement.

Parameters

$template

_compileBuiltinConditions()

_compileBuiltinConditions(  $template) 

compile built-in conditions

This will create the neccessary PHP code for:

  • __first
  • __last

Parameters

$template

_templateToPHP()

_templateToPHP(  $content,   $template) : string

build PHP code from a template

This will replace the variables in a template with PHP Code.

Parameters

$content
$template

Returns

string —

PHP code

_addToCode()

_addToCode(  $line,   $indent,   $comment = null) : void

add a line to the compiled code

Parameters

$line
$indent
$comment

_getVar()

_getVar(  $template,   $varname) 

function, used by the compiler to get a value of a variable

Checks, whether the value is locally or globally set

Parameters

$template
$varname

_prepareCompiledTemplate()

_prepareCompiledTemplate(  $template) 

prepare a template for the compiler

Parameters

$template