在yii框架中用法php模板引擎twig的例子__第1页
在yii框架中用法php模板引擎twig的例子__第2页
在yii框架中用法php模板引擎twig的例子__第3页
在yii框架中用法php模板引擎twig的例子__第4页
在yii框架中用法php模板引擎twig的例子__第5页
免费预览已结束,剩余3页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、在yii框架中用法php模板引擎twig的例子_ twig是一款快速、平安、敏捷的php模板引擎,它内置了很多filter和tags,并且支持模板继承,能让你用最简洁的代码来描述你的模板。他的语法和python下的模板引擎jinjia以及django的模板语法都特别像。 比如我们在php中需要输出变量并且将其进行转义时,语法比较累赘: 代码如下: ?php echo $var ? ?php echo htmlspecialchars($var, ent_quotes, utf-8) ? 但是在twig中可以这样写: 代码如下: var var|escape var|e # shortcut t

2、o escape a variable # 遍历数组: 代码如下: % for user in users % * % else % no user has been found. % endfor % 但是要在yii framework集成twig就会遇到点麻烦了,官方网站中已经有能够集成twig的方案,所以这里我也不再赘述。但是由于twig中是不支持php语法的,所以在有些表达上会遇到困难,比如我们在写form的视图时,常常会这么写: 代码如下: ?php $form=$this-beginwidget(cactiveform); ? spanlogin/span ul

3、 li ?php echo $form-label($model,username); ? ?php echo $form-textfield($model,username); ? /li li ?php echo $form-label($model,password); ? ?php echo $form-passwordfield($model,password); ? /li li class=last button type=submitlogin/button /li /ul ?php echo $form-error($model,password); ? ?php $this

4、-endwidget(); ? 但是这样的语法是没法在twig中表达的,所以想去扩展下twig的功能,让他能够支持我们自定义的widget标签,然后自动解析成我们需要的代码。 总共需要两个类:tokenparser和node,下面挺直上代码: 代码如下: ?php /* * this file is an extension of twig. * * (c) 2021 lfyzjck */ /* * parser widget tag in yii framework * * % beginwidget cactiveform as form % * content of form * % e

5、ndwidget % * */ class yii_widgetblock_tokenparser extends twig_tokenparser /* * parses a token and returns a node. * * param twig_token $token a twig_token instance * * return twig_nodeinterface a twig_nodeinterface instance */ public function parse(twig_token $token) $lineno = $token-getline(); $st

6、ream = $this-parser-getstream(); $name = $stream-expect(twig_token:string_type); if($stream-test(twig_token:punctuation_type) $args = $this-parser-getexpressionparser()-parsehashexpression(); else $args = new twig_node_expression_array(array(), $lineno); $stream-expect(twig_token:name_type); $assign

7、 = $stream-expect(twig_token:name_type); $stream-expect(twig_token:block_end_type); $body = $this-parser-subparse(array($this, decideblockend), true); $stream-expect(twig_token:block_end_type); return new yii_node_widgetblock(array( alias = $name-getvalue(), assign = $assign, ), $body, $args, $linen

8、o, $this-gettag(); /* * gets the tag name associated with this token parser. * * param string the tag name */ public function gettag() return beginwidget; public function decideblockend(twig_token $token) return $token-test(endwidget); class yii_node_widgetblock extends twig_node public function _co

9、nstruct($attrs, twig_nodeinterface $body, twig_node_expression_array $args = null, $lineno, $tag) $attrs = array_merge(array(value = false),$attrs); $nodes = array(args = $args, body = $body); parent:_construct($nodes, $attrs, $lineno,$tag); public function compile(twig_compiler $compiler) $compiler

10、-adddebuginfo($this); $compiler-write($context.$this-getattribute(assign)-getvalue(). = $contextthis-beginwidget(.$this-getattribute(alias).,); $argnode = $this-getnode(args); $compiler-subcompile($argnode) -raw();) -raw(n); $compiler-indent()-subcompile($this-getnode(body); $compiler-raw($contextthis-endwidget();); ? 然后在twig初始化的地方增加我们的语法解析类: 代码如下: $twig-addtokenparser(new yii_widgetblock_tokenparser); 然后我们就可以在twig的模板里这么写了: 代码如下: % beginwidget cactiveform as form % ul li fo

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论