Yii Framework Forum: New View and Layout system - Yii Framework Forum

Jump to content

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

New View and Layout system Make Views and Layouts more flexible with extends and blocks

#1 User is offline   Cihan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 02-November 10

Posted 25 December 2011 - 03:59 AM

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?
0

#2 User is offline   samdark 

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

Posted 25 December 2011 - 08:52 AM

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

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

#3 User is offline   Cihan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 02-November 10

Posted 25 December 2011 - 09:24 AM

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)

2. 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.
0

#4 User is offline   Tipugin 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 120
  • Joined: 14-June 10
  • Location:Ivanovo, Russia

Posted 25 December 2011 - 10:49 AM

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

#5 User is offline   sensorario 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,987
  • Joined: 07-September 10
  • Location:Cesena (Italy)

Posted 26 December 2011 - 04:53 PM

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.
1

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