Skip to content
alabid edited this page Aug 6, 2012 · 22 revisions

Controllers

According to the MVC specification which whirlwind is based upon, the Controller should handle the business logic. The Controller receives and responds to requests from the client. The user's requests for pages are routed to some subclass of the python class BaseRequest using the @route decorator.

Here's an example implementation of a controller that handles "static page" requests:

from whirlwind.core.request import BaseRequest                                                             
from whirlwind.db.mongo import Mongo                                                                       
from tornado.web import authenticated                                                                      
from whirlwind.view.decorators import route                                                                
                                                                                                        
@route('/')                                                                                                
class IndexHandler(BaseRequest):                                                                           
  def get(self):                                                                                         
    #template context variables go in here                                                             
    template_values = {}                                                                               
    
    # you can do some database manipulation here
    # or not do anything at all
   
    # render the template defined in application/views/site/index.html                                                                                             
    self.render_template('/site/index.html',**template_values)    

The above controller code just handles rendering the root page. This page can be in its own file: sites_controller.py.

We suggest that the Request Handlers (which are subclasses of BaseRequests) be categorized according to functionality. In other words, Request Handlers that handle static requests (that aren't related to any other indepth functions) can be placed together in sites_controller.py in the controllers folder. Handlers that handle requests to login and logout of the application can be placed in an accounts_controller.py. And so on...

BaseRequest

The BaseRequest class is defined in the whirlwind python source. It's a subclass of the RequestHandler class that's part of the tornado python web server.

##Methods of BaseRequest

The BaseRequest class has several methods that are useful in rendering template file and template strings.

The methods explicated below can be invoked in a subclass of BaseRequest using the syntax:

self.baseRequestMethod(params,...)

where baseRequestMethod is the method of the BaseRequest to invoke.


Method : template_exists

Params : template_name - name of template

Returns : true if the template defined at template_name exists. Else returns false.


Method : render_string

Params : template_name - name of template

      **kwargs - dictionary of options to pass to view. For example, `kwargs['me']="hi"` would make the variable `me` available to the view.

Returns : String that's the result of pre-processing the template at template_name against the mako template syntax rules.

Alias : render_to_string


Method : render_template

Params : template_name - name of template to render

          **kwargs - dictionary of options to pass to view. Same as in `render_string`

We suggest that the controllers Request Handlers be classified according to

@threaded

@authenticated

Rendering Templates

The controller sometimes handles requests by rendering a template or by just writing out JSON or plain HTML to the client Since the controller handles client requests, it

Clone this wiki locally