Skip to content

Why Squatting?

August 20, 2009

Below are two tutorial examples of Camping, a Web Microframework written in Ruby.

Alongside them are my two conversions written in Squatting, a Camping-inspired Web Microframework for Perl, written by John Beppu

 

The Camping Short, Short Example:

require 'camping'

Camping.goes :HomePage

module HomePage::Controllers

  # The root slash shows the `index' view.
  class Index < R '/'
    def get
      render :index 
    end
  end

  # Any other page name gets sent to the view
  # of the same name.
  #
  #   /index -> Views#index
  #   /sample -> Views#sample
  #
  class Page < R '/(\w+)'
    def get(page_name)
      render page_name
    end
  end

end

module HomePage::Views

  # If you have a `layout' method like this, it
  # will wrap the HTML in the other methods.  The
  # `self << yield' is where the HTML is inserted.
  def layout
    html do
      title { 'My HomePage' }
      body { self << yield }
    end
  end

  # The `index' view.  Inside your views, you express
  # the HTML in Ruby.  See http://code.whytheluckystiff.net/markaby/.
  def index
    p 'Hi my name is Charles.'
    p 'Here are some links:'
    ul do
     li { a 'Google', :href => 'http://google.com' }
     li { a 'A sample page', :href => '/sample' }
    end
  end

  # The `sample' view.
  def sample
    p 'A sample page'
  end
end

The Squatting Short, Short Example:

use strict;
use warnings;

{
  package ShortShortExample;
  use base 'Squatting';
}

{
  package ShortShortExample::Controllers;
  use Squatting ':controllers';
  
  our @C = (

    C( 
      Index => [ '/' ],
      get   => sub {
        my ( $self ) = @_;
        $self->render( 'index' );
      },
    ),
    
    C(
      Page => [ '/(\w+)' ],
      get  => sub {
        my ( $self, $page_name ) = @_;
        $self->render( $page_name );
      }
    ),
    
  );
}

{
  package ShortShortExample::Views;
  use Squatting ':views';
  use HTML::AsSubs;
    
  our @V = (
    
    V(  'html',
      
      layout => sub {
        my ( $self, $v, @yield ) = @_;
        html(
          title ( 'My HomePage' ),
          body ( @yield ),
        )->as_HTML;
      },
      
      index => sub {
        my ( $self, $v ) = @_;
        return (
          p ( 'Hi my name is Charles.' ),
          p ( 'Here are some links:'   ),
          ul (
            li ( a( { href => 'http://google.com' },  'Google' ) ),
            li ( a( { href => '/sample' },  'a sample page'    ) ),
          ),
        );
      },
      
      sample => sub {
        my ( $self ) = @_;
        p ( 'A sample page' );
      },
            
    ),
  );
}

1;

 

The Camping Tutorial Two:

require 'camping'

Camping.goes :Nuts

module Nuts::Controllers
  class Index < R '/'
    def get
      @t = Time.now.to_s
      render :sundial
    end
  end
end

module Nuts::Views
  def layout
    html do
      head do
        title { "Nuts And GORP" }
      end
      body { self << yield }
    end
  end

  def sundial
    p "The current time is: #{@t}"
  end
end

The Squatting Tutorial Two:

use strict;
use warnings;

{
  package Nuts;
  use base 'Squatting';
}

{ 
  package Nuts::Controllers;
  use Squatting ':controllers';
  
  our @C = (
    C(
      Index => [ '/' ],
      get   => sub { 
        my ( $self ) = @_;
        my $v = $self->v;
        $v->{t} = localtime;
        $self->render( 'sundial' );
      },
    ),
  );
}

{
  package Nuts::Views;
  use Squatting ':views';
  use HTML::AsSubs;
  
  our @V = (
    V(  'html',
    
      layout => sub { 
        my ( $self, $v, @yield ) = @_;
        html (
          head ( 'Nuts And GORP' ),
          body ( @yield ),
        )->as_HTML;
      },
      
      sundial => sub {
        my ( $self, $v ) = @_;
        p ( "The current time is: $v->{t}" );
      },
    ),
  );
}

1;

I’m not going to say much more at this point because I think the code above clearly expresses what’s going on. So just enjoy ;-)

/I3az/

 

Postscript:

The two Camping tutorials were written by Why the lucky stiff the author of Camping. These are no longer available on web because _why was regurgitating them from his own repo/website onto GitHub.

I wrote the Squatting ports of the tutorial back in October 2008 so luckily I had pulled down these Camping tutorials. I found that porting _why’s excellent Camping tutorials were a good way of learning Squatting.

The plan was always to post these on my blog. In fact I have a long list of stuff to post ranging from just a few scribbles to completed prose. I’m just trying to be “logical” about the order of these posts!

So why (excuse the pun!) post these now? The reason will be obvious to most readers…. _why’s strange disappearance from the web overnight.

I not going to conjecture further but I hope all is OK. So this post is my little ode to _why.

Below are some links on this strange event:

  • _why is no more (Hacker News)
  • _why’s best twitter posts (Hacker News)
  • Eulogy to _why (John Resig)
    • About these ads
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: