Named Blocks In Views

Hi all, I am trying out Yii for a new application.

I would like to know if I can named a block of HTML contents in the layout. Then, in the view script which extends that layout, I can either overwrite or extend that block of contents.

E.g.

In protected/views/layouts/main.php, I enclosed and named a block of HTML contents as ‘left’.

In protected/views/layouts/user/index.php, I extended the main layout. I can then overwrite/append/prepend to the contents inside ‘left’.

I’m not sure if there is any existing feature like that in Yii.

This feature will be similar to the Block feature found in Twig and ContentPlaceHolder feature found in .NET.

I came across Yii’s clip and I’m not sure if it can achieve what I want.

Hi kaiming

Welcome to forum!

Yes, clips together with CContentDecorator, widgets and partial views (renderPartial() method) are what you use to render/manipulate output (in addition to plain layouts+views).

What do you mean by ‘extend’? You use the same term for view files rendered using some layout and for your actual question. Thus does this term has the same meaning in your question? In other words, could you please give small example of what you mean to get more clear understanding.

[size=“2”]I’ll try to outline shortly common yii practices (these descriptions aren’t full or perfect):[/size]

  • [size="2"]Clips are mostly used when you prepare output ahead to be inserted in any needed place(s).[/size]
  • [size=“2”]Content decorator is useful when you have the same ‘frame’ for different outputs.[/size]
  • [size="2"]Partial views are useful when you have repeated pieces of html code. You put such piece into one file and include it where you need it. You may also dynamically change these views, passing variables to them.[/size]
  • [size="2"]Widgets are useful when you use some logic also to render a specific piece of html or need to add some scripts or need to have similar rendering for different types of data.[/size]

Even if there’s not an exact approach you’re used to, usually you may get what you need using described possibilities.

Yuga