DomTemplate


Dom_Template

Class Dom_Template

Dom_Template

public class Dom_Template

A PHP5 DOM Template Library

TODO: Add documentation on how to use this lib

NOTE: Var attribute values cannot begin with '__' because they are reserved for the template system's internal functions.


Field Summary
protected DOMElement

$body

The body tag of a html page

protected array

$choice

An array of choice DOMElement objects

protected array

$comments

Comment tags to be removed

protected DOMDocument

$document

protected DOMElement

$form

An array of form DOMElement objects

protected array

$formElement

An array of formElement DOMElement objects

protected DOMElement

$head

The head tag of a html page

protected array

$headers

Header elements to be added

protected array

$nextSibling

An array of DOMElement objects

protected Dom_Template

$parent

protected boolean

$parsed

Set to true if this template has been parsed

protected array

$repeat

An array of repeat DOMElement objects

protected DOMElement

$title

The head tag of a html page

protected array

$var

An array of var DOMElement objects

Constructor Summary

__construct(DOMDocument doc)

The constructor

Method Summary
boolean

appendDoc(string var, DOMDocument doc)

Append documents before the $var element

void

appendHeadElement(string elementName, array attributes, string value)

Appends an element to the widgets of the HTML head element.

boolean

appendTemplate(string var, Dom_Template template)

Append a template before the $var elementThis wil also grab any headers in the $template.

static string

cleanXml(string xml)

Get the xml and return the cleaned string A good place to clean any nasty html entities and other non valid XML/XHTML elements

string

getAttr(string var, string attr)

Retreive the text contained within an attribute of a node.

DOMElement

getBodyElement()

Return the body node.

DOMDocument

getDocument(boolean parse)

Return a parsed Dom document.

DOMElement

getElementById(string id)

Get a DOMElement from the document based on its unique ID ID attributes should be unique for XHTML documents, multiple names are ignored and only the first node found is returned.

Dom_Form

getForm(string id)

Return a form object from the document.

array

getHeaderList()

Return the current list of header nodes

protected DOMElement

getNextSibling(string var)

?? Used by the repeat region.

Dom_Repeat

getRepeat(string repeat)

Get a repeating region from a document.

DOMElement

getRepeatElement(string repeat)

Get a repeat element node from the document.

string

getText(string var)

Get the text inside a var node.

string

getTitleText()

Gets the page title text.

DOMElement

getVarElement(string var)

Get a var element node from the document.

private void

init(DOMElement node, string form)

A private method to initalise the template.

boolean

insertDoc(string var, DOMDocument doc)

Insert a doc into a var in this objects doc The DOMDocument will replace the var tag

static boolean

insertHTML(DOMElement element, string html, string encoding)

Insert some HTML formatted text into a dom node.

boolean

insertTemplate(string var, Dom_Template template)

Insert a template into a var in this objects doc The DOMDocument will replace the var tagThis will also grab any headers in the supplied template.

boolean

isParsed()

Get the parsed state of the template.

void

keyExists(string property, string key)

Check if a repeat,choice,var,form (template property) Exists.

static Dom_Template

load(string xmlFile)

Make a template from an xml file

static Dom_Template

loadXml(string xmlStr)

Make a template from an xml string

static void

objectToString(mixed obj)

Return a string from an object.

protected void

preInit(DOMElement node, string form)

This function is used when subclassing the template implement this function to capture nodes.

protected void

removeChildren(DOMNode node)

Removes all children from a node.

void

replaceAttr(string var, string attr, string value)

Replace an attribute value.

boolean

replaceHTML(string var, string html, string encoding)

Insert some HTML formatted text into a var node.

void

replaceText(string var, string value)

Replace the text of one or more var nodes

void

setChoice(string choice)

Set a choice node to become visible in a document.

void

setHeaderList(mixed arr, array )

Set the current list of header nodes

protected void

setNextSibling(string var, DOMElement nextSibling)

??? Used by the repeat region.

void

setTitleText(mixed value, string The)

Sets the document title text if available.

string

toString(string type)

Recive the document in the format of 'xml' or 'html'.

void

unsetChoice(string choice)

Set a choice node to become invisible in a document.

Field Detail

body

protected DOMElement $body = null

The body tag of a html page


choice

protected array $choice = array()

An array of choice DOMElement objects


comments

protected array $comments = array()

Comment tags to be removed


document

protected DOMDocument $document = null


form

protected DOMElement $form = array()

An array of form DOMElement objects


formElement

protected array $formElement = array()

An array of formElement DOMElement objects


protected DOMElement $head = null

The head tag of a html page


headers

protected array $headers = array()

Header elements to be added


nextSibling

protected array $nextSibling = array()

An array of DOMElement objects


parent

protected Dom_Template $parent = null


parsed

protected boolean $parsed = false

Set to true if this template has been parsed


repeat

protected array $repeat = array()

An array of repeat DOMElement objects


title

protected DOMElement $title = null

The head tag of a html page


var

protected array $var = array()

An array of var DOMElement objects


Constructor Detail

__construct

public __construct(DOMDocument doc)

The constructor


Method Detail

appendDoc

public boolean appendDoc(string var, DOMDocument doc)

Append documents before the $var element

Returns:
Returns true on success

appendHeadElement

public void appendHeadElement(string elementName, array attributes, string value)

Appends an element to the widgets of the HTML head element.

In the form of: <$elementName $attributes[$key]="$attributes[$key].$value">$value

NOTE: Only allows unique headers. An md5 hash is refrenced from all input parameters. Any duplicate headers are discarded.

Parameters:
attributes - An associative array of (attr, value) pairs.
value - The element value.

appendTemplate

public boolean appendTemplate(string var, Dom_Template template)

Append a template before the $var element

This wil also grab any headers in the $template.

Returns:
Returns true on success

cleanXml

public static string cleanXml(string xml)

Get the xml and return the cleaned string A good place to clean any nasty html entities and other non valid XML/XHTML elements


getAttr

public string getAttr(string var, string attr)

Retreive the text contained within an attribute of a node.


getBodyElement

public DOMElement getBodyElement()

Return the body node.


getDocument

public DOMDocument getDocument(boolean parse)

Return a parsed Dom document. After using this call you can no longer use the template render functions as no changes will be made to the template unless you use DOM functions

Parameters:
parse - Set to false to avoid parsing and return DOMDocument in its current state

getElementById

public DOMElement getElementById(string id)

Get a DOMElement from the document based on its unique ID ID attributes should be unique for XHTML documents, multiple names are ignored and only the first node found is returned.

Returns:
Returns null if not found

getForm

public Dom_Form getForm(string id)

Return a form object from the document.


getHeaderList

public array getHeaderList()

Return the current list of header nodes


getNextSibling

protected DOMElement getNextSibling(string var)

?? Used by the repeat region.

Returns:
next sibling

getRepeat

public Dom_Repeat getRepeat(string repeat)

Get a repeating region from a document.


getRepeatElement

public DOMElement getRepeatElement(string repeat)

Get a repeat element node from the document.


getText

public string getText(string var)

Get the text inside a var node.


getTitleText

public string getTitleText()

Gets the page title text.

Returns:
The title.

getVarElement

public DOMElement getVarElement(string var)

Get a var element node from the document.


init

private void init(DOMElement node, string form)

A private method to initalise the template.


insertDoc

public boolean insertDoc(string var, DOMDocument doc)

Insert a doc into a var in this objects doc The DOMDocument will replace the var tag

Returns:
Returns true on success

insertHTML

public static boolean insertHTML(DOMElement element, string html, string encoding)

Insert some HTML formatted text into a dom node.

Returns:
Returns true on success

insertTemplate

public boolean insertTemplate(string var, Dom_Template template)

Insert a template into a var in this objects doc The DOMDocument will replace the var tag

This will also grab any headers in the supplied template.

Returns:
Returns true on success

isParsed

public boolean isParsed()

Get the parsed state of the template. If true then no more changes can be made to the template


keyExists

public void keyExists(string property, string key)

Check if a repeat,choice,var,form (template property) Exists.


load

public static Dom_Template load(string xmlFile)

Make a template from an xml file

Throws:
RuntimeException - If there is an error with the XML template

loadXml

public static Dom_Template loadXml(string xmlStr)

Make a template from an xml string

Throws:
RuntimeException - If there is an error with the XML template

objectToString

public static void objectToString(mixed obj)

Return a string from an object.


preInit

protected void preInit(DOMElement node, string form)

This function is used when subclassing the template implement this function to capture nodes.

Parameters:
form - The form name if inside a form.

removeChildren

protected void removeChildren(DOMNode node)

Removes all children from a node.


replaceAttr

public void replaceAttr(string var, string attr, string value)

Replace an attribute value.


replaceHTML

public boolean replaceHTML(string var, string html, string encoding)

Insert some HTML formatted text into a var node.

Returns:
Returns true on success

replaceText

public void replaceText(string var, string value)

Replace the text of one or more var nodes

Parameters:
var - The var's name.
value - The vars value inside the tags.

setChoice

public void setChoice(string choice)

Set a choice node to become visible in a document.

Parameters:
choice - The name of the choice

setHeaderList

public void setHeaderList(mixed arr, array )

Set the current list of header nodes


setNextSibling

protected void setNextSibling(string var, DOMElement nextSibling)

??? Used by the repeat region.


setTitleText

public void setTitleText(mixed value, string The)

Sets the document title text if available.

Parameters:
The - title.

toString

public string toString(string type)

Recive the document in the format of 'xml' or 'html'. NOTE: This method does not parse the template document.

Parameters:
type - 'xml' or 'html'

unsetChoice

public void unsetChoice(string choice)

Set a choice node to become invisible in a document.

Parameters:
choice - The name of the choice

DomTemplate