yiiext / twig-renderer
为 Yii 框架提供的 Twig 渲染器。
dev-master
2018-01-18 14:55 UTC
Requires
- php: >=5.1.0
- twig/twig: 1.*
This package is auto-updated.
Last update: 2024-09-05 18:54:59 UTC
README
此扩展允许您在 Yii 中使用 Twig 模板。
资源
要求
- Yii 1.0 或更高版本
安装
手动安装
- 在
vendor/Twig
目录下提取发行文件。 - 从
fabpot-Twig-______.zip\fabpot-Twig-______\lib\Twig\
下载并提取所有 Twig 文件到protected/vendor/Twig
。 - 将以下内容添加到配置文件 'components' 部分
<?php 'viewRenderer' => array( 'class' => 'ext.ETwigViewRenderer', // All parameters below are optional, change them to your needs 'fileExtension' => '.twig', 'options' => array( 'autoescape' => true, ), 'extensions' => array( 'My_Twig_Extension', ), 'globals' => array( 'html' => 'CHtml' ), 'functions' => array( 'rot13' => 'str_rot13', ), 'filters' => array( 'jencode' => 'CJSON::encode', ), // Change template syntax to Smarty-like (not recommended) 'lexerOptions' => array( 'tag_comment' => array('{*', '*}'), 'tag_block' => array('{', '}'), 'tag_variable' => array('{$', '}') ), ),
通过 Composer 安装
- 获取 Composer
- 如果不存在,创建文件 protected/composer.json
{ "repositories":[ { "type":"package", "package":{ "name":"yiiext/twig-renderer", "version":"1.1.15", "source":{ "type":"git", "url":"https://github.com/yiiext/twig-renderer", "reference":"v1.1.15" } } } ], "require":{ "php":">=5.3.0", "yiisoft/yii": "dev-master", "twig/twig": "1.*", "yiiext/twig-renderer":"1.1.*" } }
- 在您的应用 protected 目录下运行
composer update
- 将以下内容添加到配置文件 'components' 部分
<?php 'viewRenderer' => array( 'class' => 'application.vendor.yiiext.twig-renderer.ETwigViewRenderer', 'twigPathAlias' => 'application.vendor.twig.twig.lib.Twig', // All parameters below are optional, change them to your needs 'fileExtension' => '.twig', 'options' => array( 'autoescape' => true, ), 'extensions' => array( 'My_Twig_Extension', ), 'globals' => array( 'html' => 'CHtml' ), 'functions' => array( 'rot13' => 'str_rot13', ), 'filters' => array( 'jencode' => 'CJSON::encode', ), // Change template syntax to Smarty-like (not recommended) 'lexerOptions' => array( 'tag_comment' => array('{*', '*}'), 'tag_block' => array('{', '}'), 'tag_variable' => array('{$', '}') ), ),
用法
- 见 Twig 语法。
- 当前控制器属性可通过 {{this.pageTitle}} 访问。
- Yii::app() 对象可通过 {{App}} 访问。
- Yii 的核心静态类(例如,CHtml)可通过 {{C.ClassNameWithoutFirstC.Method}} 访问(例如:{{C.Html.textField(name,'value')}})
- 要调用返回非字符串结果的函数或方法,请使用 'void' 函数包装这些调用:{{void(App.clientScript.registerScriptFile(...))}}
小部件使用示例
<div id="mainmenu"> {{ this.widget('zii.widgets.CMenu',{ 'items':[ {'label':'Home', 'url':['/site/index']}, {'label':'About', 'url':{0:'/site/page', 'view':'about'} }, {'label':'Contact', 'url':['/site/contact']}, {'label':'Login', 'url':['/site/login'], 'visible':App.user.isGuest}, {'label':'Logout ('~App.user.name~')', 'url':['/site/logout'], 'visible':not App.user.isGuest} ] }, true) }} </div><!-- mainmenu -->