经常用Bootstrap开发后台,搞了一个标签库<?php <br />
namespace Think\Template\TagLib;<br>
<br>
use Think\Template\TagLib;<br>
<br>
class Bootstrap extends TagLib<br>
{<br>
<br>
protected $tags = array(<br>
/**<br>
* id 自定义ID,不定义默认(input+表单name)<br>
* class 自定义class<br>
* help 表单的输入说明 显示再表单下方<br>
* status 当前表单的初始化状态,可选值 :success,error,warning 三个值选其一<br>
* inline 内联表单<br>
* horizontal 水平排列的表单【说明:两个数字,逗号分割,实用这个排列方式需要在表单上面添加 form-horizontal】【示例:horizontal="3,9"】<br>
*/<br>
'text' => array('attr' => array('id', 'class', 'name', 'value', 'label', 'placeholder', 'help', 'status', 'inline', 'horizontal'), 'close' => 0),<br>
'password' => array('attr' => array('id', 'class', 'name', 'label', 'placeholder', 'help', 'status', 'inline', 'horizontal'), 'close' => 0),<br>
'number' => array('attr' => array('id', 'class', 'name', 'value', 'label', 'placeholder', 'help', 'status', 'inline', 'horizontal'), 'close' => 0),<br>
'email' => array('attr' => array('id', 'class', 'name', 'value', 'label', 'placeholder', 'help', 'status', 'inline', 'horizontal'), 'close' => 0),<br>
'url' => array('attr' => array('id', 'class', 'name', 'value', 'label', 'placeholder', 'help', 'status', 'inline', 'horizontal'), 'close' => 0),<br>
'file' => array('attr' => array('id', 'class', 'name', 'label', 'help', 'inline', 'horizontal'), 'close' => 0),<br>
'checkbox' => array('attr' => array('id', 'class', 'name', 'label', 'inline', 'disabled', 'checked', 'default'), 'close' => 0),<br>
'radio' => array('attr' => array('id', 'class', 'name', 'label', 'inline', 'disabled', 'checked', 'default'), 'close' => 0),<br>
/**<br>
* key : value值字段名称,默认id<br>
* text : 显示值字段名称,默认name<br>
*/<br>
'textarea' => array('attr' => array('id', 'class', 'name', 'label', 'value', 'label', 'rows', 'key', 'text', 'horizontal'), 'close' => 0),<br>
'select' => array('attr' => array('id', 'class', 'name', 'label', 'data', 'value', 'key', 'horizontal'), 'close' => 0)<br>
);<br>
private $ids = array();//自动生产ID保存,防止重复<br>
<br>
/**<br>
* @param $id string input的ID<br>
* @param $name string input的name值<br>
* @return string<br>
*/<br>
protected function setId($id, $name)<br>
{<br>
return empty($id) ? 'input' . ucfirst($name) . mt_rand(1, 9999) : $id;<br>
}<br>
<br>
/**<br>
* 根据表单的状态返回图标和状态<br>
* @param $status 表单当前的状态<br>
* @return array()<br>
*/<br>
protected function inputStatus($attr)<br>
{<br>
switch ($attr['status']) {<br>
case 'success':<br>
$icon = '<span></span>';<br>
$class = 'has-success';<br>
break;<br>
case 'error':<br>
$icon = '<span></span>';<br>
$class = 'has-error';<br>
break;<br>
case 'warning':<br>
$icon = '<span></span>';<br>
$class = 'has-warning';<br>
break;<br>
default:<br>
$icon = '';<br>
$class = '';<br>
break;<br>
}<br>
$icon = empty($icon) ? $icon : $icon . '<span>(' . $attr['status'] . ')</span>';<br>
return array('statusClass' => $class, 'icon' => $icon);<br>
}<br>
<br>
protected function wrapDiv($attr)<br>
{<br>
$class = array();<br>
if (!empty($attr['inline'])) array_push($class, 'form-inline');<br>
if (!empty($attr['statusClass'])) array_push($class, $attr['statusClass']);<br>
if (!empty($attr['has_feedback'])) array_push($class, $attr['has_feedback']);<br>
if (!empty($attr['class'])) array_push($class, $attr['class']);<br>
if (empty($attr['typeClass'])) {<br>
//输入类型<br>
array_push($class, 'form-group');<br>
} else {<br>
//选择类型 checkbox radio<br>
array_push($class, $attr['typeClass']);<br>
}<br>
return '<div>';<br>
}<br>
<br>
protected function wrapLabel($attr)<br>
{<br>
$id = $attr['id'];<br>
$width = '';<br>
if (array_key_exists('horizontal', $attr) && !empty($attr['horizontal'])) {<br>
$width = 'col-xs-' . $attr['horizontal'][0];<br>
}<br>
$typeClass = empty($attr['typeClass']) ? 'control-label' : $attr['typeClass'];<br>
return '<label>';<br>
}<br>
<br>
protected function input($attr)<br>
{<br>
$attrs = array();<br>
if (!empty($attr['placeholder'])) array_push($attrs, $attr['placeholder']);//输入框描述<br>
if (!empty($attr['describedby'])) array_push($attrs, $attr['describedby']);//输入框与图标关联,<br>
if (empty($attr['typeClass'])) {<br>
//没有这个类型为输入框类型 text/password/email....<br>
array_push($attrs, 'class="form-control"');//calss 属性<br>
if (!empty($attr['value'])) array_push($attrs, 'value=' . $attr['value']);//默认值<br>
} else {<br>
//选择类型 checkbox/radio<br>
if (!empty($attr['default'])) {<br>
$default = $this->_trim($attr['default']);<br>
$default = explode('.', $default);<br>
$default = $default[0] . "['" . $default[1] . "']";<br>
$checked = '<?php if(' . $default . ' == ' . $attr['value'] . '){ echo "checked" ; } ?>';<br>
} else {<br>
$checked = empty($attr['checked']) ? '' : 'checked';<br>
}<br>
if (!empty($attr['disabled'])) array_push($attrs, 'disabled');//是否禁用<br>
array_push($attrs, $checked);//默认值判断<br>
}<br>
$input = "<input>";<br>
return $input . $attr['icon'] . $attr['help'];<br>
}<br>
<br>
/**<br>
* 输入框默认表单赋值<br>
* @param $attr<br>
*/<br>
protected function set_input_attr($attr)<br>
{<br>
$attr['id'] = $this->setId($attr['id'], $attr['name']);<br>
$status = $this->inputStatus($attr);<br>
$attr['statusClass'] = $status['statusClass'];//wrapDiv上面显示<br>
$attr['icon'] = $status['icon'];//wrapLabel上面显示<br>
$attr['has_feedback'] = empty($status['icon']) ? '' : 'has-feedback';//wrapDiv上面显示<br>
$attr['describedby'] = empty($status['icon']) ? '' : 'aria-describedby="' . $attr['id'] . 'Status"';//表单上关联图标<br>
$attr['help'] = empty($attr['help']) ? '' : '<span>' . $attr['help'] . '</span>';<br>
$attr['placeholder'] = empty($attr['placeholder']) ? '' : 'placeholder="' . $attr['placeholder'] . '"';<br>
if (array_key_exists('horizontal', $attr) && !empty($attr['horizontal'])) {<br>
$attr['horizontal'] = explode(',', $attr['horizontal']);<br>
}<br>
return $attr;<br>
}<br>
<br>
public function _text($attr)<br>
{<br>
$attr['type'] = 'text';<br>
return $this->inputClass($attr);<br>
}<br>
<br>
public function _password($attr)<br>
{<br>
$attr['type'] = 'password';<br>
return $this->inputClass($attr);<br>
}<br>
<br>
public function _email($attr)<br>
{<br>
$attr['type'] = 'email';<br>
return $this->inputClass($attr);<br>
}<br>
<br>
public function _number($attr)<br>
{<br>
$attr['type'] = 'number';<br>
return $this->inputClass($attr);<br>
}<br>
<br>
public function _url($attr)<br>
{<br>
$attr['type'] = 'url';<br>
return $this->inputClass($attr);<br>
}<br>
<br>
public function _file($attr)<br>
{<br>
$attr['type'] = 'file';<br>
return $this->inputClass($attr);<br>
}<br>
<br>
public function _checkbox($attr)<br>
{<br>
$attr['typeClass'] = empty($attr['inline']) ? 'checkbox' : 'checkbox-inline';<br>
$attr['type'] = 'checkbox';<br>
return $this->checkClass($attr);<br>
}<br>
<br>
public function _radio($attr)<br>
{<br>
$attr['typeClass'] = empty($attr['inline']) ? 'radio' : 'radio-inline';<br>
$attr['type'] = 'radio';<br>
return $this->checkClass($attr);<br>
}<br>
<br>
public function _textarea($attr)<br>
{<br>
$id = $this->setId($attr['id'], $attr['name']);<br>
$textarea = '<textarea>' . $attr['value'] . '</textarea>';<br>
return $this->_return($attr, $textarea);<br>
}<br>
<br>
//选择类型表单<br>
protected function checkClass($attr)<br>
{<br>
$attr = $this->set_input_attr($attr);<br>
$input = $this->input($attr);<br>
$div = $this->wrapDiv($attr);<br>
$label = $this->wrapLabel($attr);<br>
return $div . $label . $input . $attr['label'] . '</label>
</div>';<br>
}<br>
<br>
//输出输入类型表单<br>
protected function inputClass($attr)<br>
{<br>
$input = $this->input($attr);<br>
return $this->_return($attr, $input);<br>
}<br>
<br>
//去除左右花括号<br>
private function _trim($field)<br>
{<br>
$field = ltrim($field, '{');<br>
return rtrim($field, '}');<br>
}<br>
<br>
public function _select($attr)<br>
{<br>
$data = $this->_trim($attr['data']);<br>
$key = empty($attr['key']) ? 'id' : $attr['key'];<br>
$text = empty($attr['text']) ? 'name' : $attr['text'];<br>
$value = $attr['value'];<br>
$id = $this->setId($attr['id'], $attr['name']);<br>
if (strpos($value, '{') !== false) {<br>
$value = $this->_trim($value);<br>
}<br>
$select = '<select>';<br>
$select .= '<?php foreach(' . $data . ' as $v): ?>';<br>
$select .= '<option> >{$v.' . $text . '}</option>';<br>
$select .= '<?php endforeach; ?>';<br>
$select .= '</select>';<br>
return $this->_return($attr, $select);<br>
<br>
}<br>
<br>
public function _return($attr, $form)<br>
{<br>
//判断是不是水平排列的表单<br>
$horizontal = false;<br>
if (array_key_exists('horizontal', $attr) && !empty($attr['horizontal'])) {<br>
$horizontal = true;;<br>
}<br>
$attr = $this->set_input_attr($attr);<br>
$div = $this->wrapDiv($attr);<br>
$label = $this->wrapLabel($attr);<br>
<br>
if ($horizontal) {<br>
return $div . $label . $attr['label'] . '<div>' . $form . '</div>';<br>
} else {<br>
return $div . $label . $attr['label'] . '' . $form . '';<br>
}<br>
}<br>
<br>
<br>
}
调用<taglib></taglib><br>
<?php <br />
<br>
$area = array(<br>
array('id'=>0,'name'=>'北京'),<br>
array('id'=>1,'name'=>'上海'),<br>
array('id'=>2,'name'=>'天津'),<br>
array('id'=>3,'name'=>'深圳')<br>
);<br>
$member = array(<br>
'gender'=>2,<br>
'email'=>'314231604@qq.com',<br>
'sleep'=>1,<br>
'eat'=>1,<br>
'username'=>'小二郎',<br>
'url'=>'http://www.baidu.com'<br>
) ;<br>
?><br>
<form>
<br>
<text></text><br>
<email></email><br>
<password></password><br>
<url></url><br>
<number></number><br>
<file></file><br>
<div>
<br>
<div>爱好</div>
<br>
<div>
<br>
<checkbox></checkbox><br>
<checkbox></checkbox><br>
</div>
<br>
</div>
<br>
<div>
<br>
<div>性别</div>
<br>
<div>
<br>
<radio></radio><br>
<radio></radio><br>
</div>
<br>
</div>
<br>
<textarea></textarea><br>
<select></select><br>
</form>
运行效果
Bootstrap标签库.rar ( 3.94 KB 下载:52 次 )
AD:真正免费,域名+虚机+企业邮箱=0元