自己写的一个PHP面向对象方式的页面生成库,还有很多未完善的,抛砖引玉而已,边用边完善吧。
下面是类库的代码:
<?php
/*
* Filename: uis.php
* Created on 2012-11-8
* Created by RobinTang
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
class UIElement {
public static $pretab = " ";
public $tag;
public $befor;
public $childs;
public $attrs;
public function __construct($t, $atts=false, $chs=false) {
$this->tag = $t;
($atts && $this->attrs = $atts) || $this->attrs = array();
($chs && $this->childs = $chs) || $this->childs = array ();
}
public function add($e) {
array_push($this->childs, $e);
return $this;
}
public function att($k, $v){
$this->attrs[$k] = $v;
return $this;
}
public function toPage($pre = null) {
$cs = '';
$pr = $pre . UIElement :: $pretab;
$at = '';
$res = '';
if(count($this->attrs)){
$keys = array_keys($this->attrs);
foreach($keys as $k){
$v = $this->attrs[$k];
$at .= " $k='$v'";
}
}
if(count($this->childs)){
foreach ($this->childs as $e) {
if(!is_object($e)){
$cs .= $pr.$e . "\r\n";
}
else if(!is_null($e)) {
$cs .= $e->toPage($pr) . "\r\n";
}
}
$res .= "$pre<$this->tag$at>\r\n$cs$pre</$this->tag>";
}
else{
$res .= "$pre<$this->tag$at/>";
}
return $this->befor? $this->befor . "\r\n" . $res:$res;
}
}
function UIPage($iswml = false){
$body = new UIElement('body');
$head = new UIElement('head');
$title = new UIElement('title');
$head->add($title);
$page = new UIElement('html');
$page->add($head);
if($iswml){
$page->befor = '<?xml version="1.0" encoding="utf-8"?>'."\r\n".'<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">';
$page->att('xmlns', 'http://www.w3.org/1999/xhtml');
$wml = new UIElement('wml');
$page->add($wml->add($body));
}
else{
$page->befor = '<!DOCTYPE html>';
$page->add($body);
}
$page->title = $title;
$page->head = $head;
$page->body = $body;
return $page;
}
function UIForm($method='GET', $action=false) {
if($action){
return new UIElement('form', array('method'=>$method, 'action'=>$action));
}
else{
return new UIElement('form', array('method'=>$method));
}
}
function UIInput($name, $type='text', $val=false) {
$e = new UIElement('input', array('name'=>$name, 'type'=>$type));
$e && $e->att('value', $val);
return $e;
}
function UIDiv($att = false, $chs = false){
return new UIElement('div', $att, $chs);
}
function UIBr($att = false){
return new UIElement('br', $att);
}
function UITable($att = false){
return new UIElement('table', $att);
}
function UITr($chs=false, $att = false){
$e = new UIElement('tr', $att);
$chs && $e->childs = $chs;
return $e;
}
function UITd($ch=false, $att = false, $colspan = false){
$e = new UIElement('td', $att);
$ch && $e->add($ch);
$colspan && $e->att('colspan', $colspan);
return $e;
}
function UITh($ch=false, $att = false, $colspan = false){
$e = new UIElement('th', $att);
$ch && $e->add($ch);
$colspan && $e->att('colspan', $colspan);
return $e;
}
function UIA($title='', $href = false, $att = false){
if(!$href)
$href = $title;
$e = new UIElement('a', $att);
$e->att('href', $href);
$e->add($title);
return $e;
}
function uif_align($e, $al = 'center'){
$e->att('align', $al);
return $e;
}
?>
下面是使用的例子,完成一个简单的百度搜索:
<?php
/*
* Filename: demo.php
* Created on 2012-11-11
* Created by RobinTang
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include_once('uis.php');
$pag = UIPage(true); // true表示输出的是web页面,如果是false则输出wap页面
$pag->title->add('测试页面');
$div = UIDiv();
$pag->body->add($div);
uif_align($div); //居中对齐
$div->add('HelloWorld');
$div->add(UIBr());
// 下面创建一个表达
$form = UIForm('GET','http://www.baidu.com/s');
$form->add(UIInput('wd')); // 文本框
$form->add(UIInput('submit', 'submit', '搜索')); // 提交按钮
$div->add($form); // 添加到div中
// 最后输出
header("Content-Type: text/html; charset=UTF-8");
echo $pag->toPage();
?>