New View and Layout system

I see in the View engine "twig" many interesting approaches for more flexibility and expandability. Three of them are e.g.:

1st Extension of the layouts by "extend"

2nd Defining blocks by "block". Thus one can determine at any time in each view, what should appear in which the layout block. You can maintain predefined content of the block as "parent" and extend or override.

3rd No PHP in your views and layouts.

I think the flexibility and expandability of the Views is one of the very important points. Are there already implementations for Yii 2.0 intended for?

We aren’t going to use any template engine by default. How would syntax for your suggestions look in terms of native Yii views?

Before i write some solutions, i want to explain the current problem now.

We have three parts in the Viewmanagement:

  1. Layout => is the "master" view

  2. The view of the controller

  3. Partials

Yii renders first the controller view then the layout. Imagine i have in the layout view a dynamic sidebar and i would like to add in diffrent controller views a diffrent content to this sidebar. I could solve this with clip-widgets in yii. but its not soo flexible like blocks in twig. and also we can not extend layout views in a more flexibel way.

For yii, I would suggest this approaches:

  1. a nativ integration of a view engine like twig. we could also make it as a package. one problem i see is, how to solve the e.g. CHtml-Helpers in this view engine. One solution could be to convert or parse the view-commend in this view engine in the nativ php-helper code. e.g:

<?= CHtml::textField(‘mytext’, ‘value’, array(‘id’ => ‘xy’, ‘class’ => ‘inputfield’)); ?>

could be in the twig view:

{{ chtml.textField ‘mytext’ ‘value’ [‘id’: ‘xy’, ‘class’: ‘inputfield’] }}

(we could also solve this way namespace in classes)

  1. We continue with the previous solution, and make the view part more flexibel with native php. what i miss at the moment is to make flexibel blocks and extends of views like in twig in the current view solution of yii.

the way could be in the view:




<? $this->view->extend('mainlayout'); ?>


  <? $this->view->beginBlock('theblockname'); ?>

    <? $this->view->block->parentContent(); // puts the content of "theblockname" in mainlayout here ?>

    ... here comes the content of the this view...

  <? $this->view->endBlock; ?>


  <? $this->view->beginBlock('sidebar'); ?>

    ... here comes the content of the this view for the sidebar

  <? $this->view->endBlock; ?>



in mainlayout-layout we could define the blocks like this:




<!doctype html>

  <head>...</head>

  <body>

    <div class="maincontent">

      <? $this->view->initBlock('theblockname'); ?>

        ... here comes the content ...

      <? $this->view->endBlock(); ?>   

    </div>


    <div class="sidebar">

      <? $this->view->initBlock('sidebar'); ?> 

    </div>

  </body>

</html>



i imagine something like this.

I agree, that there should not be any template engine by default. A lot of people are using native php in views.

Mmmm I use renderPartial for similar purpose or clips (introduced in 1.1.8)




<?php $this->beginClip('hello')?>

<p>Hello, {username}!</p>

<?php $this->endClip() ?>

 

<?php $this->renderClip('hello',array(

    '{username}'=>'Qiang',

))?>

<?php $this->renderClip('hello',array(

    '{username}'=>'Alex',

))?>



Your controller can define his Clip. You can also extend yii with your own clips.