Alternative To Views And Layouts

Hello,

I am looking for a way to leverage all of the native functionality of YII when rendering full page (include html/head/body tags). These pages will change fairly regularly, so I am hoping I can store the views/layouts in a db/table, pull them in with the controller action and apply the layout/views appropriately.

Is there a way to do this?

Many thanks!

You can. Suppose you store your content in a similarly named table, you could do something like this:




$model = Content::model()->findByPk($id);


if ($model !== null) {

    $this->renderText($model->yourContent);

}



Doesn’t render text require a layout to work? Can a layout be set via a model?

By default, renderText() inserts the rendered string into the current controller layout. You can set the layout for the controller by adjusting the layout property of the controller.

So in your controller action, simply set the layout as desired before calling renderText().

Thanks jodev for the repies… I’m going to share some specific examples to illustrate.

Unless I am missing a step, the default functionality is that in the controller, you set the PATH to the layout, anything “echo’d” during the action will be placed into the “content” variable of the layout. render (for view files) and renderText (for code strings) if $returns is set to false, they natively echo out, and are "captured’ for the content variable in the layout.

I would like to define layout in the INIT of the controller or the beforeAction, and set the layout to a code string instead of a PATH. In that code string, I would like to have some sort of token (or $content variable), that could then be replaced wtih anything “echo’d” from the action.

A final unmentioned step, but kind of the whole reason I am exploring this line of thinking…

[list=1][]step 1, using outputfilter for cache, check if page is cached, if yes, extract cached portion, skip step 3 and 4[]step 2, initialize layout and view of page[]step 3, render page (i assume by echoing from the action), including injecting client scripts and styles into their appropriate locations.[]step 4, cache page (from contents echoed in step 3)[]step 5, replace tokens from output cache will realtime information using afterAction[]actually output the entire, fully rendered (and tokens replaced) page.[/list]

Would this work? Is it possible?

thanks again jodev!

Is there a way to output buffer the output (included what is cached by output filter), manipulate the buffer in the afterAction method, and then output the entire buffer?

That would work, and is my larger requirement then having the layout as a code string.

thanks!

Apologies for the late reply. I’m not sure about the answer to your last questions. I’ve been away from Yii for a while, but picking it up again lately. I don’t know enough yet about Yii internal caching system to say what is or is not possible, so I hope someone else can help you find the answer.

Good luck! :)