• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

yii框架php用twig模板引擎

武飞扬头像
PHP中文网
帮助5

学新通技术网

yii2 默认使用PHP 和html 混合的方式来写视图层,如果您已经非常习惯使用twig的语法,可以选择使用twig视图引擎。

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

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']);

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tangbicab
系列文章
更多 icon
同类精品
更多 icon
继续加载