自己写的一个PHP面向对象方式的页面生成库(同时支持Web和Wap)

10 篇文章 0 订阅
6 篇文章 0 订阅

自己写的一个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();
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RobinTang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值