pclib  3.0.0
Lightweight PHP framework
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Tpl Class Reference

Template engine. More...

Inheritance diagram for Tpl:
BaseObject Form Grid Layout

Public Member Functions

 __construct ($path='', $sessName='')
 Load and parse template file. More...
 
 init ()
 Initialization - must be called after load()
 
 load ($path)
 Load template file. More...
 
 loadString ($s)
 Load string $s as template. More...
 
 out ($block=null)
 Display template populated with content. More...
 
 html ($block=null)
 Return html output of the template populated with content. More...
 
 enable ()
 Enable (show) or disable (hide) tag or block $name. More...
 
 disable ()
 Disable (hide) tag or block $name. More...
 
 setAttr ($id, $value, $value2=null)
 Set attribute globally for template elements. More...
 
 getBlock ($block)
 Return values of block $block. More...
 
 getValue ($id)
 Return value of element $id. More...
 
 loadSession ()
 Load object from session.
 
 saveSession ()
 Save object to session.
 
 deleteSession ()
 Remove object from session.
 
 create ($tableName, $templatePath='')
 Use default template for displaying database table content.
 
 print_Element ($id, $sub, $value)
 This function is called for each template tag when it is printed. More...
 
 print_Number ($id, $sub, $value)
 Print numeric $value. More...
 
 print_String ($id, $sub, $s)
 Print string $value. More...
 
 print_Bind ($id, $sub, $value)
 Bind $value to LABEL coming from datasource and print LABEL. More...
 
 print_Link ($id, $sub, $value)
 Create html link. More...
 
 print_Env ($id, $sub, $value)
 Print value from url (from _GET array). More...
 
 print_Class ($id, $sub, $value)
 Print all fields into template. More...
 
 print_Action ($id, $sub, $value)
 Call controller's method and include result into template. More...
 
 print_Block ($block)
 Print template block. More...
 
 addTag ($line)
 Add new element. More...
 
 addType ($name, $fn)
 Add new type of element handled by callback function $fn. More...
 
 setFields ($fields)
 Show only this $fields in template (using tag {grid.fields} or {form.fields}). More...
 
 htmlTag ($name, $attr=[], $content=null)
 Return html tag $name with attributes. More...
 
- Public Member Functions inherited from BaseObject
 loadDefaults ($className=null)
 Load default parameters of class $className into object instance.
 
 setProperties (array $defaults)
 Set public properties of object from the array. More...
 
 toArray ()
 Convert object to array. More...
 

Data Fields

 $name
 Name of the template.
 
 $elements = array()
 Array of elements loaded from <?elements ? > section.
 
 $values = array()
 Array of template values.
 
 $db
 var Db
 
 $router
 var Router
 
 $escapeHtmlFunction
 Function for escaping html in template values.
 
- Data Fields inherited from BaseObject
 $serviceLocator
 var function() Return service object when requested with service().
 

Protected Member Functions

 getAttr ($id, $attr)
 Return attribute of element $id. More...
 
 getVariable ($id)
 Get template variable tvar...
 
 getRowNo ()
 Return row number of the current block.
 
 createFromTable ($tableName, $templatePath)
 Use default template for displaying database table content.
 
 compute ($id)
 Return computed value of element $id.
 
 trPrintElement ($elem)
 Print element in table layout. More...
 
 getPopup ($id, $attr, $url)
 Generate javascript code for popup window. More...
 
 getUrl ($elem)
 Return url for the element (button, link) with completed parameters.
 
 formatDate ($dtstr, $fmt='')
 DATABASE DATE => HUMAN DATE (in date() format) More...
 
 formatStr ($s, $fmt)
 Format string $s according format $fmt.
 
 getItems ($id)
 Load lookup table for elements such as bind, select, check or radio. More...
 
- Protected Member Functions inherited from BaseObject
 service ($service, $default=null)
 Try acquire $service and load it into property $this->$service. More...
 

Protected Attributes

 $sessName
 Name of the session variable where template values are stored. More...
 
 $app
 var App Link to application object.
 
 $config = null
 Link to array of configuration parameters.
 
 $document
 Document array - It contains parsed template.
 
 $parser
 var TplParser
 
 $fields = []
 List of id's to be printed in {grid.fields} tag.
 
 $types = []
 Additional element type handlers.
 

Additional Inherited Members

- Static Public Member Functions inherited from BaseObject
static defaults ()
 Set or retireve default parameters of the object. More...
 

Detailed Description

Template engine.

Load template, populate it with values and display it. Template is usually html file with template tags {TAGNAME}. Features:

See Template tags implemented by class Tpl for description of implemented tags.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $path = '',
  $sessName = '' 
)

Load and parse template file.

Parameters
string$pathFilename of template file
string$sessNameWhen set, object is stored in session as $sessName

Member Function Documentation

◆ addTag()

addTag (   $line)

Add new element.

Parameters
string$lineElement definition in template file syntax - e.g. "string Title noprint"

◆ addType()

addType (   $name,
  $fn 
)

Add new type of element handled by callback function $fn.

Parameters
string$nameName of new type
callable$fnfunction($obj, $id, $sub, $value)

◆ disable()

disable ( )

Disable (hide) tag or block $name.

Parameters
array|listof tag names

◆ enable()

enable ( )

Enable (show) or disable (hide) tag or block $name.

$tpl->enable('tag'); $tpl->enable('tag1','tag2'); $tpl->enable('tag', false);

Parameters
array|listof tag names
bool$valEnable? true|false

◆ formatDate()

formatDate (   $dtstr,
  $fmt = '' 
)
protected

DATABASE DATE => HUMAN DATE (in date() format)

See also
date()

◆ getAttr()

getAttr (   $id,
  $attr 
)
protected

Return attribute of element $id.

Perform cascading search of attribute value - if attr is not found in element it will look in element's block and template header.

Parameters
string$idElement Id
Returns
string $val Element value

◆ getBlock()

getBlock (   $block)

Return values of block $block.

Parameters
string$blockId of template block
Returns
array $values

◆ getItems()

getItems (   $id)
protected

Load lookup table for elements such as bind, select, check or radio.

Element must contains exactly one of following attributes: list, query, lookup. See Common attributes of template tags. You can set array of items directly: $t->_element->items = $items;

Parameters
int$ididentificator of element
Returns
array $items

◆ getPopup()

getPopup (   $id,
  $attr,
  $url 
)
protected

Generate javascript code for popup window.

Parameters
string$attrpopup attribute of element (Example: popup "600x400+100+100")
string$urlurl of page to open
Returns
string $js javascript window code

◆ getValue()

getValue (   $id)

Return value of element $id.

Parameters
string$idElement Id
Returns
string $val Element value

Reimplemented in Form.

◆ html()

html (   $block = null)

Return html output of the template populated with content.

Parameters
string$blockIf set, only html of the block $block will be returned.

◆ htmlTag()

htmlTag (   $name,
  $attr = [],
  $content = null 
)

Return html tag $name with attributes.

$attr: "<$name $attr>$content</$name>".

◆ load()

load (   $path)

Load template file.

Parameters
string$pathfilename of template file
See also
loadString()

◆ loadString()

loadString (   $s)

Load string $s as template.

Useful, if you need read template from another source than filesystem.

Parameters
string$sstring containing template source

◆ out()

out (   $block = null)

Display template populated with content.

Replace all tags in template with $values, perform any formatting and callback functions for the $elements and write output.

Parameters
string$blockIf set, only block $block will be printed.

◆ print_Action()

print_Action (   $id,
  $sub,
  $value 
)

Call controller's method and include result into template.

Example: action comments route "comments/list/id:{id}" will call method CommentsController::listAction($id)

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array

◆ print_Bind()

print_Bind (   $id,
  $sub,
  $value 
)

Bind $value to LABEL coming from datasource and print LABEL.

Datasource can be specified using attributes list, query or lookup. See Common attributes of template tags for details.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array

◆ print_Block()

print_Block (   $block)

Print template block.

Block is template section marked with:

{block name}
html code...
{/block}
html($block=null)
Return html output of the template populated with content.
Definition: Tpl.php:218

Block is treated like normal template element. It's posible hide whole block (noprint) or repeat block n-times (repeat "n"). Warning! It's sharing namespace with another elements, so his name must be unique.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array

◆ print_Class()

print_Class (   $id,
  $sub,
  $value 
)

Print all fields into template.

It uses simple table layout. Type {tpl.fields} into template.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array

Reimplemented in Form.

◆ print_Element()

print_Element (   $id,
  $sub,
  $value 
)

This function is called for each template tag when it is printed.

You can redefine this function in descendant and add your own element types.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array

Reimplemented in Layout, Grid, and Form.

◆ print_Env()

print_Env (   $id,
  $sub,
  $value 
)

Print value from url (from _GET array).

Use in template like this: {GET.variable}.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array

◆ print_Link()

print_Link (   $id,
  $sub,
  $value 
)

Create html link.

You can use 'popup' for popup window.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array

◆ print_Number()

print_Number (   $id,
  $sub,
  $value 
)

Print numeric $value.

Perform numeric formatting.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array

◆ print_String()

print_String (   $id,
  $sub,
  $s 
)

Print string $value.

It supports string formatting, string crop, date formating, tooltip etc. See Template tags implemented by class Tpl for details.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array

◆ setAttr()

setAttr (   $id,
  $value,
  $value2 = null 
)

Set attribute globally for template elements.

Parameters
string$idElement/Attribute id
string$valueAttribute id/Attribute value
string$value2Attribute value

◆ setFields()

setFields (   $fields)

Show only this $fields in template (using tag {grid.fields} or {form.fields}).

Parameters
array$fieldslist of field (element) ids

◆ trPrintElement()

trPrintElement (   $elem)
protected

Print element in table layout.

See also
print_class();

Reimplemented in Grid, and Form.

Field Documentation

◆ $sessName

$sessName
protected

Name of the session variable where template values are stored.

See also
loadSession(), saveSession()

The documentation for this class was generated from the following file: