Class Template

Description

Easy Template class that replaces tags in html file like <!--TagName --> with any value
For additional HTML tag support and functionality, use the TemplateHTML class instead.

Mar 13, 2007 - Initial Release
Mar 27, 2007 - Separated HTML Support into Extended Class

  • author: Jeff L. Williams
  • version: 2.5

Usage:

include("template.class.php");

$template = new Template("filename.html");

Direct descendents
Class Description
TemplateHTML Easy Template optional HTML class that replaces tags like <!--TagName --> and actual HTML tags with any value These methods were placed into an extended class for performance because they require the regex engine The separation of code also makes the class more maintainable and easier to use for smaller projects.
Method Summary
Template __construct ([string $templateFile = ''])
boolean createHTMLSelectData (array $array, string/integer $displayColumn, [string $valueColumn = ''], [string $selectedValue = ''], [boolean $caseSensitive = false], integer $indent)
boolean createHTMLTableData (array $array, [boolean $showAssocArrayColumnTitles = true], integer $indent, [string $optionalRowHeadingTags = ''], [string $optionalCellHeadingTags = ''], [string $optionalRowTags = ''], [string $optionalCellTags = ''], [string $optionalRowTags2 = ''], [string $optionalCellTags2 = ''])
string getFormValue (string $name)
string getHTMLChecked (string/boolean $isChecked)
string getPage ()
TRUE hasTag (string $tagName)
void loadPage (string $templateFile)
void removeAllTags ()
void removeTag (string $tagName)
void replaceTag (string $tagName, [string $content = ''], [boolean $fixHTML = false], [boolean $stripSlashes = false], [boolean $keepTag = false])
void replaceTagFromFile (string $tagName, string $fileName, [boolean $fixHTML = false], [boolean $stripSlashes = false], [boolean $keepTag = false])
void replaceTagFromWeb (string $tagName, string $url, [boolean $fixHTML = false], [boolean $stripSlashes = false], [boolean $keepTag = false])
void setPage (string $html)
void showPage ()
Methods
Constructor __construct (line 26)

Constructor method

  • access: public
Template __construct ([string $templateFile = ''])
  • string $templateFile: the filename for the template
createHTMLSelectData (line 283)

Creates OPTION lists for a SELECT from an array

  • return: Either the HTML string or FALSE for failure
  • static: use Template::createHTMLSelectData()
  • access: public
boolean createHTMLSelectData (array $array, string/integer $displayColumn, [string $valueColumn = ''], [string $selectedValue = ''], [boolean $caseSensitive = false], integer $indent)
  • array $array: An array containing numbered or associated arrays (rows)
  • string/integer $displayColumn: The column in the arrays that contains the displayed text
  • string $valueColumn: The column in the arrays that contains the form return values
  • string $selectedValue: The default selected item in the list
  • boolean $caseSensitive: TRUE if the selected item is chosen using case sensitivity
  • integer $indent: The number of tabs to indent
createHTMLTableData (line 335)

Creates table data from an array (not including the open and ending <TABLE> tags)

  • return: Either the HTML string or FALSE for failure
  • static: use Template::createHTMLTableData()
  • access: public
boolean createHTMLTableData (array $array, [boolean $showAssocArrayColumnTitles = true], integer $indent, [string $optionalRowHeadingTags = ''], [string $optionalCellHeadingTags = ''], [string $optionalRowTags = ''], [string $optionalCellTags = ''], [string $optionalRowTags2 = ''], [string $optionalCellTags2 = ''])
  • array $array: An array containing numbered or associated arrays (rows)
  • boolean $showAssocArrayColumnTitles: TRUE if you would like to display heading titles. NOTE: This only works with associated arrays that contain key values
  • integer $indent: The number of tabs to indent
  • string $optionalRowHeadingTags
  • string $optionalCellHeadingTags
  • string $optionalRowTags
  • string $optionalCellTags
  • string $optionalRowTags2
  • string $optionalCellTags2
getFormValue (line 40)

Get a POST or GET value by a form element name

  • static: use Template::getFormValue()
string getFormValue (string $name)
  • string $name: The name of the form element sent by POST or GET
getHTMLChecked (line 448)

Returns the checked tag for checkboxes and radio buttons based on a string (i.e. value from a post) or boolean.

This is helpful when reposting forms that fail validation.

  • return: Returns checked="checked" if TRUE, blank if FALSE
  • static: use Template::getHTMLChecked()
  • access: public
string getHTMLChecked (string/boolean $isChecked)
  • string/boolean $isChecked: true, "on", "selected", "checked", "yes", "y", "true", or "t" (not case-sensitive)
getPage (line 60)

Returns the template HTML

  • return: Processed HTML
  • access: public
string getPage ()
hasTag (line 71)

Determines if a specified comment tag exists

  • return: if exists, FALSE if not
  • access: public
TRUE hasTag (string $tagName)
  • string $tagName: The name of the comment style tag
loadPage (line 93)

Loads a template HTML from a file

  • access: public
void loadPage (string $templateFile)
  • string $templateFile: the filename for the template
removeAllTags (line 110)

Removes all tags from the document

WARNING: There is no way to differentiate between the tags and HTML comments since the tags actually are HTML comments, so all comments with <!-- --> tags will be removed using this method!

  • access: public
void removeAllTags ()
removeTag (line 121)

Removes a tag from the page

  • access: public
void removeTag (string $tagName)
  • string $tagName: The name of the tag to remove
replaceTag (line 136)

Replaces a tag embedded in the template with a string

  • access: public
void replaceTag (string $tagName, [string $content = ''], [boolean $fixHTML = false], [boolean $stripSlashes = false], [boolean $keepTag = false])
  • string $tagName: The name of the tag <!--tagName -->
  • string $content: The content to insert
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
  • boolean $keepTag: If true, the tag will stay in the template
replaceTagFromFile (line 166)

Replaces a tag embedded in the template with data from a file

  • access: public
void replaceTagFromFile (string $tagName, string $fileName, [boolean $fixHTML = false], [boolean $stripSlashes = false], [boolean $keepTag = false])
  • string $tagName: The name of the tag <!--tagName -->
  • string $fileName: The name of the file containing the data
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
  • boolean $keepTag: If true, the tag will stay in the template
replaceTagFromWeb (line 202)

Replaces a tag embedded in the template with data from web page

  • access: public
void replaceTagFromWeb (string $tagName, string $url, [boolean $fixHTML = false], [boolean $stripSlashes = false], [boolean $keepTag = false])
  • string $tagName: The name of the tag <!--tagName -->
  • string $url: The URL containing the web data
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
  • boolean $keepTag: If true, the tag will stay in the template
setPage (line 234)

Sets the template HTML

  • access: public
void setPage (string $html)
  • string $html: Template HTML
showPage (line 243)

Shows the template HTML

  • access: public
void showPage ()

Class TemplateHTML

Description

Easy Template optional HTML class that replaces tags like <!--TagName --> and actual HTML tags with any value
These methods were placed into an extended class for performance because they require the regex engine
The separation of code also makes the class more maintainable and easier to use for smaller projects.

Mar 13, 2007 - Initial Release
Mar 22, 2007 - Special thanks to Todd Morrow for regex code
Mar 27, 2007 - Separated HTML Support in Extended Class

  • author: Jeff L. Williams
  • version: 2.5

Usage:

include("templatehtml.class.php");

$template = new TemplateHTML("filename.html");

Template
   |
   --TemplateHTML
Method Summary
string getHTMLElement (string $elementTag, string $elementName)
string getHTMLElementFromFile (string $elementTag, string $elementName, string $url)
string getHTMLElementFromString (string $elementTag, string $elementName, string $html)
string getHTMLElementFromURL (string $elementTag, string $elementName, string $url)
string getHTMLElementTag (string $elementTag, string $elementName)
string getHTMLElementTagFromFile (string $elementTag, string $elementName, string $url)
string getHTMLElementTagFromString (string $elementTag, string $elementName, string $html)
string getHTMLElementTagFromURL (string $elementTag, string $elementName, string $url)
TRUE hasHTMLTag (string $elementTag, string $elementName)
void removeHTMLElement (string $elementTag, string $elementName)
void removeHTMLElementData (string $elementTag, string $elementName)
void removeHTMLElementTag (string $elementTag, string $elementName)
void replaceHTMLElement (string $elementTag, string $elementName, string $newText, [boolean $fixHTML = false], [boolean $stripSlashes = false])
boolean replaceHTMLElementData (string $elementTag, string $elementName, string $newText, [boolean $fixHTML = false], [boolean $stripSlashes = false])
void replaceHTMLElementDataFromFile (string $elementTag, string $elementName, string $fileName, [boolean $fixHTML = false], [boolean $stripSlashes = false])
void replaceHTMLElementDataFromWeb (string $elementTag, string $elementName, string $url, [boolean $fixHTML = false], [boolean $stripSlashes = false])
void replaceHTMLElementFromFile (string $elementTag, string $elementName, string $fileName, [boolean $fixHTML = false], [boolean $stripSlashes = false])
void replaceHTMLElementFromWeb (string $elementTag, string $elementName, string $url, [boolean $fixHTML = false], [boolean $stripSlashes = false])
void replaceHTMLElementTag (string $elementTag, string $elementName, string $newText, [boolean $fixHTML = false], [boolean $stripSlashes = false])
void replaceHTMLElementTagFromFile (string $elementTag, string $elementName, string $fileName, [boolean $fixHTML = false], [boolean $stripSlashes = false])
void replaceHTMLElementTagFromWeb (string $elementTag, string $elementName, string $url, [boolean $fixHTML = false], [boolean $stripSlashes = false])
boolean replaceHTMLSelectData (string $htmlSelectName, array $array, string/integer $displayColumn, [string $valueColumn = ''], [string $selectedValue = ''], [boolean $caseSensitive = false], integer $indent)
boolean replaceHTMLTableData (string $htmlTableName, array $array, [boolean $showAssocArrayColumnTitles = true], integer $indent, [string $optionalRowHeadingTags = ''], [string $optionalCellHeadingTags = ''], [string $optionalRowTags = ''], [string $optionalCellTags = ''], [string $optionalRowTags2 = ''], [string $optionalCellTags2 = ''])
Methods
getHTMLElement (line 25)

Returns a complete HTML form element from the template

  • return: HTML form element with opening and closing tags included
  • access: public
string getHTMLElement (string $elementTag, string $elementName)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
getHTMLElementFromFile (line 37)

Returns a complete HTML form element from an HTML file

  • return: HTML form element with opening and closing tags included
  • access: public
string getHTMLElementFromFile (string $elementTag, string $elementName, string $url)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $url: The URL location of the page to extract from
getHTMLElementFromString (line 54)

Returns a complete HTML form element from a string containing HTML

  • return: HTML form element with opening and closing tags included
  • access: public
string getHTMLElementFromString (string $elementTag, string $elementName, string $html)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $html: A string containing HTML
getHTMLElementFromURL (line 81)

Returns a complete HTML form element from a live URL

  • return: HTML form element with opening and closing tags included
  • access: public
string getHTMLElementFromURL (string $elementTag, string $elementName, string $url)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $url: The URL location of the page to extract from
getHTMLElementTag (line 98)

Returns an HTML form element tag from the template

  • return: HTML form element tag
  • access: public
string getHTMLElementTag (string $elementTag, string $elementName)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
getHTMLElementTagFromFile (line 110)

Returns an HTML form element tag from an HTML file

  • return: HTML form element tag
  • access: public
string getHTMLElementTagFromFile (string $elementTag, string $elementName, string $url)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $url: The URL location of the page to extract from
getHTMLElementTagFromString (line 127)

Returns an HTML form element tag from a string containing HTML

  • return: HTML form element tag
  • access: public
string getHTMLElementTagFromString (string $elementTag, string $elementName, string $html)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $html: A string containing HTML
getHTMLElementTagFromURL (line 154)

Returns an HTML form element tag from a live URL

  • return: HTML form element tag
  • access: public
string getHTMLElementTagFromURL (string $elementTag, string $elementName, string $url)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $url: The URL location of the page to extract from
hasHTMLTag (line 171)

Checks to see if an HTML tag element exists

  • return: if exists, FALSE if not
  • access: public
TRUE hasHTMLTag (string $elementTag, string $elementName)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
removeAllComments (line 193)

Removes all comments from the document

  • access: public
void removeAllComments ()
removeHTMLElement (line 205)

Removes an HTML form element

  • access: public
void removeHTMLElement (string $elementTag, string $elementName)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
removeHTMLElementData (line 215)

Removes data between an HTML from element opening and closing tag

  • access: public
void removeHTMLElementData (string $elementTag, string $elementName)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
removeHTMLElementTag (line 225)

Removes an HTML form element tag

  • access: public
void removeHTMLElementTag (string $elementTag, string $elementName)
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
replaceHTMLElement (line 238)

Replaces a template HTML form element with a string

  • access: public
void replaceHTMLElement (string $elementTag, string $elementName, string $newText, [boolean $fixHTML = false], [boolean $stripSlashes = false])
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $newText: The replacement text
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
replaceHTMLElementData (line 262)

Replaces template data between an opening and closing HTML form tag with a string

  • return: TRUE if success, FALSE if failure
  • access: public
boolean replaceHTMLElementData (string $elementTag, string $elementName, string $newText, [boolean $fixHTML = false], [boolean $stripSlashes = false])
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $newText: The replacement text
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
replaceHTMLElementDataFromFile (line 291)

Replaces template data between an opening and closing HTML form tag with data from a file

  • access: public
void replaceHTMLElementDataFromFile (string $elementTag, string $elementName, string $fileName, [boolean $fixHTML = false], [boolean $stripSlashes = false])
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $fileName: The name of the file containing the data
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
replaceHTMLElementDataFromWeb (line 309)

Replaces template data between an opening and closing HTML form tag with data from a live URL

  • access: public
void replaceHTMLElementDataFromWeb (string $elementTag, string $elementName, string $url, [boolean $fixHTML = false], [boolean $stripSlashes = false])
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $url: The URL location of the page to extract from
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
replaceHTMLElementFromFile (line 328)

Replaces a template HTML form element with data from a file

  • access: public
void replaceHTMLElementFromFile (string $elementTag, string $elementName, string $fileName, [boolean $fixHTML = false], [boolean $stripSlashes = false])
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $fileName: The name of the file containing the data
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
replaceHTMLElementFromWeb (line 346)

Replaces a template HTML form element with data from a live URL

  • access: public
void replaceHTMLElementFromWeb (string $elementTag, string $elementName, string $url, [boolean $fixHTML = false], [boolean $stripSlashes = false])
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $url: The URL location of the page to extract from
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
replaceHTMLElementTag (line 365)

Replaces a template HTML form element opening tag with a string

  • access: public
void replaceHTMLElementTag (string $elementTag, string $elementName, string $newText, [boolean $fixHTML = false], [boolean $stripSlashes = false])
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $newText: The replacement text
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
replaceHTMLElementTagFromFile (line 390)

Replaces a template HTML form element opening tag with data from a file

  • access: public
void replaceHTMLElementTagFromFile (string $elementTag, string $elementName, string $fileName, [boolean $fixHTML = false], [boolean $stripSlashes = false])
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $fileName: The name of the file containing the data
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
replaceHTMLElementTagFromWeb (line 408)

Replaces a template HTML form element opening tag with data from a live URL

  • access: public
void replaceHTMLElementTagFromWeb (string $elementTag, string $elementName, string $url, [boolean $fixHTML = false], [boolean $stripSlashes = false])
  • string $elementTag: The type of element (i.e. table, input or div)
  • string $elementName: The name of the element (name= attribute)
  • string $url: The URL location of the page to extract from
  • boolean $fixHTML: Determines if the content should be formatted
  • boolean $stripSlashes: Encodes the contect
replaceHTMLSelectData (line 430)

Replaces the list data in an existing HTML SELECT within the template

  • return: TRUE if success, FALSE if failure
  • access: public
boolean replaceHTMLSelectData (string $htmlSelectName, array $array, string/integer $displayColumn, [string $valueColumn = ''], [string $selectedValue = ''], [boolean $caseSensitive = false], integer $indent)
  • string $htmlSelectName: The name of the SELECT element
  • array $array: An array containing numbered or associated arrays (rows)
  • string/integer $displayColumn: The column in the arrays that contains the displayed text
  • string $valueColumn: The column in the arrays that contains the form return values
  • string $selectedValue: The default selected item in the list
  • boolean $caseSensitive: TRUE if the selected item is chosen using case sensitivity
  • integer $indent: The number of tabs to indent
replaceHTMLTableData (line 454)

Replaces the table data in an existing HTML table within the template

  • return: TRUE if success, FALSE if failure
  • access: public
boolean replaceHTMLTableData (string $htmlTableName, array $array, [boolean $showAssocArrayColumnTitles = true], integer $indent, [string $optionalRowHeadingTags = ''], [string $optionalCellHeadingTags = ''], [string $optionalRowTags = ''], [string $optionalCellTags = ''], [string $optionalRowTags2 = ''], [string $optionalCellTags2 = ''])
  • string $htmlTableName: The name of the table (i.e. name=)
  • array $array: An array containing numbered or associated arrays (rows)
  • boolean $showAssocArrayColumnTitles: TRUE if you would like to display heading titles. NOTE: This only works with associated arrays that contain key values
  • integer $indent: The number of tabs to indent
  • string $optionalRowHeadingTags: Tag attributes for row headings (i.e. style or class)
  • string $optionalCellHeadingTags: Tag attributes for cells (i.e. style or class)
  • string $optionalRowTags: Tag attributes for rows (i.e. style or class)
  • string $optionalCellTags: Tag attributes for cells (i.e. style or class)
  • string $optionalRowTags2: Tag attributes for alternating rows (i.e. style or class)
  • string $optionalCellTags2: Tag attributes for alternating row cells (i.e. style or class)

Inherited Methods

Inherited From Template

Template::__construct()
Template::createHTMLSelectData()
Template::createHTMLTableData()
Template::getFormValue()
Template::getHTMLChecked()
Template::getPage()
Template::hasTag()
Template::loadPage()
Template::removeAllTags()
Template::removeTag()
Template::replaceTag()
Template::replaceTagFromFile()
Template::replaceTagFromWeb()
Template::setPage()
Template::showPage()