PHP Classes

File: example3.php

Recommend this page to a friend!
  Classes of Jeff Williams   Easy Template   example3.php   Download  
File: example3.php
Role: Example script
Content type: text/plain
Description: Example 3 - New 2.5 Features!
Class: Easy Template
Template engine based on real HTML tag replacement
Author: By
Last change: Methods were separated into an extended class for performance (because they use the regex engine). The separation of code also makes the class more maintainable, faster, memory efficient and easier to use for smaller projects. Use "Template" for simple templating and "TemplateHTML" for ultra high performance HTML support where actual form elements can be used as template tags.
Date: 17 years ago
Size: 2,677 bytes
 

Contents

Class file image Download
<?php
/*
Easy Template Example
This example uses actual form controls as replace tags
*/

// Let's include the class file and create a new template object
include("templatehtml.class.php");
$t = new TemplateHTML("example3.html");


// Easy Template can create dropdowns, lists, and tables from arrays
$array = array(
    array(
"EmployeeID" => "1", "FirstName" => "Bob", "LastName" => "Smith", "Region" => "West"),
    array(
"EmployeeID" => "2", "FirstName" => "John", "LastName" => "Jones", "Region" => "North"),
    array(
"EmployeeID" => "3", "FirstName" => "Susan", "LastName" => "King", "Region" => null),
    array(
"EmployeeID" => "4", "FirstName" => "Jane", "LastName" => "Duncan","Region" => "East"),
    array(
"EmployeeID" => "5", "FirstName" => "Steven","LastName" => "Miller","Region" => null)
);

// We could instead grab an array from the Ultimate MySQL database class
// http://www.phpclasses.org/browse/package/3698.html
/*
include("mysql.class.php");
$db = new UltimateMySQL();
$db->Query("SELECT EmployeeID, FirstName, LastName, Region FROM employees");
$array = $db->RecordsArray(MYSQL_ASSOC);
*/

// Let's set up some example style information for a table
// NOTE: the style information in the opening TABLE and SELECT tags
// will not be changed so you can set style information in the template
$trHeader = 'style="color: rgb(204, 0, 0);"';
$tdHeader = 'style="font-weight: bold;"';
$tr = 'style="background-color: rgb(204, 255, 255);"';
$td = 'style="font-family: Helvetica,Arial,sans-serif;"';
$tr2 = 'style="background-color: rgb(255, 255, 204);"';
$td2 = 'style="font-style: italic;"';

// Replace actual elements and data in the HTML of the template
// NOTE: You could also inject data from another file, a string,
// another template, or even another live web page!

// Replace the actual table data in the page!
$t->replaceHtmlTableData("table3", $array, true, 1, $trHeader, $tdHeader, $tr, $td, $tr2, $td2);

// Let's replace the SELECT dropdown data with our array yet
// leave the SELECT intact with all style/attribute information!
// That means you can make your templates in ANY HTML editor
$t->replaceHtmlSelectData("MyList", $array, "FirstName", "EmployeeID", "5", false, 1);

// Replace ANY tag's data
$t->replaceHtmlElementData("caption", "MyCaption", "This caption has been replaced");

// Use div's as a template tag
$t->replaceHtmlElement("div", "MyDiv", "<h2>Test Template</h2>");

// We'll even replace this textbox with a checkbox
$t->replaceHtmlElementTag("input", "MyTextbox", '<input type="checkbox" />Now it\'s a checkbox');

// Show our new page
$t->showPage();
?>