Yii Framework Forum: Twig Additional Syntax - Yii Framework Forum

Jump to content

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

Twig Additional Syntax

#1 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,428
  • Joined: 17-January 09
  • Location:Russia

Posted 28 April 2013 - 09:49 AM

As you probably know if you're following Yii2 issues, basic support for Twig and Smarty is done. That means basic stuff like assigning variables and rendering works. The issue is that lots of functionality available in PHP templates isn't yet exposed to Twig templates.

I've checked Twig and realized that it can be done in different ways. Since I'm not a Twig user it's hard to decide which way is better.

For example, creating a link. In Yii2 it's:

echo Html::a('Posts, page 2', array('post/index', 'page' => 2));


If we'll use it as a Twig function as is it will be:


{{ a('Posts, page 2', {0: 'post/index', 'page': 2}) | raw }}


Not really clear, right? How would you like to use it?

There are more things to expose:

// I think Twig has it's own, right?
echo Html::encode('content with <html>');

// Model-less form
echo Html::beginForm(array('post/edit', 'id' => 10), 'post', array('enctype' => 'multipart/form-data'));
echo Html::dropDownList('mySelect', 1, array(0 => 'A', 1 => 'B'), array('class' => 'mySelectCSSClass'));
echo Html::activeLabel($postModel, 'title', array('class' => 'postTitleLabel'));
echo Html::endForm();

// URL
echo Html::url(array('post/index', 'page' => 2));

// application property
echo \Yii::$app->language;

// title of the page
echo $this->title;

// view partial
echo $this->render('viewName', array('x' => 'y'));

// using a widget
echo $this->widget('yii\widgets\Something', array('x' => 'y', 'z' => 42));

// ActiveForm
$form = $this->beginWidget('yii\widgets\ActiveForm');
echo $form->field($model, 'username')->textInput();
echo $form->field($model, 'password')->checkboxAlt();
$this->endWidget();

// including assets bundle
$this->registerAssetBundle('jquery');

Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
1

#2 User is offline   Bogdan Savluk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 20-April 10
  • Location:Kiev

Posted 29 April 2013 - 04:51 AM

By now, I am using twig templates with Yii 1.1.

I think integration with twig should be done in sf2 like way:

Html::a
<a href="{{ path('post/index',{'page':2})}}">Posts</a>

Html::encode
{{ 'content with <html>' | e }}
{{ 'content with <html>' | escape }}
{{ 'content with <html>' | escape(context) }}

Application properties, page title:
I am using:
{{ app.language }}
{{ this.title }}

View partial:
Twig has own mechanism for this - just use "include" tag.

Using widgets:
For now I am using:
{{ this.widget('yii\widgets\Something', {'x':'y', 'z':42})|raw }}
or 
{% set someWidget = this.beginWidget('yii\widgets\Something', {'x':'y', 'z':42}) %}
some content 
{{ someWidget.property }}
{% do this.endWidget() %}

Including assets bundle:
I think we can keep it as is:
{% do this.registerAssetBundle('jquery') %} 

About forms:
I like sf2 Form component, and its integration with twig. I would like to see something similar for Yii forms.
But I did not see nothing bad in using ActiveForm widget, as described above.
4

#3 User is offline   xicond 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 06-October 12

Posted 06 May 2013 - 04:12 AM

View PostBogdan Savluk, on 29 April 2013 - 04:51 AM, said:

By now, I am using twig templates with Yii 1.1.

I think integration with twig should be done in sf2 like way:

Html::a
<a href="{{ path('post/index',{'page':2})}}">Posts</a>

Html::encode
{{ 'content with <html>' | e }}
{{ 'content with <html>' | escape }}
{{ 'content with <html>' | escape(context) }}

Application properties, page title:
I am using:
{{ app.language }}
{{ this.title }}

View partial:
Twig has own mechanism for this - just use "include" tag.

Using widgets:
For now I am using:
{{ this.widget('yii\widgets\Something', {'x':'y', 'z':42})|raw }}
or 
{% set someWidget = this.beginWidget('yii\widgets\Something', {'x':'y', 'z':42}) %}
some content 
{{ someWidget.property }}
{% do this.endWidget() %}

Including assets bundle:
I think we can keep it as is:
{% do this.registerAssetBundle('jquery') %} 

About forms:
I like sf2 Form component, and its integration with twig. I would like to see something similar for Yii forms.
But I did not see nothing bad in using ActiveForm widget, as described above.


imagine like emmet syntax
0

#4 User is offline   soee 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 06-July 12

Posted 12 July 2013 - 07:59 AM

Twig is going to be used as default templating engine ?
0

#5 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,428
  • Joined: 17-January 09
  • Location:Russia

Posted 14 July 2013 - 02:08 PM

No.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#6 User is offline   alex-w 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 223
  • Joined: 25-November 10

Posted 15 July 2013 - 03:14 AM

I was working on something similar a while back.
http://stackoverflow...ion-or-variable

Would need to be more secure, probably an array of allowed functions etc. but would make it more dynamic.
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