Ai
7 Star 23 Fork 9

myDcool/CornerPHP

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Html.php 13.58 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
<?php
class Html
{
private $doctype = '<!DOCTYPE html>';
private $lang = 'cn';
private $title = '';
private $head= '';
private $arrMeta = array('<meta charset="UTF-8">');
private $arrStyle = array();
private $arrJs = array();
private $body = '';
private $indent = ' ';
private $eol = PHP_EOL;
public static function ini()
{
return new self;
}
public function setLang($lang)
{
$this->lang = $lang;
return $this;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* @param string $name 可选: author, description, keywords, generator, revised
* @param string $content
* @return self
*/
public function setMetaName($name, $content)
{
$this->arrMeta[] = "<meta name='{$name}' content='{$content}'>";
return $this;
}
/**
* @param string $equiv 可选: content-type, expires, refresh, set-cookie
* @param string $content
* @return self
*/
public function setMetaEquiv($equiv, $content)
{
$this->arrMeta[] = "<meta http-equiv='{$equiv}' content='{$content}'>";
return $this;
}
/**
* @param string $url <link rel='stylesheet' href='xxx.min.css'>
* @return self
*/
public function css($url)
{
$this->arrStyle[] = "<link rel='stylesheet' href='{$url}'>";
return $this;
}
/**
* @param string $url <script src="" type="text/javascript"></script>
* @return self
*/
public function js($url)
{
$this->arrJs[] = "<script type='application/javascript' src='{$url}'></script>";
return $this;
}
/**
* 组装 head 标签
* @return $this
*/
public function setHead()
{
$arrHead = array();
$arrHead[] = '<head>';
$arrHead[] = $this->indent.implode($this->eol.$this->indent, $this->arrMeta);
$arrHead[] = $this->indent."<title> {$this->title} </title>";
$arrHead[] = $this->indent.implode($this->eol.$this->indent, $this->arrStyle);
$arrHead[] = $this->indent.implode($this->eol.$this->indent, $this->arrJs);
$arrHead[] = '</head>';
$this->head = implode($this->eol, $arrHead);
return $this;
}
/**
* @param $body
* @return $this
*/
public function setBody(body $body)
{
$this->body = $body->out();
return $this;
}
/**
* 输出HTML内容
* @return string
*/
public function out()
{
$str = $this->doctype.$this->eol;
$str .= "<html lang='{$this->lang}'>".$this->eol;
$str .= $this->head.$this->eol;
$str .= $this->body.$this->eol;
$str .= '</html>';
return $str;
}
}
/**
* HTML标签公用属性, 内容, 缩进等
* Trait attribute
*/
trait attribute
{
private $text = ''; //文字内容
public $indent = ' '; //缩进
public $eol = PHP_EOL; //换行
public $tag = '';
public $arrAttr = array(); // array('data-x' => '123', 'align' => 'left', ...)
/**
* 获取赋了值的属性, 组装成字符串返回
* @return string
*/
public function getAttrs()
{
$attributes = array();
if (!empty($this->arrAttr)) {
$class = !empty($this->arrAttr['class']) ? $this->arrAttr['class'] : array();
unset($this->arrAttr['class']);
if (!empty($class)) {
$str = implode(' ', $class);
$attributes[] = "class='{$str}'";
}
foreach ($this->arrAttr as $k => $v) {
$k = strval($k);
$v = strval($v);
$attributes[] = (strlen($v) == 0) ? "{$k}" : "{$k}='{$v}'";
}
return ' '.implode(' ', $attributes);
} else {
return '';
}
}
/**
* 每次调用out()方法后清空对象中的属性, 避免引起混乱
*/
public function init()
{
$this->arrAttr = array();
}
public function setText($v)
{
$this->text = $v;
return $this;
}
public function setAttr($k, $v)
{
$this->arrAttr[$k] = $v;
return $this;
}
public function addClass($v)
{
$this->arrAttr['class'][] = $v;
return $this;
}
}
//一下是单独列出的标签类, 其中tag类是通用的可闭合标签类, 支持嵌套
// 还有些可闭合的标签单独成类: 例如p标签也是可闭合的, 但是单独列出来是为了显示时没有多余的换行, 不支持嵌套
//注释
class comment
{
public $indent = ' ';
public $eol = '';
public $text;
public static function ini()
{
return new self;
}
public function setIndent($indent)
{
$this->indent = $indent;
return $this;
}
public function eol($eol)
{
$this->eol = $eol;
return $this;
}
public function setText($text)
{
$this->text = $text;
return $this;
}
public function out()
{
return $this->indent."<!-- {$this->text} -->".$this->eol;
}
}
class label
{
use attribute;
public static function ini()
{
return new self;
}
public function out()
{
$str = '<label'.$this->getAttrs().'>'.$this->text.'</label>';
$this->init();
return $this->indent.$str;
}
}
class a
{
use attribute;
public static function ini()
{
return new self;
}
public function out()
{
$str = '<a'.$this->getAttrs().'>'.$this->text.'</a>';
$this->init();
return $this->indent.$str;
}
}
class p
{
use attribute;
public static function ini()
{
return new self;
}
public function out()
{
$str = '<p'.$this->getAttrs().'>'.$this->text.'</p>';
$this->init();
return $this->indent.$str;
}
}
class img
{
use attribute;
public static function ini()
{
return new self;
}
public function out()
{
$str = '<img'.$this->getAttrs().'>';
$this->init();
return $this->indent.$str;
}
}
class input
{
use attribute;
public static function ini()
{
return new self;
}
public function out()
{
$str = '<input'.$this->getAttrs().'>';
$this->init();
return $this->indent.$str;
}
}
class checkbox
{
use attribute;
public static function ini()
{
return new self;
}
public function out()
{
$str = '<input'.$this->getAttrs().'>';
$this->init();
return $this->indent.$str;
}
}
class radio
{
use attribute;
public static function ini()
{
return new self;
}
public function out()
{
$str = '<input'.$this->getAttrs().'>';
$this->init();
return $this->indent.$str;
}
}
class select
{
use attribute;
public static function ini()
{
return new self;
}
public $arrChild = array();
public function append(option $option)
{
$this->arrChild[] = $option;
return $this;
}
//在顶部插入
public function appendTop($child)
{
array_unshift($this->arrChild, $child);
return $this;
}
public function out()
{
$str = '<select'. $this->getAttrs() .'>'.$this->eol;
foreach ($this->arrChild as $k => $option) {
$option->indent .= $this->indent;
$this->arrChild[$k] = $option->out();
}
$str .= implode($this->eol, $this->arrChild);
$str .= $this->eol.$this->indent.'</select>';
$this->init();
return $this->indent.$str;
}
}
class option
{
use attribute;
public static function ini()
{
return new self;
}
public function out()
{
$str = '<option'.$this->getAttrs().'>'.$this->text.'</option>';
$this->init();
return $this->indent.$str;
}
}
class form
{
use attribute;
const ENCTYPE_DEFAULT = 'application/x-www-form-urlencoded'; // 空格转换为 "+" 加号,特殊符号转换为 ASCII HEX 值
const ENCTYPE_FILE = 'multipart/form-data'; // 文件上传
const ENCTYPE_TEXT = 'text/plain'; // 空格转换为+, 其他特殊字符不做处理
public $arrChild = array();
public static function ini()
{
return new self;
}
/**
* @param mixed $child 子元素对象, 必须有out方法, 并且返回HTML
*/
public function append($child)
{
$this->arrChild[] = $child;
return $this;
}
//在顶部插入
public function appendTop($child)
{
array_unshift($this->arrChild, $child);
return $this;
}
public function out()
{
$str = '<form'.$this->getAttrs().'>'.$this->eol;
//补充缩进
foreach ($this->arrChild as $ck => $child) {
$child->indent .= $this->indent;
$this->arrChild[$ck] = $child->out();
}
$str .= implode($this->eol, $this->arrChild);
$str .= $this->eol.$this->indent.'</form>';
$this->init();
return $this->indent.$str;
}
}
class table
{
use attribute;
public $arrChild = array();
public static function ini()
{
return new self;
}
public function append($child)
{
$this->arrChild[] = $child;
return $this;
}
//在顶部插入
public function appendTop($child)
{
array_unshift($this->arrChild, $child);
return $this;
}
public function out()
{
$str = '<table'.$this->getAttrs().'>'.$this->eol;
foreach ($this->arrChild as $trk => $tr) {
$tr->indent .= $this->indent;
foreach ($tr->arrChild as $tdk => $td) {
$td->indent .= $tr->indent;
$tr->arrChild[$tdk] = $td->out();
}
$this->arrChild[$trk] = $tr->out();
}
$str .= implode($this->eol.$this->eol, $this->arrChild);
$str .= $this->eol.$this->indent.'</table>';
$this->init();
return $this->indent.$str;
}
}
class tr
{
use attribute;
public static function ini()
{
return new self;
}
public $arrChild = array();
public function append($child)
{
$this->arrChild[] = $child;
return $this;
}
//在顶部插入
public function appendTop($child)
{
array_unshift($this->arrChild, $child);
return $this;
}
public function out()
{
$str = '<tr'.$this->getAttrs().'>'.$this->eol;
$str .= implode($this->eol, $this->arrChild);
$str .= $this->eol.$this->indent.'</tr>';
$this->init();
return $this->indent.$str;
}
}
class th
{
use attribute;
public static function ini()
{
return new self;
}
public function out()
{
$str = '<th'.$this->getAttrs().'>'.$this->text.'</td>';
$this->init();
return $this->indent.$str;
}
}
class td
{
use attribute;
public static function ini()
{
return new self;
}
public function out()
{
$str = '<td'.$this->getAttrs().'>'.$this->text.'</td>';
$this->init();
return $this->indent.$str;
}
}
//自定义闭合标签
class tag
{
use attribute;
public $arrChild = array();
public static function ini($tag)
{
return new tag($tag);
}
public function __construct($tag)
{
$this->tag = $tag;
}
//在后边追加
public function append($child, $key='')
{
if (!empty($key)) {
$this->arrChild[$key] = $child;
} else {
$this->arrChild[] = $child;
}
return $this;
}
//在顶部插入
public function appendTop($child)
{
array_unshift($this->arrChild, $child);
return $this;
}
public function out()
{
$str = '<'.$this->tag.$this->getAttrs().'>'.$this->eol;
$str .= $this->indent.$this->text.$this->eol;
foreach ($this->arrChild as $ck => $child) {
$child->indent .= $this->indent;
$this->arrChild[$ck] = $child->out();
}
$str .= implode($this->eol, $this->arrChild);
$str .= $this->eol.$this->indent."</{$this->tag}>";
return $this->indent.$str;
}
}
class body
{
use attribute;
public $arrChild = array();
/**
* @param mixed $child 子元素对象, 必须有out方法, 并且返回HTML文件
* @return $this
*/
public function append($child)
{
$this->arrChild[] = $child->out();
return $this;
}
/**
* @param mixed $child 子元素对象, 必须有out方法, 并且返回HTML文件
* @return $this
*/
public function appendTop($child)
{
array_unshift($this->arrChild, $child->out());
return $this;
}
public function out()
{
$str = '<body>'.$this->eol;
$str .= implode($this->eol.$this->eol, $this->arrChild);
$str .= $this->eol.'</body>';
return $str;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/myDcool/SummerPHP.git
git@gitee.com:myDcool/SummerPHP.git
myDcool
SummerPHP
CornerPHP
master

搜索帮助