Twig with Yii2 - Snippet Time!

After being AWOL for some time, I am now back to working with Yii - and Yii2 takes a while getting used to.

The documentation is still sketchy, and I have a feeling that Yii2 is not widely used yet?

I am trying to use the Twig renderer with Yii2, and it delights me to learn that it is part of the core.

However, it doesn’t seem to be well-documented, nor as full-featured as the default template engine.

I figured out how to use Twig for both views and layouts after some trial and error…

The forum isn’t of much help because I can’t find anyone either using Twig or having problems with it :)

So, are you guys using Twig with Yii2?

If so, do you have any code/tips/tricks to share? :)

I am particularly interested in how to get at and for conditional rendering…

But any example really would help.

Otherwise I should probably start creating a subclass of it and start hacking?

I figured it out. Turned out to be a matter of asking a question - and voilá: the answer comes to you… ;)

This is a twig version of the main layout:

{{ use('yii/helpers/Html') }}

{{ use('yii/bootstrap/Nav') }}

{{ use('yii/bootstrap/NavBar') }}

{{ use('frontend/assets/AppAsset') }}

{{ use('frontend\\widgets\\Alert') }}

{{ use('yii\\widgets\\Breadcrumbs') }}

{{ register_app_asset() }}

{{ this.beginPage() | raw}}

<!DOCTYPE html>

<html lang="{{app.language}}">


	<meta charset="{{app.charset}}"/>

	<meta name="viewport" content="width=device-width, initial-scale=1">

	{{ html.csrfMetaTags() | raw }}

	<title>{{ html.encode(this.title) | raw }}</title>

	{{ this.head()  | raw }}



	{{ this.beginBody() | raw }}

	<div class="wrap">

	{{ nav_bar_begin({

    	'brandLabel': 'Bugitor',

    	'options' : {

        	'class': 'navbar-inverse navbar-fixed-top',



	{% if app.user().isGuest() %} 

    	{% set items = [

            	{'label': 'Home', 'url': '/site/index'},

            	{'label': 'About', 'url': '/site/about'},

            	{'label': 'Contact', 'url': '/site/contact'},

            	{'label': 'Signup', 'url': '/site/signup'},

            	{'label': 'Login', 'url': '/site/login'}



	{% else %} 

	{% set logoutTxt = ['Logout (', app.user().identity().username ]|join('') %}

	{% set logoutTxt = [logoutTxt, ')']|join('') %}

    	{% set items = [

            	{'label': 'Home', 'url': '/site/index'},

            	{'label': 'About', 'url': '/site/about'},

            	{'label': 'Contact', 'url': '/site/contact'},

            	{'label': logoutTxt, 'url': '/site/logout', 'linkOptions': {'data-method': 'post'}}



	{% endif %} 

	{{ nav_widget({

    	'options' : {

        	'class': 'navbar-nav navbar-right',


    	'items': items

	}) }}

	{{ nav_bar_end() }}

   	<div class="container">

    	{{ breadcrumbs_widget({

    	'links': this.params['breadcrumbs'],

    	}) }}

    	{{ alert_widget() }}

    	{{ content  | raw}}



	<footer class="footer">

    	<div class="container">

    	<p class="pull-left">&copy; My Company 2015</p>

    	<p class="pull-right">{{Yii.powered()  | raw}}</p>



	{{this.endBody() | raw}}



{{this.endPage() | raw}}

If anyone have any Twig snippets to share, just dump them into this topic.

Captcha form field:

        	{{ form.field(model, 


        	         	{'template': '<div class="row"><div class="col-lg-3">{image}</div>

        	         	<div class="col-lg-6">{input}</div></div>'})

         	| raw }}

1 Like


{{ set(this, 'title', 'About') }}

{{ set(this, 'params', { 'breadcrumbs' : { '' : this.title } }) }}

the post is very nice and very good, the captcha of twig that i find very long time