Yii Framework Forum: Twig with Yii2 - Snippet Time! - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Twig with Yii2 - Snippet Time! Rate Topic: -----

#1 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,698
  • Joined: 10-October 10
  • Location:Denmark

Posted 24 February 2015 - 01:32 PM

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
yii.app.user.isGuest and yii.app.user.identity for conditional rendering..
But any example really would help.

Otherwise I should probably start creating a subclass of it and start hacking?
"Less noise - more signal"
0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,698
  • Joined: 10-October 10
  • Location:Denmark

Posted 24 February 2015 - 01:51 PM

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}}">
<head>
	<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 }}
</head>
<body>
	{{ 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}}
    	</div>
	</div>
	<footer class="footer">
    	<div class="container">
    	<p class="pull-left">&copy; My Company 2015</p>
    	<p class="pull-right">{{Yii.powered()  | raw}}</p>
    	</div>
	</footer>
	{{this.endBody() | raw}}
</body>
</html>
{{this.endPage() | raw}}

This post has been edited by jacmoe: 27 February 2015 - 02:47 PM
Reason for edit: Updated with breadcrumbs

"Less noise - more signal"
0

#3 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,698
  • Joined: 10-October 10
  • Location:Denmark

Posted 24 February 2015 - 02:08 PM

If anyone have any Twig snippets to share, just dump them into this topic.
"Less noise - more signal"
0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,698
  • Joined: 10-October 10
  • Location:Denmark

Posted 27 February 2015 - 01:31 PM

Captcha form field:
        	{{ form.field(model, 
        	         	'verifyCode').widget('yii\\captcha\\Captcha', 
        	         	{'template': '<div class="row"><div class="col-lg-3">{image}</div>
        	         	<div class="col-lg-6">{input}</div></div>'})
         	| raw }}

"Less noise - more signal"
0

#5 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,698
  • Joined: 10-October 10
  • Location:Denmark

Posted 27 February 2015 - 02:17 PM

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

"Less noise - more signal"
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users