On Mar 28, 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 think the usual way to do this is to do a query for the comments in
your movie controller's `view` action:
class MovieController(BaseController):
def view(self, ...):
# stuff
c.comments = Session.query(Comment).all() # or whatever query
makes sense
# more stuff
return render(...)
If you have a complex Comment query that you need often, you might
create a class method in your Comment class, but for a simple query,
I'd just inline it as I did above.
--
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.