Skip to content
Lance Pollard edited this page Apr 12, 2012 · 1 revision

Rendering

  • Templates
  • JSON
  • The Rendering Process

Templates

class App.PostsController extends Tower.Controller
  index: ->
    @render "index"

JSON

class App.PostsController extends Tower.Controller
  show: ->
    App.Post.find @params.id, (error, post) =>
      @render json: post

The Rendering Process

respondWith

show: ->
  App.Post.find @params.id, (error, post) =>
    @respondWith post, (format) =>
      format.html => @render "show"

This will perform content negotiation, i.e. it will figure out what the mime type the browser prefers and run the corresponding responder method (for html, json, csv, etc.). Those methods then call the render method.

render

show: ->
  App.Post.find @params.id, (error, post) =>
    @render "show"

Calling the render method directly forces a specific content type to be rendered. Here is the method signature:

render json: {hello: "world"}
render "show"                 # render action: "show"
render "posts/show"           # render file: "posts/show"
render -> h1 "Hello World"
render text: "success", status: 200

_normalizeRender

This converts the render arguments into a normalized options hash.

_renderToBody

_renderOption

_renderTemplate

Tower.js

Everything here will be reflected on http://towerjs.org/guides.

Clone this wiki locally