中山php|最优网络中山做网站 中山php建站

最优良人

2011/08/16 at 10:31

FleaPHP页面控件 WebControls的使用

如果不使用模板引擎,需要先注册控件 $ui =& FLEA::initWebControls() ;其实就是返回控件的实例,该函数的代码是

00662 function & initWebControls()
00663 {
00664 return FLEA::getSingleton(FLEA::getAppInf('webControlsClassName'));
00665 }

'webControlsClassName'默认是FLEA目录下的webControls类,该类封装了页面组件的实现,以及一些常用的页面控件,在找不到这些自带控件的时候就会去尝试搜索我们自定义的以_ctl开头的控件

/**
* 构造一个控件的 HTML 代码
*
* @param string $type
* @param string $name
* @param array $attribs
* @param boolean $return
*
* @return string
*/
function control($type, $name, $attribs = null, $return = false)
{
$type = strtolower($type);
$render = '_ctl' . ucfirst($type);
$attribs = (array)$attribs;

$__ctl_out = false;
if (method_exists($this, $render)) {
$__ctl_out = $this->{$render}($name, $attribs);
} else {
$extfilename = ucfirst($type) . '.php';
if (!isset($this->_extends[$type])) {
foreach ($this->_extendsDir as $dir) {
if (file_exists($dir . DS . $extfilename)) {
require($dir . DS . $extfilename);
$this->_extends[$type] = true;
break;
}
}
}

if (isset($this->_extends[$type])) {
$__ctl_out = call_user_func_array($render,
array('name' => $name, 'attribs' => $attribs));
}
}

if ($__ctl_out === false) {
$__ctl_out = "INVALID CONTROL TYPE \"{$type}\"";
}

if ($return) { return $__ctl_out; }
echo $__ctl_out;
return '';
}

实例化控件之后,在模版(也就是 .php)中:

1 <?php
2 $ui->control('textbox', 'username',
3 array(
4 'class' => 'textbox',
5 'size' => 28,
6 'maxlength' => 22,
7 )
8 );
9 ?>

如果使用smarty,调用方式就是:

{ webcontrol type='textbox' value=$textbox_value }
系统会自动去实例化控件对象

标签:,
-