>
> What's the practical difference between controller based approach and
> views based one? Eg. Django views, and controllers in Pylons? It
> doesn't seem that much different, so why not make all controller
> actions regular functions, instead of class methods? What's the gain
> in this controller approach, if any? :)

Just want to add to what Ben and Shannon have already said by showing some
example code which might illustrate the point better.

You can take a look at a real app's Pylons REST controller that powers
beta.toscawidgets.org here [1]. (Not that other apps are "unreal".. I mean
that it's not a MyFirstBlog tutorial ;)

Notice that:

* The __before__ method (which is executed before any method) performs 
basic authorization and binds the object being exposed, if any, at an
attribute in self. This avoids repeating this code in every controller
action and is much simpler to implement properly than decorators.

* There's no presentation logic there at all. This controller's actions
only query the model or push changes back to it. Queried objects are sent
down to the template (view) where they are displayed. I find the fact that
Django forces you to intermangle presentation logic (date formatting, item
grouping, sorting, etc..) with business logic in a "view" function very
annoying (I've inherited a Django app at work). This is not Django's fault
really but its brain-damaged template system which doesn't allow python
expressions but forces you to use those "tag filters" or whatever they're
called.

The main advantage I see of the controller class approach is that it
allows for easy code reuse using OO principles. For example, in the code I
linked, when I get to write a controller for administering trac instances,
much of this logic (authentication, etc) could be factored to a common
base class quite easily. mixins can also be used to reuse code of common
actions, example:

class PublishableMixin:
    """Adds a publish action for objects that can be published"""
    def publish(self, id):
        # it is assumed that __before__ has
        # fetched obj and bound it at self.obj
        self.obj.publish()
        flash_message("Object %s has been published succesfully" % self.obj)
        redirect_to(url_for(action="index"))

Now, every controller that multiple inherits from this mixin will have a
"publish" action for free without having to write extra regexps at urls.py

Hope this example has made things a little bit clearer to you.

Alberto
[1]
http://beta.toscawidgets.org/trac/twWebSite/browser/twwebsite/controllers/repositories.py



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to pylons-discuss@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to