beforeRender and afterRender

Dear Experts

I like the afterRender method, since parameter $output(the content rendered) is passed as referrence.

Inside this method,We can modify the content rendered.

seeni.

I raise the afterRender event in my controllers so an attached behavior can subscribe to the event and output view content or register CSS or Javascript code.

I wouldn’t be able to do that by using the beforeAction or afterAction events. Would I?

@clapas You can probably start output buffering in beforeAction and fetch it in afterAction and thereby process the output, but it’s hackier than having the beforeRender/afterRender ones where you get the output.

There’s also a difference; There might be actions that doesn’t render things in the common way and in these cases you might not want your beforeAction/afterAction to apply, hence being able to use beforeRender/afterRender in these cases might be good.

I’m thinking there are enough use cases to preserve these events after all.

I wonder if buffering output could interfere with nested output buffering, e.g. I think Yii clips use output buffering. May be more Yii methods use output buffering during the render phase, not sure…

Sorry, I don’t understand what you mean here. Could you explain please?

In this post, zaccaria explains one of reasons for using afterRender().

I was thinking about using beforeRender or afterRender to insert some extra content to larger group of my views (not all of them, but a significant number, among different controllers). But this path turned out to be wrong. I think behaviors will be better.

  • 1

+beforeRender

If you want to run all the logic but cancel the output, beforeRender is may the right method

+afterRender

If you want to add/modify/remove html code for all actions

Rest in Peace, dear topic ;)