Twig Additional Syntax

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

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

Twig is going to be used as default templating engine ?

No.

I was working on something similar a while back.

Would need to be more secure, probably an array of allowed functions etc. but would make it more dynamic.