Change the default template engine in yii2

Yii

Posted on 03 Aug 15 by Vickey
  1481



Yii use php as the template engine by default but we can change different template engine for example we can use smarty, twig and others
In this post I will use smarty as the template engine with yii. I am using yii2 and i am working on Ubuntu operating System with xampp. so lets start

1. Go to Yii root directory and you will see a file here name as 'composer.json', open this and search the like given below

  "require": {
  
        "php": ">=5.4.0",
        some code here
        some code here
        some code here
        some code here
        
        
    },

now add the below code
      

  "yiisoft/yii2-smarty": "*"


you should see something like this
    

      "require": {
  
        "php": ">=5.4.0",
        some code here,
        some code here,
        some code here,
        some code here,
        
        "yiisoft/yii2-smarty": "*"
        
    },

now go to directory name as  'config' and open web.php file and search text 'components'   and add the below code
    

    'components' => [
    
    
    
            'view' => [
                'renderers' => [
                        'tpl' => [
                        'class' => 'yii\smarty\ViewRenderer',
                    ],
                ],
            ],


 Now open you terminal and locate in yii root folder
                   

cd /opt/lampp/htdocs/yiiroot

 and update composer

sudo /opt/lampp/bin/php /usr/local/bin/composer update

( in window you need different command   'composer update')
            
after installation complete go to your controller and change your render view name
            
 return $this->render('index.tpl');

 now in your views create a file index.tpl and put your smarty code here and open your webpage you see your new tpl file content
            
            
            
            
            
            
            
            
    
    


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com