You got me interested in this anyway.
$page = new DTStrict4();
$html = new Element('html');
$page->addSibling($html);
$head = new Element('head');
$body = new Element('body');
$html->addChild($head)->addChild($body);
$head->addChild(new HTitle('Project-Pad Project Form'))->addChild(new CSS('/include/css/base.css'));
$form = new HForm($_SERVER['PHP_SELF'],'POST','project-form');
$body->addChild($form);
$projectIdInput = new FormInput();
$projectIdInput->addAttribute('name','project[id]')->addAttribute('type','hidden');
$form->addChild($projectIdInput);
$fieldsFieldset = new Fieldset(null,'fields');
$controlsFieldset = new Fieldset(null,'controls');
$form->addChild($fieldsFieldset)->addChild($controlsFieldset);
$ol1 = new OL('project-data');
$ol2 = new OL('form-controls');
$fieldsFieldset->addChild(new Legend('Project Form'));
$fieldsFieldset->addChild($ol1);
$controlsFieldset->addChild($ol2);
$titleLi = new LI();
$categoryLi = new LI();
$rangeLi = new LI();
$messageLi = new LI();
$ol1->addChild($titleLi)->addChild($categoryLi)->addChild($rangeLi)->addChild($messageLi);
$titleInput = new FormInput();
$titleInput->addAttribute('name','project[title]')->addAttribute('type','text')->addAttribute('maxlength','40')->addAttribute('id','project-title');
$titleLi->addChild(new Label('Project Title:','project-title'))->addChild($titleInput);
$categorySelect = new FormSelect();
$categorySelect->addAttribute('id','project-category')->addAttribute('name','project[category]');
$categoryLi->addChild(new Label('Project Category:','project-category'))->addChild($categorySelect);
$rangeSelect = new FormSelect();
$rangeSelect->addAttribute('id','project-range')->addAttribute('name','project[range]');
$rangeLi->addChild(new Label('Project Range:','project-range'))->addChild($rangeSelect);
$messageTextarea = new Textarea();
$messageTextarea->addAttribute('id','project-message')->addAttribute('name','project[message]')->addAttribute('cols','60')->addAttribute('rows','15');
$messageLi->addChild(new Label('Project Description:','project-message'))->addChild($messageTextarea);
$resetButton = new FormInput();
$resetButton->addAttribute('type','reset')->addAttribute('name','reset')->addAttribute('value','Reset Form');
$submitButton = new FormInput();
$submitButton->addAttribute('type','submit')->addAttribute('name','submit')->addAttribute('value','Submit Form');
$resetLi = new LI();
$submitLi = new LI();
$ol2->addChild($resetLi->addChild($resetButton));
$ol2->addChild($submitLi->addChild($submitButton));
// fill tempate with data
foreach(Category::find('all',array('sort'=>array('name'=>'ASC'))) as $category) {
$option = new FormOption(ucwords($category->name),$category->id);
if(isset($_POST['project']['category']) && $_POST['project']['category']==$category->id) { $option->addAttribute('selected','selected'); }
$categorySelect->addChild($option);
}
foreach(Range::find('all',array('sort'=>array('min_value'=>'ASC'))) as $range) {
$option = new FormOption(ucwords('$'.$range->min_value.' – $'.$range->max_value),$range->id);
if(isset($_POST['project']['range']) && $_POST['project']['range']==$range->id) { $option->addAttribute('selected','selected'); }
$rangeSelect->addChild($option);
}
if(isset($_POST['project']['message']) && !empty($_POST['project']['message'])) $messageTextarea->addChild(new SimpleText($_POST['project']['message']));
if(isset($_POST['project']['title']) && !empty($_POST['project']['title'])) $titleInput->addAttribute('value',$_POST['project']['title']);
echo $page->render();
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Project-Pad Project Form</title>
<link rel="stylesheet" type="text/css" href="/include/css/base.css">
</head>
<body>
<form action="/project_form_dynamic.php" method="POST" id="project-form">
<input name="project[id]" type="hidden">
<fieldset class="fields">
<legend>Project Form</legend>
<ol id="project-data">
<li>
<label name="project-title">Project Title:</label>
<input name="project[title]" type="text" maxlength="40" id="project-title">
</li>
<li>
<label name="project-category">Project Category:</label>
<select id="project-category" name="project[category]">
<option value="5">Advertising</option>
<option value="4">Copywriting</option>
<option value="3">Graphic Design</option>
<option value="6">Marketing</option>
<option value="2">Mysql</option>
<option value="1">Web Design</option>
</select>
</li>
<li>
<label name="project-range">Project Range:</label>
<select id="project-range" name="project[range]">
<option value="1">$10 – $50</option>
<option value="2">$50 – $100</option>
<option value="3">$100 – $200</option>
<option value="4">$200 – $500</option>
<option value="5">$500 – $1000</option>
<option value="6">$1000 – $2000</option>
<option value="7">$2000 – $5000</option>
<option value="8">$5000 – $10000</option>
<option value="9">$10000 – $50000</option>
<option value="10">$50000 – $200000</option>
</select>
</li>
<li>
<label name="project-message">Project Description:</label>
<textarea id="project-message" name="project[message]" cols="60" rows="15"></textarea>
</li>
</ol>
</fieldset>
<fieldset class="controls">
<ol id="form-controls">
<li>
<input type="reset" name="reset" value="Reset Form">
</li>
<li>
<input type="submit" name="submit" value="Submit Form">
</li>
</ol>
</fieldset>
</form>
</body>
</html>
Basically, it will not only build all the HTML, but do in a easy to read manor also. I can’t stand auto generators that just result in one long string.
Example of some of the subclasses I’ll leave the rest up to you if you feel so inclined to create something similar:
class Paragraph extends Element {
public function __construct($text=null) {
parent::__construct('p');
if($text) {
$this->addChild(new SimpleText($text));
}
}
public function addChild(SimpleText $text) {
return parent::addChild($text);
}
}
class FormSelect extends Element {
public function __construct() {
parent::__construct('select');
}
public function addChild(FormOption $option) {
return parent::addChild($option);
}
}
class FormOption extends Element {
public function __construct($text=null,$value=null) {
parent::__construct('option');
if($text) {
$this->addChild(new SimpleText($text));
}
if($value) {
$this->addAttribute('value',$value);
}
}
public function addChild(SimpleText $text) {
return parent::addChild($text);
}
}
class FormInput extends ClosedElement {
public function __construct() {
parent::__construct('input');
}
}
class Div extends Element {
public function __construct($id=null,$class=null) {
parent::__construct('div');
if($id) {
$this->addAttribute('id',$id);
}
if($class) {
$this->addAttribute('class',$class);
}
}
}
class HForm extends Element {
public function __construct($action=null,$method=null,$id=null,$class=null) {
parent::__construct('form');
if($action) $this->addAttribute('action',$action);
if($method) $this->addAttribute('method',$method);
if($id) $this->addAttribute('id',$id);
if($class) $this->addAttribute('class',$class);
}
}
class Fieldset extends Element {
public function __construct($id=null,$class=null) {
parent::__construct('fieldset');
if($id) $this->addAttribute('id',$id);
if($class) $this->addAttribute('class',$class);
}
}
class OL extends Element {
public function __construct($id=null,$class=null) {
parent::__construct('ol');
if($id) $this->addAttribute('id',$id);
if($class) $this->addAttribute('class',$class);
}
public function addChild(LI $element) {
return parent::addChild($element);
}
}
class UL extends Element {
public function __construct($id=null,$class=null) {
parent::__construct('ul');
if($id) $this->addAttribute('id',$id);
if($class) $this->addAttribute('class',$class);
}
public function addChild(LI $element) {
return parent::addChild($element);
}
}
class LI extends Element {
public function __construct($id=null,$class=null) {
parent::__construct('li');
if($id) $this->addAttribute('id',$id);
if($class) $this->addAttribute('class',$class);
}
}
Here is an example of using it to create a table component:
// projects table
$filter = array();
$filter['status<>'] = 0;
$time = time();
$projects=
Project::find(
'all'
,array(
'include'=>array('range','category','user','bids')
,'condition'=>array('won'=>'Project.id NOT IN (SELECT DISTINCT project_id FROM won_projects)')
,'conditionMap'=>'{won}'
,'filter'=>$filter
,'group'=>'id'
,'dynamic'=>array(
'bid_count'=>'COUNT(Bid.project_id)'
,'days'=>array('TIMESTAMPDIFF(DAY,Project.created,FROM_UNIXTIME(?))',$time)
)
,'limit'=>100
,'offset'=>0
,'sort'=>array('created'=>'DESC')
)
,array()
,array()
,array('select'=>'name')
,array(
'select'=>'created'
,'include'=>'user'
,'require'=>false
,'condition'=>array('stat'=>'(Bid.status IS NULL OR Bid.status <> 0)')
,'conditionMap'=>'{stat}'
)
,array('select'=>'name','require'=>false)
);
$projectsTable = new Element('table');
$projectsTable->addAttribute('class','projects');
$projectsTableHead = new Element('thead');
$projectsTableBody = new Element('tbody');
$projectsTable->addChild($projectsTableHead)->addChild($projectsTableBody);
$projectsHeadTitle = new Element('th');
$projectsHeadBids = new Element('th');
$projectsHeadBudget = new Element('th');
$projectsHeadCategory = new Element('th');
$projectsHeadPosted = new Element('th');
$projectsHeadTitle
->addChild(new SimpleText('Title'))
->addAttribute('class','project-title');
$projectsHeadBids
->addChild(new SimpleText('Bids'))
->addAttribute('class','project-bids');
$projectsHeadBudget
->addChild(new SimpleText('Budget'))
->addAttribute('class','project-range');
$projectsHeadCategory
->addChild(new SimpleText('Category'))
->addAttribute('class','project-category');
$projectsHeadPosted
->addChild(new SimpleText('Posted'))
->addAttribute('class','project-created');
$projectsTableHead
->addChild($projectsHeadTitle)
->addChild($projectsHeadBids)
->addChild($projectsHeadBudget)
->addChild($projectsHeadCategory)
->addChild($projectsHeadPosted);
foreach($projects as $project) {
$row = new Element('tr');
$projectsTableBody
->addChild($row);
$projectTitle = new Element('td');
$projectBids = new Element('td');
$projectBudget = new Element('td');
$projectCategory = new Element('td');
$projectPosted = new Element('td');
$row
->addAttribute('class','project')
->addChild($projectTitle)
->addChild($projectBids)
->addChild($projectBudget)
->addChild($projectCategory)
->addChild($projectPosted);
$projectTitleAnchor = new Anchor('/index.php/project/'.$project->id);
$projectTitleAnchor
->addChild(new SimpleText($project->title));
$projectTitle
->addChild($projectTitleAnchor)
->addAttribute('class','project-title');
$projectBids
->addChild(new SimpleText($project->bid_count))
->addAttribute('class','project-bids');
$projectBudget
->addChild(new SimpleText('$'.$project->range->min_value.'–$'.$project->range->max_value))
->addAttribute('class','project-range');
$projectCategory
->addChild(new SimpleText(ucwords($project->category->name)))
->addAttribute('class','project-category');
$day = $project->days==0 || $project->days == 1?$project->days==0?'today':'yesterday':$project->days.' days ago';
$projectPosted
->addChild(new SimpleText($day))
->addAttribute('class','project-created');
}
echo "\
".$projectsTable->render();
<table class="projects">
<thead>
<th class="project-title">Title</th>
<th class="project-bids">Bids</th>
<th class="project-range">Budget</th>
<th class="project-category">Category</th>
<th class="project-created">Posted</th>
</thead>
<tbody>
<tr class="project">
<td class="project-title">
<a href="/index.php/project/13">My new project *****es!!!</a>
</td>
<td class="project-bids">0</td>
<td class="project-range">$5000–$10000</td>
<td class="project-category">Graphic Design</td>
<td class="project-created">2 days ago</td>
</tr>
<tr class="project">
<td class="project-title">
<a href="/index.php/project/12">new title</a>
</td>
<td class="project-bids">0</td>
<td class="project-range">$50–$100</td>
<td class="project-category">Marketing</td>
<td class="project-created">3 days ago</td>
</tr>
<tr class="project">
<td class="project-title">
<a href="/index.php/project/6">My new project</a>
</td>
<td class="project-bids">2</td>
<td class="project-range">$100–$200</td>
<td class="project-category">Copywriting</td>
<td class="project-created">7 days ago</td>
</tr>
<tr class="project">
<td class="project-title">
<a href="/index.php/project/11">My new project</a>
</td>
<td class="project-bids">2</td>
<td class="project-range">$100–$200</td>
<td class="project-category">Copywriting</td>
<td class="project-created">12 days ago</td>
</tr>
<tr class="project">
<td class="project-title">
<a href="/index.php/project/5">wqdwq d qw d qw d q wd qw d</a>
</td>
<td class="project-bids">0</td>
<td class="project-range">$200–$500</td>
<td class="project-category">Mysql</td>
<td class="project-created">16 days ago</td>
</tr>
<tr class="project">
<td class="project-title">
<a href="/index.php/project/4">wqdwq d qw d qw d q wd qw d</a>
</td>
<td class="project-bids">0</td>
<td class="project-range">$200–$500</td>
<td class="project-category">Mysql</td>
<td class="project-created">16 days ago</td>
</tr>
<tr class="project">
<td class="project-title">
<a href="/index.php/project/3">Looking for web designer</a>
</td>
<td class="project-bids">1</td>
<td class="project-range">$1000–$2000</td>
<td class="project-category">Graphic Design</td>
<td class="project-created">17 days ago</td>
</tr>
<tr class="project">
<td class="project-title">
<a href="/index.php/project/2">Small On going assignment</a>
</td>
<td class="project-bids">0</td>
<td class="project-range">$100–$200</td>
<td class="project-category">Copywriting</td>
<td class="project-created">17 days ago</td>
</tr>
</tbody>
</table>