Yii Framework Forum: beforeRender and afterRender - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

beforeRender and afterRender

#21 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 29 January 2013 - 03:06 PM

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

#22 User is offline   clapas 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 21-February 11

Posted 01 April 2013 - 01:08 PM

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

#23 User is offline   rAWTAZ 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 102
  • Joined: 08-January 10

Posted 01 April 2013 - 01:23 PM

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

#24 User is offline   clapas 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 21-February 11

Posted 03 April 2013 - 05:52 AM

View PostrAWTAZ, on 01 April 2013 - 01:23 PM, said:

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

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

View PostrAWTAZ, on 01 April 2013 - 01:23 PM, said:

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

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

#25 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 15 April 2013 - 03:48 AM

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.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#26 User is offline   Yang He 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 08-January 12

Posted 10 May 2014 - 12:51 PM

+ 1

View Posttwisted1919, on 29 June 2012 - 04:26 AM, said:

I use afterRender() allot, mainly to register scripts and css files for the current controller/action.
This happens mainly because in the current Yii, you need to register most of the JS/CSS files from within the layout file(it has no use to do it in controller when you use themes because various themes use various assets), then, if you register new files from within the controller action will screw up the order of the scripts being rendered, and i found that the solution to avoid this issue is to register the scripts in afterRender().
Another solution for my problem would be to create a general behavior that would read a config file from the theme and will register the assets found in that file, but it seems overkill for this.
Anyway, bottom line, do not remove these methods, they are useful.

0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • 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