手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:smarty

Yii2-smarty的一些小坑

在写本文前我不得不说一句,其实我是不想用smarty的,我想尝试一下twig,但是phpstorm的Twig插件真要命,卡成翔,所以我只能用smarty。为什么不用prado了呢?官方说不支持了,我晶啊

在使用smarty的时候官方的代码和例子看上去很美,不过要注意几点

1、用yii2-smarty,还是必须得用layout,如果你不支持layout文件,默认就是/layouts/main.php,天啊,为什么是PHP?而且在这里面也还真的能用PHP代码。整个都崩溃了

2、你可以指定layout文件,比如:main.tpl,OK你必须得象PHP文件一样,得写{$this->head()},{$this->startBody()}{$this->endPage()}等,否则 ClientScript功能就无法使用

3、如果你指定layout=false,那么,就不支持ClientScript了。因为你incude file='xxx.tpl',在每一个独立的文件里都必须要象2中一个个的this->head(),this->endPage全写上

4、再来一个bug:{registerJsFile url=''},这个函数有BUG

原来是:

PHP代码
  1. public function functionRegisterJsFile($params$template)  
  2. {  
  3.     if (!isset($params['url'])) {  
  4.         trigger_error("registerJsFile: missing 'url' parameter");  
  5.     }  
  6.   
  7.     $url = ArrayHelper::remove($params'url');  
  8.     $key = ArrayHelper::remove($params'key', null);  
  9.     $depends = ArrayHelper::remove($params'depends', null);  
  10.     if (isset($params['position']))  
  11.         $params['position'] = $this->getViewConstVal($params['position'], View::POS_END);  
  12.   
  13.     Yii::$app->getView()->registerJsFile($url$depends$params$key);  
  14. }  

改成为:

PHP代码
  1. /** 
  2.  * Smarty function plugin 
  3.  * Usage is the following: 
  4.  * 
  5.  * {registerJsFile url='http://maps.google.com/maps/api/js?sensor=false' position='POS_END'} 
  6.  * 
  7.  * Supported attributes: url, key, depends, position and valid HTML attributes for the script tag. 
  8.  * Refer to Yii documentation for details. 
  9.  * The position attribute is passed as text without the class prefix. 
  10.  * Default is 'POS_END'. 
  11.  * 
  12.  * @param $params 
  13.  * @param \Smarty_Internal_Template $template 
  14.  * @return string 
  15.  * @note Even though this method is public it should not be called directly. 
  16.  */  
  17. public function functionRegisterJsFile($params$template)  
  18. {  
  19.     if (!isset($params['url'])) {  
  20.         trigger_error("registerJsFile: missing 'url' parameter");  
  21.     }  
  22.   
  23.     $url = ArrayHelper::remove($params'url');  
  24.     $key = ArrayHelper::remove($params'key', null);  
  25.     $params['depends'] = ArrayHelper::remove($params'depends', null);  
  26.     if (isset($params['position']))  
  27.         $params['position'] = $this->getViewConstVal($params['position'], View::POS_END);  
  28.   
  29.     Yii::$app->getView()->registerJsFile($url$params$key);  
  30. }  

其实就是$params['depends']这个参数。registerJsFile只能接受3个参数,但事实上用了4个参数,所以调整一下即可

 

Tags: yii2, smarty, twig

using zf and smarty

使用zend framework开发时,可以采用第三方模版,比如smarty,在网上找了很多资料,一般来说是两种

1、扩展view

2、使用Zend_Registry,在初始化的时候加载smarty,然后在输出的时候使用Zend_Registry::get('smarty')->display();

使用第二种方式的话,我当然是没有什么说的了。我这里说的是使用第一种方案。

在第一种方案中,官方有例子,页面地址为:http://framework.zend.com/manual/en/zend.view.scripts.html,写上一个类,调用Zend_View_Interface,写上相同的函数就可以了。。

官方有源码,我这里就不贴了。我把官方的例子写一下:

 

PHP代码
  1. //Example 1. In initView() of initializer.  
  2. $view = new Zend_View_Smarty('/path/to/templates');  
  3. $viewRenderer =  
  4.     new Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');  //使用此例子时,请将new去掉,静态方法不需要new
  5. $viewRenderer->setView($view)  
  6.              ->setViewBasePathSpec($view->_smarty->template_dir)  
  7.              ->setViewScriptPathSpec(':controller/:action.:suffix')  
  8.              ->setViewScriptPathNoControllerSpec(':action.:suffix')  
  9.              ->setViewSuffix('tpl');  
  10.   
  11. //Example 2. Usage in action controller remains the same...  
  12. class FooController extends Zend_Controller_Action  
  13. {  
  14.     public function barAction()  
  15.     {  
  16.         $this->view->book   = 'Zend PHP 5 Certification Study Guide';  
  17.         $this->view->author = 'Davey Shafik and Ben Ramsey'  
  18.     }  
  19. }  
  20.   
  21. //Example 3. Initializing view in action controller  
  22. class FooController extends Zend_Controller_Action  
  23. {  
  24.     public function init()  
  25.     {  
  26.         $this->view   = new Zend_View_Smarty('/path/to/templates');  
  27.         $viewRenderer = $this->_helper->getHelper('viewRenderer');  
  28.         $viewRenderer->setView($this->view)  
  29.                      ->setViewBasePathSpec($view->_smarty->template_dir)  
  30.                      ->setViewScriptPathSpec(':controller/:action.:suffix')  
  31.                      ->setViewScriptPathNoControllerSpec(':action.:suffix')  
  32.                      ->setViewSuffix('tpl');  
  33.     }  

以上是官方的example。不过,如果按第一个例子测试,是会出错的。。。

 

请看第4行。。。静态方法居然用了new。(应该是粗心吧。不过我昨天是直接复制的,死活报错,也没有仔细看,丢人啊)写这篇 文章,主要也就是提醒一下,这个例子有点问题。

顺便说一下,由于官方的例子里,是把$_smarty写成了protected,那么,其实在外面是不能够被直接引用的。要么写一个__get方法,要么,把属性改为public吧

不过,在使用smarty后,你会发现,你原来的layout功能不能完全使用了,为什么呢?因为,在原来的layout里面,代码都是类似于这样:

 

PHP代码
  1. <?php  
  2. $this->layout()->title;  
  3. ?>  

大致是这样的代码,这个,可不能用在smarty中。虽然在smarty中也可以用标签来调用PHP代码,但这毕竟不是一个好办法。

 

不过还好,又有牛人写了一个很牛叉的例子。LOOK:http://anders.tyckr.com/2008/03/12/implementing-zend-layout-and-smarty-using-zend-framework-mvc/,不过这个例子我还没有全部看完,先贴上来。以后慢慢看,应该会用在项目中吧?

不然,我就用不了layout了,除非我放弃这个东西。。哈哈

Tags: zend, framework, smarty

TP1.5版本中使用smarty模版引擎的技巧

从TP1.5开始,对于其他的模版引擎有了原生支持(不再是以前那种插件机制了)。
本文以使用smarty模版为例作点简单介绍,其他的,可以参考一下View.class.php中的fetch方法可知。

TP的SVN中已经含有smarty模版库,因此当你要使用的时候,只需要在项目的config.php里作一点简单的配置:

PHP代码
  1. 'TMPL_ENGINE_TYPE' => 'smarty',    //这个是设置引擎为smarty  


从2009-01-07下午的SVN版本里,流年为又增加了一个TMPL_ENGINE_CONFIG这个数组,即:

PHP代码
  1. 'TMPL_ENGINE_CONFIG'array(  
  2.         'template_dir' => TMPL_PATH , //这个就是tpl目录了  
  3.         'compile_dir'  => CACHE_PATH . "tplCompile/"//这是我自己设定的,模版编译缓存放在这个目录里  
  4.         'cache_dir'    => CACHE_PATH . "tplCache/",   //如果需要生成页面缓存,这个也是必须的  
  5.         'left_delimiter' => '{',  
  6.         'right_delimiter' => '}',  
  7.         'caching' => false,  
  8.         'force_compile' => true,  
  9.         'compile_check' => true,  
  10. ),  


备注:如果按照我这样的写法,请到cache目录里手动创建tplCompile和tplCache两个目录,否则程序会报错。
报错信息大致为:

XML/HTML代码
  1. Catchable fatal error: Object of class Smarty could not be converted to string in D:\local\htdocs\ThinkPHP\Album\Temp\~runtime.php on line 145  


如果出现这样的报错信息,请先检查这两个目录是否存在

在项目开发的时候,caching 最好设为false,否则你根本看不到效果。

如此设定完毕后,你就可以直接在项目中使用了,下面以Index模块的index方法进行举例:在IndexAction.class.php的index的方法里

PHP代码
  1. $this->assign("test" , "This is a test string");  
  2. $this->display();  

然后到模版里:

XML/HTML代码
  1. {$test} 

就可以看到输出了。不过,这里需要注意的是,如果你$this->display()没有指定文件名,那么默认的模版文件就是default/Index/index.html,这点和原先使用TP默认的模版引擎没有什么区别。
出现问题最大的应该是在include方法里,include的使用方法是:{include file="Public/header.html"},就象我前面所说,smarty的模版路径只指到了tpl目录,但实际上,我们是在默认的default目录下操作,因此正确的写法应该是{include file="default/Public/header.html"}
如果我们写的程序要对应多模版,那么,上面那种直接写死default的方法是不行的,还好,TP为我们留了一个常量:TEMPLATE_NAME,于是我们的写法就可以是现在这样:

XML/HTML代码
  1. {include file="`$smarty.const.TEMPLATE_NAME`/Public/header.html"}  

现在试一下,是否公用目录里的header.html被加载了?

最后再报一个warning。如果是在WINDOWS下面开发并且开启了DEBUG_MODE,那么你在读取模版的时候,页面的Trace信息里,偶尔会出现一个注意:

 

XML/HTML代码
  1. [ 09-01-08 14:36:02 ] 注意:[2] unlink(./Cache/tplCompile/\%%70^706^706C3AFE%%index.html.php) [function.unlink]: No such file or directory core.write_file.php 第 44 行.  


这些信息,可以被忽略掉。模版文件还是会正常的生成和编译的。它只会在第一次生成模版编译文件的时候出现

Tags: thinkphp, smarty, template

smarty3即将出来

PHP的模版里,smarty一直就是让人又爱又恨的,功能太强大了,以致于美工们看到他也会感觉头疼,毕竟是又要学一门新的语言了。
这么多年,smarty都是在2.x版本里面徘徊,如今,终于要出3了。
以下是官方的change log:

10/6/2008

- completed compilation of {include} tag, variable scoping same as in smarty2

- added compilation of {capture} tag.

- added error message on none existing template files.

10/4/2008
- added comments in the parser definition y file.

- added array of $smarty_token_names to lexer for use in trigger_template_error function

- change handling of none smarty2 style tags like {if}, {for}....

- lexer/parser optimization

10/3/2008

- create different compiled template files depending if caching and/or security is enabled

- cleaned up compiler

- lexer/parser optimization

10/2/2008

- new method trigger fatal error, displays massage and terminates smarty

- compile error status flag added
The compiler will in case of error continue to parse the template(s) to display all errors. No compiled
templates will be written, Smarty terminates after the compiler exits.

- added error message to function __call in Smarty.class.php

Tags: smarty, version, php, template, smarttemplate

smarty中的注释

写代码的时候不可避免的会使用到注释。大多数的情况下,我们都是使用<!-- 这里是注释 -->,因为这是HTML自带的注释功能,在这里的代码都不会被显示到浏览器。

然而,使用了smarty之类,我们确实不是很建议这样使用,因为,在<!---->标记里的smarty代码其实还是被解析了,如果是这样的话,那么,我们其实是多做了很多事情,却没有被显示出来,那就是说,我们其实多做了很多无用功。

因此,我们在使用smarty模版的时候,应该根据smarty的规范来。让我们看看手册怎么说:


 所有例子中,我们假定你使用缺省的分隔符。Smarty中,所有在分隔符之外的内容被显示为静态内容,或者说不会被改变。一旦Smarty遇见分隔符,它将尝试解释它们,然后在其位置处显示合适的内容。

注释

    模板注释由星号包围,继而由分隔符包围,型如:{* 这是一个注释 *}。Smarty注释不会在最终模板的输出中显示,这点和<!-- HTML comments -->不同。前者对于在模板中插入内部注释有用,因为没有人能看到。;-)

 

模版中的注释
  1. {* 这是Smarty注释,不出现在编译后的输出中 *}  
  2. <html>  
  3. <head>  
  4. <title>{$title}</title>  
  5. </head>  
  6. <body>  
  7.   
  8. {* 另一个单行Smarty注释 *}  
  9. <!-- HTML注释将发送到浏览器 -->  
  10.   
  11. {* 这是一个多行  
  12.    Smarty注释  
  13.    并不发送到浏览器  
  14. *}  
  15.   
  16. {*********************************************************  
  17. 多行注释块,包含了版权信息  
  18.   @ author:         bg@example.com  
  19.   @ maintainer:     support@example.com  
  20.   @ para:           var that sets block style  
  21.   @ css:            the style output  
  22. **********************************************************}  
  23.   
  24. {* 包含了主LOGO和其他东西的头文件 *}  
  25. {include file='header.tpl'}  
  26.   
  27.   
  28. {* 开发注解:$includeFile变量在foo.php脚本中赋值 *}  
  29. <!-- 显示主内容块 -->  
  30. {include file=$includeFile}  
  31.   
  32. {* 该<select>块是多余的 *}  
  33. {*  
  34. <select name="company">  
  35.   {html_options options=$vals selected=$selected_id}  
  36. </select>  
  37. *}  
  38.   
  39. {* 模板的cvs标记。下面的36应该是美元符号。  
  40. 但是在CVS中被转换了。 *}  
  41. {* &#36;Id: Exp &#36; *}  
  42. {* $Id: *}  
  43. </body>  
  44. </html>  

 

Tags: smarty, 注释

Records:712