But what about a Plack and TT replacement?

On my VPS, I currently use Perl’s Plack and TemplateToolkit to serve up html pages. This means (for sites thats fit this requirement) I can take an HTML site (perhaps provided by a designer) and just serve it up.. any dynamic content can be added as a when required with no change to setup except the source HTML pages.

I achieve the above by basically reading this page doing rougthy what it says: CPAN Page

… and the Ruby equivalent

It seems the most common Ruby templating engine is ERB, so let use that. and lets write a rackup script to kick it off (I wonder if this could become a gem?) .. this is my 1st version and has many issues, but will do for starters (called app.ru)

require 'erb'
require 'mime/types'

module Benifit

  class Controller
    attr_accessor :public_path
    attr_reader :env, :response_code, :response_src, :response_type

    def initialize( env )
      @env = env

      @public_path = File.join( File.dirname(__FILE__), 'public');

      raise "Path to public dir (#{@public_path}) does not exists!" unless File.directory?( @public_path )

      if template_file_exists? then
          set_response(200)
      else
          set_response(404, "Page not found")
      end

    end

    def set_response( code = 404, msg = nil )

      @response_code = code

      if ( msg.nil? ) then
        @response_src = File.read( self.template_file )
        mtype = MIME::Types.type_for( self.template_file )
        @response_type = mtype.empty? ? 'text/plain' : mtype[0].to_s
      else
        @response_type = 'text/plain'
        @response_src = msg
      end

    end

    def template_file
      requested = env['REQUEST_PATH'] 
      requested = 'index.html' if ( requested.nil? || requested[-1] == '/' )
      File.join( self.public_path, requested )
    end

    def template_file_exists?
      File.exists?( self.template_file )
    end

    def get_binding 
      binding
    end
  end

  class Rack

    def call(env)
      c = Controller.new( env )
      template = ERB.new( c.response_src )
      return [ 
          c.response_code,
          {'Content-Type' => c.response_type }, 
          [ template.result( c.get_binding ) ] 
      ]
    end

  end

end

##############################################################################
# End of Module
##############################################################################

unless ( __FILE__ == $PROGRAM_NAME ) then
    # call run (made available via running rackup)
    run Benifit::Rack.new
else
    # run tests - to the filled in :)
    require 'test/unit'
    class BenifiTest < Test::Unit::TestCase
      def setup
          @bc = Benifit::Controller.new({})
      end
      def test_simple 
          assert( @bc )
      end
    end
end

Notice at then end of the file I have included some tests, these means I can inline the tests in the same file, but the tests will not run when running under ‘rackup’, yet they will run when run directly by ruby.

But, as ever, the problem has been encountered and solved already: Rack-Server-Pages