论坛首页 编程语言技术论坛

QEE学习 在视图模板中使用自定义的辅助类

浏览 3147 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-12-16   最后修改:2009-12-16
PHP

这几天看阿旭使用QEE,我也心痒不已,正好手头有个项目,正好学习下....又学习了又挣钱了...

 

QEE不说,还真难,官房提供的快速指南只能说 太简易.....

 

色色比较熟悉flea,好不容易说服自己使用qee,这次一定要全身而退!!!!!

 

qee的控制器实现的不错,还实现了命名空间的概念,这个确实很吸引我,加上路由功能....

 

只是视图这块做的我觉得不咋的,我也很讨厌smarty,我是说纯PHP引擎你也多写点辅助方法啊....

 

缺省生成的视图页面: default_layout.php 写道
<link rel="stylesheet" type="text/css" href="<?php echo $_BASE_DIR; ?>css/style.css">

 

里面 竟然有这种存在,很无语啊,起码你也要像rails,做个什么 样式表标签之类的吧.... 自己来吧,开始...

 

在helper目录下建立一个x.php文件,里面是色色为自己提供的常规方法....

<?php
class Helper_X {
	
	private $_baseuri ;	private $_basedir ;
	
	protected function __construct($_baseuri){
		
		$this->_basedir = Q::ini('app_config/ROOT_DIR') ;
		if (!preg_match("/\\$/i",$this->_basedir))
			$this->_basedir .= '/' ; 
		$this->_baseuri = $_baseuri ;	
		if (!preg_match("/\\$/i",$_baseuri))
			$this->_basedir .= '/' ; 
	}
	
	static function instance($_baseuri){
		static $instance;
        if (is_null($instance))
        {
            if (!empty($_baseuri))
            {                
                $instance = new Helper_X($_baseuri);
            }else
            	die('INVALID CONSTRUCT X');
            
        }
        return $instance;
	}
	
	private function resIsExist($fpath){
		if(file_exists("{$this->_basedir}{$fpath}"))
			return true ;
		return false ;
	}
	
	private function ftimestamp($fpath){
		return filemtime("{$this->_basedir}{$fpath}");
	}
	
	public function stylesheet_link_tag(){
		$css_fpath = "css/%s.css" ;
		
		$params = func_get_args();		
		foreach ($params as $param){
			$fpath = sprintf($css_fpath,$param);
			if(!$this->resIsExist($fpath)){
				echo sprintf("<style>/* {File not found : %s%s */</style>\n",$this->_baseuri,$fpath);
//				QLog::log("未能加载CSS文件: {$this->_basedir}{$fpath}", QLog::WARN);
			}
			else {
				
				echo sprintf("<link rel=\"stylesheet\" href=\"{$this->_baseuri}%s?%d\" type=\"text/css\" media=\"screen\" />\n",
					$fpath,$this->ftimestamp($fpath));	
			}
		}	
	}
	
	public function js_include_tag(){		
		$js_fpath = "js/%s.js" ;
		
		$params = func_get_args();		
		foreach ($params as $param){
			$fpath = sprintf($js_fpath,$param);
			if(!$this->resIsExist($fpath)){
				echo sprintf("<script>/* {File not found : %s%s */</script>\n",$this->_baseuri,$fpath);
//				QLog::log("未能加载JS文件: {$this->_basedir}{$fpath}", QLog::WARN);
			}
			else {
				echo sprintf("<script type=\"text/javascript\" src=\"{$this->_baseuri}%s?%d\"></script>\n",
					$fpath,$this->ftimestamp($fpath));	
			}
		}		
	}
	
	function js_include_ucren($skin='qq',$iscache=false){	
		$fpath = 'js/engine/boot.js' ;
		if(!$this->resIsExist($fpath)){
			echo sprintf("<script>/* {Ucren not found : %s%s */</script>\n",$this->_baseuri,$fpath);
			QLog::log("未能加载Ucren组件: {$this->_basedir}{$fpath}", QLog::WARN);
		}else {
			echo sprintf("<script type=\"text/javascript\" src=\"{$this->_baseuri}%s?%d&skin=%s\"></script>\n",
				$fpath,$this->ftimestamp($fpath),$skin);
		}
	}
	
	public function js_text($js_code=null){
		echo "<script>{$js_code}</script>\n" ;
	}
	
	function css_text($css_code=null){
		echo "<style>{$css_code}</style>\n" ;
	}
	
	/**
	 * 返回图片地址
	 *
	 * @param string $_path
	 */
	public function img_url($_path,$return = false){
		if ($return)
			return "{$this->_baseuri}img/{$_path}";
		echo "{$this->_baseuri}img/{$_path}";
	}
	
	//定义一个函数用于调用FCKeditor
	public function call_fck($path=null,$t_set=null,$iname=null,$ivalue=null,$w=null,$h=null){
		if (!class_exists('Helper_FCKeditor'))
			Q::loadClass('Helper_FCKeditor');
		$fcked = Q::singleton('Helper_FCKeditor') ; 
		$fcked->BasePath = empty($path)? $this->_baseuri . 'js/editor/':$path; 
		$fcked->ToolbarSet = empty($t_set)?'Basic':$t_set ; //工具栏设置
		$fcked->InstanceName = empty($iname)?'test':$iname ;
		$fcked->Width = empty($w)?'100%':$w ;
		$fcked->Height = empty($h)?'200':$h ;
		$fcked->Value = $ivalue;	
		$fck_area = $fcked->CreateHtml();
		return $fck_area ;
	}
}
?>

 

 

这种 简单的方法,注释我看还是不用写了吧,要看注释的,请看我之前发的色色的FLEAPHP扩展,这些方法基本摘自那里,又简化封装了下....

 

对应于最后一个fck调用,这里还要在helper目录下建立一个fckeditor.php文件,内容如下:

<?php
/**
 * 富文本编辑器 辅助器
 *
 */
class Helper_FCKeditor
{
	var $InstanceName ;
	var $BasePath ;
	var $Width ;
	var $Height ;
	var $ToolbarSet ;
	var $Value ;
	var $Config ;

	// PHP 4 Contructor
	function Helper_FCKeditor( $instanceName = "test")
	{
		$this->InstanceName	= $instanceName ;
		$this->BasePath		= './editor/' ;
		$this->Width		= '100%' ;
		$this->Height		= '350' ;
		$this->ToolbarSet	= 'Default' ;
		$this->Value		= '' ;

		$this->Config		= array() ;
	}

	function Create()
	{
		echo $this->CreateHtml() ;
	}

	function CreateHtml()
	{
		$HtmlValue = htmlspecialchars( $this->Value ) ;

		$Html = '<div>' ;

		if ( $this->IsCompatible() )
		{
			$File = 'fckeditor.html' ;

			$Link = "{$this->BasePath}editor/{$File}?InstanceName={$this->InstanceName}" ;

			if ( $this->ToolbarSet != '' )
				$Link .= "&amp;Toolbar={$this->ToolbarSet}" ;

			// Render the linked hidden field.
			$Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}\" name=\"{$this->InstanceName}\" value=\"{$HtmlValue}\" style=\"display:none\" />" ;

			// Render the configurations hidden field.
			$Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}___Config\" value=\"" . $this->GetConfigFieldString() . "\" style=\"display:none\" />" ;

			// Render the editor IFRAME.
			$Html .= "<iframe id=\"{$this->InstanceName}___Frame\" src=\"{$Link}\" width=\"{$this->Width}\" height=\"{$this->Height}\" frameborder=\"0\" scrolling=\"no\"></iframe>" ;
		}
		else
		{
			if ( strpos( $this->Width, '%' ) === false )
				$WidthCSS = $this->Width . 'px' ;
			else
				$WidthCSS = $this->Width ;

			if ( strpos( $this->Height, '%' ) === false )
				$HeightCSS = $this->Height . 'px' ;
			else
				$HeightCSS = $this->Height ;

			$Html .= "<textarea name=\"{$this->InstanceName}\" rows=\"4\" cols=\"40\" style=\"width: {$WidthCSS}; height: {$HeightCSS}\">{$HtmlValue}</textarea>" ;
		}

		$Html .= '</div>' ;

		return $Html ;
	}

	function IsCompatible()
	{
		global $HTTP_USER_AGENT ;

		if ( isset( $HTTP_USER_AGENT ) )
			$sAgent = $HTTP_USER_AGENT ;
		else
			$sAgent = $_SERVER['HTTP_USER_AGENT'] ;

		if ( strpos($sAgent, 'MSIE') !== false && strpos($sAgent, 'mac') === false && strpos($sAgent, 'Opera') === false )
		{
			$iVersion = (float)substr($sAgent, strpos($sAgent, 'MSIE') + 5, 3) ;
			return ($iVersion >= 5.5) ;
		}
		else if ( strpos($sAgent, 'Gecko/') !== false )
		{
			$iVersion = (int)substr($sAgent, strpos($sAgent, 'Gecko/') + 6, 8) ;
			return ($iVersion >= 20030210) ;
		}
		else
			return false ;
	}

	function GetConfigFieldString()
	{
		$sParams = '' ;
		$bFirst = true ;

		foreach ( $this->Config as $sKey => $sValue )
		{
			if ( $bFirst == false )
				$sParams .= '&amp;' ;
			else
				$bFirst = false ;

			if ( $sValue === true )
				$sParams .= $this->EncodeConfig( $sKey ) . '=true' ;
			else if ( $sValue === false )
				$sParams .= $this->EncodeConfig( $sKey ) . '=false' ;
			else
				$sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $sValue ) ;
		}

		return $sParams ;
	}

	function EncodeConfig( $valueToEncode )
	{
		$chars = array(
			'&' => '%26',
			'=' => '%3D',
			'"' => '%22' ) ;

		return strtr( $valueToEncode,  $chars ) ;
	}
}
?>

 

然后 将 文件拷贝到'js/editor' 下即可....

 

然后修改控制器,在你自己的控制器中覆盖Controller_Abstract 的_before_render方法即可

 

/**
     * 渲染之前调用
     *
     * @param QView_Render_PHP
     */
    protected function _before_render($response)
    {
    	$x = Helper_X::instance($response->getVar('_BASE_DIR'));
		$response->assign('x',$x);
    }

 

 

然后在你的视图文件中就可以使用了;

<?php 
// 指示该视图从 _layouts/default_layout 继承
$this->_extends('_layouts/default_layout'); 
?>

<?php
// 定义一个名为 contents 的区块
$this->_block('contents'); 
?>


<?php

$x->js_include_tag("xx",'jq','bbs');
$x->js_include_ucren();
$x->js_text("
Ucren.onReady(function(){
	Ucren.alert('Ucren');
});
");


$x->stylesheet_link_tag("xx",'jq','style');
$x->css_text("body{color:red;}");
?>

<img src="<?php $x->img_url('qeephp.jpg'); ?>" />

<form name="form_user" id="form_user"
    action="<?php echo url('admin::city'); ?>" method="post">
 
  <fieldset>
    <p>
      <label for="username">用户名</label>
      <input type="text" name="username" id="username" />
    </p>
 
    <p>
      <label for="password">密码</label>
      <input type="password" name="password" id="password" />
    </p>
 
    <p>
      <label for="body">描述</label>
      <?php
		$fck_body = $x->call_fck(null,'TIC_MIN','body','',"98%",'100');
		echo $fck_body ;
		
      ?>
    </p>
 
    <p>
      <input type="submit" name="Submit" value="提交" />
    </p>
    
 
  </fieldset>
 
</form>
<?php
// 区块定义结束
$this->_endblock(); 
?>

 

运行后页面源代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>QeePHP: 新一代的敏捷开发框架</title>
<link rel="stylesheet" type="text/css" href="/xutic/css/style.css">
</head>
<body>

<div id="page">

  
<div id="sidebar">
  <ul id="sidebar-items">
    <li>
      <form id="search" action="http://www.google.com/search" method="get" target="_blank">
        <input type="hidden" name="hl" value="en" />
        <input type="text" id="search-text" name="q" value="site:qeephp.org " />
        <input type="submit" value="搜索" />
        QeePHP 网站
      </form>
    </li>
    <li>
      <h3>加入社区</h3>
      <ul class="links">
        <li><a href="http://qeephp.com/" target="_blank">QeePHP 官方网站</a></li>
        <li><a href="http://qeephp.com/bbs/" target="_blank">论坛</a></li>
        <li><a href="http://code.google.com/p/qeephp/issues/list" target="_blank">Bug 报告</a></li>
      </ul>
    </li>
    <li>
      <h3>浏览文档</h3>
      <ul class="links">
        <li><a href="http://qeephp.org/docs/qeephp/api/" target="_blank">QeePHP API 文档</a></li>
        <li><a href="http://www.php.net/docs.php" target="_blank">PHP 文档</a></li>
      </ul>
    </li>
  </ul>
</div>

  <div id="content">



<script type="text/javascript" src="/xutic/js/xx.js?1260961596"></script>
<script>/* {File not found : /xutic/js/jq.js */</script>
<script>/* {File not found : /xutic/js/bbs.js */</script>
<script type="text/javascript" src="/xutic/js/engine/boot.js?1257408552&skin=qq"></script>
<script>
Ucren.onReady(function(){
	Ucren.alert('Ucren');
});
</script>
<style>/* {File not found : /xutic/css/xx.css */</style>
<style>/* {File not found : /xutic/css/jq.css */</style>
<link rel="stylesheet" href="/xutic/css/style.css?1260857742" type="text/css" media="screen" />
<style>body{color:red;}</style>

<img src="/xutic/img/qeephp.jpg" />

<form name="form_user" id="form_user"
    action="/xutic/index.php/admin/city" method="post">
 
  <fieldset>
    <p>
      <label for="username">用户名</label>
      <input type="text" name="username" id="username" />
    </p>
 
    <p>
      <label for="password">密码</label>
      <input type="password" name="password" id="password" />
    </p>
 
    <p>
      <label for="body">描述</label>
      <div><input type="hidden" id="body" name="body" value="" style="display:none" /><input type="hidden" id="body___Config" value="" style="display:none" /><iframe id="body___Frame" src="/xutic/js/editor/editor/fckeditor.html?InstanceName=body&amp;Toolbar=TIC_MIN" width="98%" height="100" frameborder="0" scrolling="no"></iframe></div>    </p>
 
    <p>
      <input type="submit" name="Submit" value="提交" />
    </p>
    
 
  </fieldset>
 
</form>

  </div>
  <div id="footer">
    <p>
      Powered by <a href="http://qeephp.com/" target="_blank">QeePHP 2.1</a>
      |
      <a href="http://www.qeeyuan.com/" target="_blank">起源科技</a>
    </p>
  </div>
</div>

</body>
</html>

 

 

这里我再给大家抓个图看看啊:

 

 

  • 大小: 30.4 KB
   发表时间:2009-12-17  
轻松实现 QEE多级控制器目录模块结构 -- 色色写的 http://qeephp.com/bbs/viewthread.php?tid=10560&extra=
0 请登录后投票
   发表时间:2009-12-17  
启用个gzip压缩,只需在index.php中 这样写:
//启用gzip压缩
//ob_start('ob_gzhandler');

Q::ini('app_config/compress', false) ? ob_start() : ob_start('ob_gzhandler');
$ret = MyApp::instance($app_config)->dispatching();
ob_end_flush();
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics