当前位置:   article > 正文

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

视图实体辅助类

这几天看阿旭使用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文件,里面是色色为自己提供的常规方法....

  1. <?php
  2. class Helper_X {
  3. private $_baseuri ; private $_basedir ;
  4. protected function __construct($_baseuri){
  5. $this->_basedir = Q::ini('app_config/ROOT_DIR') ;
  6. if (!preg_match("/\\$/i",$this->_basedir))
  7. $this->_basedir .= '/' ;
  8. $this->_baseuri = $_baseuri ;
  9. if (!preg_match("/\\$/i",$_baseuri))
  10. $this->_basedir .= '/' ;
  11. }
  12. static function instance($_baseuri){
  13. static $instance;
  14. if (is_null($instance))
  15. {
  16. if (!empty($_baseuri))
  17. {
  18. $instance = new Helper_X($_baseuri);
  19. }else
  20. die('INVALID CONSTRUCT X');
  21. }
  22. return $instance;
  23. }
  24. private function resIsExist($fpath){
  25. if(file_exists("{$this->_basedir}{$fpath}"))
  26. return true ;
  27. return false ;
  28. }
  29. private function ftimestamp($fpath){
  30. return filemtime("{$this->_basedir}{$fpath}");
  31. }
  32. public function stylesheet_link_tag(){
  33. $css_fpath = "css/%s.css" ;
  34. $params = func_get_args();
  35. foreach ($params as $param){
  36. $fpath = sprintf($css_fpath,$param);
  37. if(!$this->resIsExist($fpath)){
  38. echo sprintf("<style>/* {File not found : %s%s */</style>\n",$this->_baseuri,$fpath);
  39. // QLog::log("未能加载CSS文件: {$this->_basedir}{$fpath}", QLog::WARN);
  40. }
  41. else {
  42. echo sprintf("<link rel=\"stylesheet\" href=\"{$this->_baseuri}%s?%d\" type=\"text/css\" media=\"screen\" />\n",
  43. $fpath,$this->ftimestamp($fpath));
  44. }
  45. }
  46. }
  47. public function js_include_tag(){
  48. $js_fpath = "js/%s.js" ;
  49. $params = func_get_args();
  50. foreach ($params as $param){
  51. $fpath = sprintf($js_fpath,$param);
  52. if(!$this->resIsExist($fpath)){
  53. echo sprintf("<script>/* {File not found : %s%s */</script>\n",$this->_baseuri,$fpath);
  54. // QLog::log("未能加载JS文件: {$this->_basedir}{$fpath}", QLog::WARN);
  55. }
  56. else {
  57. echo sprintf("<script type=\"text/javascript\" src=\"{$this->_baseuri}%s?%d\"></script>\n",
  58. $fpath,$this->ftimestamp($fpath));
  59. }
  60. }
  61. }
  62. function js_include_ucren($skin='qq',$iscache=false){
  63. $fpath = 'js/engine/boot.js' ;
  64. if(!$this->resIsExist($fpath)){
  65. echo sprintf("<script>/* {Ucren not found : %s%s */</script>\n",$this->_baseuri,$fpath);
  66. QLog::log("未能加载Ucren组件: {$this->_basedir}{$fpath}", QLog::WARN);
  67. }else {
  68. echo sprintf("<script type=\"text/javascript\" src=\"{$this->_baseuri}%s?%d&skin=%s\"></script>\n",
  69. $fpath,$this->ftimestamp($fpath),$skin);
  70. }
  71. }
  72. public function js_text($js_code=null){
  73. echo "<script>{$js_code}</script>\n" ;
  74. }
  75. function css_text($css_code=null){
  76. echo "<style>{$css_code}</style>\n" ;
  77. }
  78. /**
  79. * 返回图片地址
  80. *
  81. * @param string $_path
  82. */
  83. public function img_url($_path,$return = false){
  84. if ($return)
  85. return "{$this->_baseuri}img/{$_path}";
  86. echo "{$this->_baseuri}img/{$_path}";
  87. }
  88. //定义一个函数用于调用FCKeditor
  89. public function call_fck($path=null,$t_set=null,$iname=null,$ivalue=null,$w=null,$h=null){
  90. if (!class_exists('Helper_FCKeditor'))
  91. Q::loadClass('Helper_FCKeditor');
  92. $fcked = Q::singleton('Helper_FCKeditor') ;
  93. $fcked->BasePath = empty($path)? $this->_baseuri . 'js/editor/':$path;
  94. $fcked->ToolbarSet = empty($t_set)?'Basic':$t_set ; //工具栏设置
  95. $fcked->InstanceName = empty($iname)?'test':$iname ;
  96. $fcked->Width = empty($w)?'100%':$w ;
  97. $fcked->Height = empty($h)?'200':$h ;
  98. $fcked->Value = $ivalue;
  99. $fck_area = $fcked->CreateHtml();
  100. return $fck_area ;
  101. }
  102. }
  103. ?>

 

 

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

 

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

  1. <?php
  2. /**
  3. * 富文本编辑器 辅助器
  4. *
  5. */
  6. class Helper_FCKeditor
  7. {
  8. var $InstanceName ;
  9. var $BasePath ;
  10. var $Width ;
  11. var $Height ;
  12. var $ToolbarSet ;
  13. var $Value ;
  14. var $Config ;
  15. // PHP 4 Contructor
  16. function Helper_FCKeditor( $instanceName = "test")
  17. {
  18. $this->InstanceName = $instanceName ;
  19. $this->BasePath = './editor/' ;
  20. $this->Width = '100%' ;
  21. $this->Height = '350' ;
  22. $this->ToolbarSet = 'Default' ;
  23. $this->Value = '' ;
  24. $this->Config = array() ;
  25. }
  26. function Create()
  27. {
  28. echo $this->CreateHtml() ;
  29. }
  30. function CreateHtml()
  31. {
  32. $HtmlValue = htmlspecialchars( $this->Value ) ;
  33. $Html = '<div>' ;
  34. if ( $this->IsCompatible() )
  35. {
  36. $File = 'fckeditor.html' ;
  37. $Link = "{$this->BasePath}editor/{$File}?InstanceName={$this->InstanceName}" ;
  38. if ( $this->ToolbarSet != '' )
  39. $Link .= "&amp;Toolbar={$this->ToolbarSet}" ;
  40. // Render the linked hidden field.
  41. $Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}\" name=\"{$this->InstanceName}\" value=\"{$HtmlValue}\" style=\"display:none\" />" ;
  42. // Render the configurations hidden field.
  43. $Html .= "<input type=\"hidden\" id=\"{$this->InstanceName}___Config\" value=\"" . $this->GetConfigFieldString() . "\" style=\"display:none\" />" ;
  44. // Render the editor IFRAME.
  45. $Html .= "<iframe id=\"{$this->InstanceName}___Frame\" src=\"{$Link}\" width=\"{$this->Width}\" height=\"{$this->Height}\" frameborder=\"0\" scrolling=\"no\"></iframe>" ;
  46. }
  47. else
  48. {
  49. if ( strpos( $this->Width, '%' ) === false )
  50. $WidthCSS = $this->Width . 'px' ;
  51. else
  52. $WidthCSS = $this->Width ;
  53. if ( strpos( $this->Height, '%' ) === false )
  54. $HeightCSS = $this->Height . 'px' ;
  55. else
  56. $HeightCSS = $this->Height ;
  57. $Html .= "<textarea name=\"{$this->InstanceName}\" rows=\"4\" cols=\"40\" style=\"width: {$WidthCSS}; height: {$HeightCSS}\">{$HtmlValue}</textarea>" ;
  58. }
  59. $Html .= '</div>' ;
  60. return $Html ;
  61. }
  62. function IsCompatible()
  63. {
  64. global $HTTP_USER_AGENT ;
  65. if ( isset( $HTTP_USER_AGENT ) )
  66. $sAgent = $HTTP_USER_AGENT ;
  67. else
  68. $sAgent = $_SERVER['HTTP_USER_AGENT'] ;
  69. if ( strpos($sAgent, 'MSIE') !== false && strpos($sAgent, 'mac') === false && strpos($sAgent, 'Opera') === false )
  70. {
  71. $iVersion = (float)substr($sAgent, strpos($sAgent, 'MSIE') + 5, 3) ;
  72. return ($iVersion >= 5.5) ;
  73. }
  74. else if ( strpos($sAgent, 'Gecko/') !== false )
  75. {
  76. $iVersion = (int)substr($sAgent, strpos($sAgent, 'Gecko/') + 6, 8) ;
  77. return ($iVersion >= 20030210) ;
  78. }
  79. else
  80. return false ;
  81. }
  82. function GetConfigFieldString()
  83. {
  84. $sParams = '' ;
  85. $bFirst = true ;
  86. foreach ( $this->Config as $sKey => $sValue )
  87. {
  88. if ( $bFirst == false )
  89. $sParams .= '&amp;' ;
  90. else
  91. $bFirst = false ;
  92. if ( $sValue === true )
  93. $sParams .= $this->EncodeConfig( $sKey ) . '=true' ;
  94. else if ( $sValue === false )
  95. $sParams .= $this->EncodeConfig( $sKey ) . '=false' ;
  96. else
  97. $sParams .= $this->EncodeConfig( $sKey ) . '=' . $this->EncodeConfig( $sValue ) ;
  98. }
  99. return $sParams ;
  100. }
  101. function EncodeConfig( $valueToEncode )
  102. {
  103. $chars = array(
  104. '&' => '%26',
  105. '=' => '%3D',
  106. '"' => '%22' ) ;
  107. return strtr( $valueToEncode, $chars ) ;
  108. }
  109. }
  110. ?>

 

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

 

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

 

  1. /**
  2. * 渲染之前调用
  3. *
  4. * @param QView_Render_PHP
  5. */
  6. protected function _before_render($response)
  7. {
  8. $x = Helper_X::instance($response->getVar('_BASE_DIR'));
  9. $response->assign('x',$x);
  10. }

 

 

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

  1. <?php
  2. // 指示该视图从 _layouts/default_layout 继承
  3. $this->_extends('_layouts/default_layout');
  4. ?>
  5. <?php
  6. // 定义一个名为 contents 的区块
  7. $this->_block('contents');
  8. ?>
  9. <?php
  10. $x->js_include_tag("xx",'jq','bbs');
  11. $x->js_include_ucren();
  12. $x->js_text("
  13. Ucren.onReady(function(){
  14. Ucren.alert('Ucren');
  15. });
  16. ");
  17. $x->stylesheet_link_tag("xx",'jq','style');
  18. $x->css_text("body{color:red;}");
  19. ?>
  20. <img src="<?php $x->img_url('qeephp.jpg'); ?>" />
  21. <form name="form_user" id="form_user"
  22. action="<?php echo url('admin::city'); ?>" method="post">
  23. <fieldset>
  24. <p>
  25. <label for="username">用户名</label>
  26. <input type="text" name="username" id="username" />
  27. </p>
  28. <p>
  29. <label for="password">密码</label>
  30. <input type="password" name="password" id="password" />
  31. </p>
  32. <p>
  33. <label for="body">描述</label>
  34. <?php
  35. $fck_body = $x->call_fck(null,'TIC_MIN','body','',"98%",'100');
  36. echo $fck_body ;
  37. ?>
  38. </p>
  39. <p>
  40. <input type="submit" name="Submit" value="提交" />
  41. </p>
  42. </fieldset>
  43. </form>
  44. <?php
  45. // 区块定义结束
  46. $this->_endblock();
  47. ?>

 

运行后页面源代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  6. <title>QeePHP: 新一代的敏捷开发框架</title>
  7. <link rel="stylesheet" type="text/css" href="/xutic/css/style.css">
  8. </head>
  9. <body>
  10. <div id="page">
  11. <div id="sidebar">
  12. <ul id="sidebar-items">
  13. <li>
  14. <form id="search" action="http://www.google.com/search" method="get" target="_blank">
  15. <input type="hidden" name="hl" value="en" />
  16. <input type="text" id="search-text" name="q" value="site:qeephp.org " />
  17. <input type="submit" value="搜索" />
  18. QeePHP 网站
  19. </form>
  20. </li>
  21. <li>
  22. <h3>加入社区</h3>
  23. <ul class="links">
  24. <li><a href="http://qeephp.com/" target="_blank">QeePHP 官方网站</a></li>
  25. <li><a href="http://qeephp.com/bbs/" target="_blank">论坛</a></li>
  26. <li><a href="http://code.google.com/p/qeephp/issues/list" target="_blank">Bug 报告</a></li>
  27. </ul>
  28. </li>
  29. <li>
  30. <h3>浏览文档</h3>
  31. <ul class="links">
  32. <li><a href="http://qeephp.org/docs/qeephp/api/" target="_blank">QeePHP API 文档</a></li>
  33. <li><a href="http://www.php.net/docs.php" target="_blank">PHP 文档</a></li>
  34. </ul>
  35. </li>
  36. </ul>
  37. </div>
  38. <div id="content">
  39. <script type="text/javascript" src="/xutic/js/xx.js?1260961596"></script>
  40. <script>/* {File not found : /xutic/js/jq.js */</script>
  41. <script>/* {File not found : /xutic/js/bbs.js */</script>
  42. <script type="text/javascript" src="/xutic/js/engine/boot.js?1257408552&skin=qq"></script>
  43. <script>
  44. Ucren.onReady(function(){
  45. Ucren.alert('Ucren');
  46. });
  47. </script>
  48. <style>/* {File not found : /xutic/css/xx.css */</style>
  49. <style>/* {File not found : /xutic/css/jq.css */</style>
  50. <link rel="stylesheet" href="/xutic/css/style.css?1260857742" type="text/css" media="screen" />
  51. <style>body{color:red;}</style>
  52. <img src="/xutic/img/qeephp.jpg" />
  53. <form name="form_user" id="form_user"
  54. action="/xutic/index.php/admin/city" method="post">
  55. <fieldset>
  56. <p>
  57. <label for="username">用户名</label>
  58. <input type="text" name="username" id="username" />
  59. </p>
  60. <p>
  61. <label for="password">密码</label>
  62. <input type="password" name="password" id="password" />
  63. </p>
  64. <p>
  65. <label for="body">描述</label>
  66. <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>
  67. <p>
  68. <input type="submit" name="Submit" value="提交" />
  69. </p>
  70. </fieldset>
  71. </form>
  72. </div>
  73. <div id="footer">
  74. <p>
  75. Powered by <a href="http://qeephp.com/" target="_blank">QeePHP 2.1</a>
  76. |
  77. <a href="http://www.qeeyuan.com/" target="_blank">起源科技</a>
  78. </p>
  79. </div>
  80. </div>
  81. </body>
  82. </html>

 

 

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

 

 

  • 大小: 30.4 KB
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/369339
推荐阅读
相关标签
  

闽ICP备14008679号