yii2 默认使用PHP 和html 混合的方式来写视图层,但我个人还是喜欢纯模板语言的方式。而且已经非常习惯使用twig的语法,最近想使用yii2进行开发,所以还是选择使用twig视图引擎。

github 已经有人提供了这样的vendor ,可以直接comoser 配置一下进行使用。

composer.json 文件 require 添加 "yiisoft/yii2-twig": "*" 然后 composer update

   前往common/config 下 main.php 加上配置

[    'components' =>
[ 'view' => [
'class' => 'yii\web\View',
'renderers' => [
'tpl' => [
'class' => 'yii\smarty\ViewRenderer',
//'cachePath' => '@runtime/Smarty/cache',
],
'twig' => [
'class' => 'yii\twig\ViewRenderer',
'cachePath' => '@runtime/Twig/cache',
// Array of twig options:
'options' => [
'auto_reload' => true,
],
'globals' => ['html' => '\yii\helpers\Html'],
'uses' => ['yii\bootstrap'],
],
// ...
],
],
],]

    其中tpl 的配置是smarty 引擎。如果不使用smarty 可以不配置。接下来就可以去控制器下使用了。

return $this->render('test.twig', ['test' => 'hello,yii']);

   github地址:https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-template-engines.md

yii2 使用twig 模板引擎的更多相关文章

  1. opencart 引入 TWIG 模板引擎

     1.首先将 twig 包放入 system\library 目录. 2.在 system/startup.php 文件最后添加引入语句. require_once(DIR_SYSTEM . 'lib ...

  2. 构建自己的PHP框架(Twig模板引擎)

    完整项目地址:https://github.com/Evai/Aier Twig 模板引擎 模版引擎 twig 的模板就是普通的文本文件,也不需要特别的扩展名,.html .htm .twig 都可以 ...

  3. SSTI(以Twig模板引擎为例)

    一.模板注入与常见Web注入 就注入类型的漏洞来说,常见 Web 注入有:SQL 注入,XSS 注入,XPATH 注入,XML 注入,代码注入,命令注入等等.注入漏洞的实质是服务端接受了用户的输入,未 ...

  4. twig 模板引擎使渲染视图更加优雅

    在使用 laravel 的时候接触过 blade 模板引擎.在学习的时候,接触到了另外一个强大的模板引擎:twig 官网:https://twig.sensiolabs.org/ 中文手册:http: ...

  5. 原生twig模板引擎详解(安装使用)

    最近在学习SSTI(服务器模板注入),所以在此总结一下 0x00 Twig的介绍 什么是Twig? Twig是一款灵活.快速.安全的PHP模板引擎. Twig的特点? 快速:Twig将模板编译为纯粹的 ...

  6. symfony2 twig模板引擎

    1.基本语法 Says something:{{    }} Does something:{%  %} Comment something:{#    #} {% extends "App ...

  7. laravel Blade 模板引擎

    与视图文件紧密关联的就是模板代码,我们在视图文件中通过模板代码和 HTML 代码结合实现视图的渲染.和很多其他后端语言不同,PHP 本身就可以当做模板语言来使用,但是这种方式有很多缺点,比如安全上的隐 ...

  8. thinkphp5.1 - twig模板-全局变量

    thinkphp5.1 - twig模板-全局变量我们在定义 ccs 之类的静态文件的时候,经常会使用<link rel="stylesheet" href="__ ...

  9. php模板引擎

    http://baike.baidu.com/link?url=HmXfdJBv3zpCdnZPeaSmZmqDBHlyTBnz9Rmb5it-jf1_NLHfaku6_i8ssUYbnaTQEBD4 ...

随机推荐

  1. (转)PowerDesigner提示Existence of index、key、reference错误

    建立一个表后,为何检测出现Existence of index的警告    A table should contain at least one column, one index, one key ...

  2. (40) Aeroo 服务安装

    服务器 Odoo 8.0 操作系统: Ubuntu trusty14.04 说明:安装aeroo 要的要两个服务 areoo-docs 和 soffice 这里设定两个端口 8989 和 8100 = ...

  3. 鼠标滚轮控制侧边div上下翻动效果

    css部分: <style> * { margin: 0; padding: 0;} .wrap { width: 1000px; margin: 0 auto; overflow: hi ...

  4. pc, 手机全屏

    全屏 1  div{ position:absolute/relative/fixed; top:0; bottom:0; left:0; right:0;} 2 <!doctype html& ...

  5. android 中theme样式的解释

    android:theme="@android:style/Theme.Dialog" : Activity显示为对话框模式 android:theme="@androi ...

  6. 八款你不得不知的开源前端JS框架

    angular.js Angular.JS是一个开源的JavaScript框架,最适于开发客户端的单页面应用.它实现了前端MVC架构,专注于扩展HTML功能,提供动态数据绑定(Data Binding ...

  7. ocp 1Z0-051 141题---感觉有问题

    141. View the Exhibit and examine the structure of CUSTOMERS and GRADES tables. You need to display ...

  8. [最短路径SPFA] POJ 1847 Tram

    Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...

  9. Rhel6-sersync配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主服务器: 192.168.122.160 server60.example.com 目标服务器: 1 ...

  10. css3隔行变换色实现示例

    <style>#list1 li:nth-of-type(odd){ background:#00ccff;}/*奇数行*/ #list1 li:nth-of-type(even){ ba ...