robert hahn

a darn good web developer

July 21, 2007

Camping Without the View

It’s possible to construct a web application using the Camping microframework without using the *::Views module. Most of the time, you would be crazy to do so, since the *::Views module makes it very easy to print out HTML responses using _why’s Markaby markup engine.

There is a strong use case for not using *::Views– if you want to return data that is not supposed to be formatted as HTML (like XML or JSON), you’ll probably find that it’s more work than it really ought to be, to print out that response.

The Model-less, View-less Camping App.

That’s right. I’m going to show you a camping app that does not use either the *::Views or the *::Models modules, that is perfectly legit:

#!/usr/bin/env ruby

require 'rubygems'
require 'camping'

Camping.goes :HelloWorld

module HelloWorld::Controllers
  class Index < R '/'
    def get
      %Q(
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head><title>My small page</title></head>
      <body>
        <h1>Hello, world!</h1>
      </body>
      </html>
      )
    end
  end
end

If you save this program as hello_world.rb, then run camping hello_world.rb, it will run fine, and if you point your browser to http://localhost:3301/, you will see the friendly greeting.

What’s going on? Let’s rewrite the application to something more familiar:

#!/usr/bin/env ruby

require 'rubygems'
require 'camping'

Camping.goes :HelloWorld

module HelloWorld::Controllers
  class Index < R '/'
    def get
      render :hello_world
    end
  end
end

module HelloWorld::Views
  def hello_world
    html do
      head do
        title "My small page"
      end
      body do
        h1 "Hello, world!"
      end
    end
  end
end

If you run this hello_world.rb app, you’ll get almost the same output. There is almost no magic involved here; in the get function, we have a render statement that performs the necessary work to call the hello_world function in the HelloWorld::Views module. What’s not obvious here (except to seasoned Rubyists, I suppose) is that there is an implicit return in the hello_world function; it returns a string containing the HTML to be printed. There is also an implicit return happening in the controller, where the results of the render statement (the Hello, world page) is returned to its caller.

So the dirty secret is out: as long as the methods in your controller classes return a string, whatever is in that string is what’s going to be returned to the browser.

Why do we want to do this, again?

Most of the time, you want to use the *::Views module, because, most of the time, you want to return HTML to the browser.

Knowing what we know now, we have the ability to do a couple of useful and interesting things:

I’m going to get into more of this in future articles.

decorative image of trees

Copyright © 2009
Robert Hahn.
All Rights Reserved unless otherwise indicated.