On Sun, Mar 28, 2010 at 11:26 AM, Yifan <[email protected]> wrote: > Hi, > > I want to know if it is best practice to include some result returned > by other controllers in a template. > > For instance, I have a 'movie' controller and 'view' action, and in > the movie/view.mako template, I want to include the list of all > comments. I can, however, use <%include file='/comment/list.mako' /> > in the template. But this requires extra code querying the database > for comments in the movie\view action, which may be duplicate with the > code in comment\list action. > > Therefore, I want to directly include the return value of the comment > \list action in the movie/view.mako template, like: > <% > import myproj.controllers.comment import CommentController > %> > ${CommentController().list()} > > Is this a good idea, or there is other options?
I would use 'self' rather than instantiating the controller anew. Pylons may set instance attributes in the controller, and future versions will probably to more of this. You can, for instance, do one of these: c.comments = self.list() c.controller = self c.list_action = self.list However, if you're concern is duplicated controller code, you should probably factor that code into a utility method and call it from both actions. If your concern is repeating a query, then you'd have to refactor your code further. -- Mike Orr <[email protected]> -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
