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

#1 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,892
  • Joined: 04-October 08
  • Location:DC, USA

Posted 28 May 2012 - 08:53 AM

In Yii 1.1, we have beforeRender and afterRender in CController. I'm wondering what do you use them for. Could you please elaborate how these become useful in your project? I need this information to determine whether or not we should support them in 2.0, or perhaps come up with a better solution. Thanks.
0

#2 User is offline   Seal 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 02-February 10

Posted 28 May 2012 - 11:21 AM

Personally, I have not used beforeRender nor afterRender my projects.
Sylvester La-Tunje

Posted Image
0

#3 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 28 May 2012 - 12:29 PM

I`ve used it as part of the communication betweenmodules.



Use case:

I've developed a user module which allows to maintain a user'sprofile. The only user fields this module provides in the 'edit profile' vieware the username, email, name and photo.

I've also developed a 'social' module which uses the afterRender event toadd more fields to the 'Edit profile' view.



Within the 'update' action I've created a new event which is called after the default fields are saved. This allows to save the additional fields


0

#4 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 28 May 2012 - 01:40 PM

Sending customized headers could be another use case, especially when using Yii as an API framework but that could also be done using filters I guess
0

#5 User is offline   samdark 

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

Posted 28 May 2012 - 02:43 PM

Personally I've never used it. That's what I was able to find:

http://www.yiiframew...fied-meta-tags/
http://pastebin.com/mC9qsRMS
http://www.yiiframew...other-approach/
Yii 1.1 Application Development Cookbook

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

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 28 May 2012 - 08:02 PM

I am using it fairly often. :)
That's probably because I come from CakePHP..

What is the alternative?
"Less noise - more signal"
0

#7 User is offline   yugene 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 513
  • Joined: 08-August 09

Posted 28 May 2012 - 11:03 PM

I used only beforeRender() to set meta tags.
0

#8 User is offline   Seal 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 126
  • Joined: 02-February 10

Posted 28 May 2012 - 11:58 PM

I have seen samdark's links. However I am curious. What other use case scenario are you using beforeRender / afterRender() ?


Sylvester La-Tunje

Posted Image
0

#9 User is offline   samdark 

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

Posted 29 May 2012 - 03:25 AM

jacmoe
How exactly?
Yii 1.1 Application Development Cookbook

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

#10 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 29 May 2012 - 04:01 AM

I am using it to perform tasks before the render phase.
It's really convenient.
It's easier than setting up a filter.
However, I can live with it being removed.
It was added in 2010.
And it has some flaws, because render is not guaranteed to be called - renderPartial and a simple echo are examples of that.
So, if it means that I need to add a filter to my base controller, I'm fine with it. ;)
"Less noise - more signal"
0

#11 User is offline   samdark 

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

Posted 29 May 2012 - 05:23 AM

I think it was used because there was no proper beforeAction event, just a method.
Yii 1.1 Application Development Cookbook

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

#12 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 29 May 2012 - 05:46 AM

D'oh - I always mix those two functions up! :)
Yeah, with beforeAction, I really can't think of any use for beforeRender.
"Less noise - more signal"
0

#13 User is offline   rAWTAZ 

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

Posted 27 June 2012 - 11:51 AM

I don't recall having used [before,after]Render(). But I'd like to say the following:

  • I don't mind keeping them. But if it's a huge performance issue (which I doubt) I wouldn't defend them till death. If removed and chaos ensues, they can always be added back later on I presume.

  • Assuming they're kept, please add events for them, so that we can create behaviors that support them. I once reported Issue 2400 where I couldn't do a clean solution (behavior) due to lack of events for these two methods.

  • Out of the three people (aside core devs) involved in that issue, 100% wanted events to be added for these methods. One of the best things about Yii is that it is pretty flexible. A part of that is naturally the event system, but it's useless without events for key methods/actions.

Danke.
0

#14 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 27 June 2012 - 12:21 PM

IMHO I rather use beforeAction than beforeRender... I have never used that to be honest, and I cannot see any good reason to use it instead of other solutions
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#15 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 29 June 2012 - 03:58 AM

beforeRender should go because it is not guaranteed to be called anyway..
It's just confusing and cluttering up the core.
"Less noise - more signal"
0

#16 User is offline   twisted1919 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 640
  • Joined: 23-October 10
  • Location:Romania

Posted 29 June 2012 - 04:26 AM

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

#17 User is offline   rAWTAZ 

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

Posted 29 June 2012 - 04:42 AM

View Postjacmoe, on 29 June 2012 - 03:58 AM, said:

beforeRender should go because it is not guaranteed to be called anyway..
It's just confusing and cluttering up the core.

Can you rephrase that? I'm not sure I understand what you mean. It seems you are saying that unless everyone uses beforeRender() (i.e. it being "guaranteed" to be called under all circumstances) it should be removed. Presumably that is not what you meant :)

I don't see what is confusing about it. beforeAction() is run before the requested action method is called, and beforeRender() is naturally called after the/any render() call in the action, but before the rendering is actually done. Seems straight forward to me.
0

#18 User is offline   elexperimento 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 12-March 11
  • Location:Bogotá, Colombia, South America

Posted 19 November 2012 - 04:56 PM

View Postqiang, on 28 May 2012 - 08:53 AM, said:

In Yii 1.1, we have beforeRender and afterRender in CController. I'm wondering what do you use them for. Could you please elaborate how these become useful in your project? I need this information to determine whether or not we should support them in 2.0, or perhaps come up with a better solution. Thanks.



Hi all,

well I've found an interesting case where I just needed it, but remembered the question you had asked months ago, before relying upon it. However, it's not so trivial to discard it, let me explain: in the flow of work AFTER the massive assignation, we try to save a model, and afterwards either redirect or render, usually depending on whether there was a validation error when trying to save (or reviewing the $model->errors, or the $model->hasErrors(), there are several ways to do it). But let's focus on the situation where there was an error, and one of your model attributes deserves a special attention, which you had already treated when the action begun, but BEFORE the massive assignment.

In such a case, if the user CHANGED the value of such attribute, but the $model->save() method generated an error, then the $POST value containing the new value will either reset the previous treatment, or the new value will be ignored (for example, if the treatment was saved in a variable, independently of the massive assignment).


Such error-proof situation can be solved with the beforeRender method, and is a place reserved for that matter.


There are, however, for the programmer using Yii, some alternative methods to achieve the same result, and the last one, which will be considered obvious after stating it, is not obvious when first facing the problem:

1. Something like defining a callback function according to the case, and then call it with any available data what ever it might be in the moment of the call, might solve the problem

2. Defining a method in the controller, such method will be in charge of the treatment, and drawing the control of the treated data in the view layer, for example in _form.php. The _form.php will include just a one-line statement calling that controller method, so the method will be called already in the rendering. Nonetheless, its simplicity (one line) will not harm to the pure nature of the view layer more than the usual php calling for example the CHtml class static functions.

--------------------------------------------------

Conclusion: although it might be replaced by other ways to solve the problem I met, thinking ahead that it might be eliminated, I discarded its usage and found other solutions which up to the same problem, seem to be as effective and harmless for the view. BUT the cost was that I had to call a controller function in a view file.

---------------------------------------------------
Suggested readings for newcomers in order to understand what I mean: you can read What is massive assignment


I hope it was useful,

best regards from Bogotá, Colombia,

David López
Investigación y Programación SAS
0

#19 User is offline   Imre 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 30-March 11

Posted 06 December 2012 - 10:21 AM

before/afterRender doesnt have much logic in it. If user needs such methods he can easily override the render methods in his own custom Controller.php he then extends.

I am totally for not cluttering code with methods that in reality dont get much use. Sure they make a good impression in Tutorials/Video but in real world we want lean codebase.
0

#20 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 27 January 2013 - 12:38 PM

never used before/afterRender. Don't even see how they are better than before/afterAction
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