Skip to content

Instantly share code, notes, and snippets.

@seanami
Created July 28, 2010 23:24
Show Gist options
  • Save seanami/496702 to your computer and use it in GitHub Desktop.
Save seanami/496702 to your computer and use it in GitHub Desktop.

Revisions

  1. seanami created this gist Jul 28, 2010.
    45 changes: 45 additions & 0 deletions sinatra_erb_block_helper_test.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    require 'rubygems'
    require 'sinatra'
    require 'erb'

    helpers do
    def buffer()
    @_out_buf
    end
    def capture(buffer)
    pos = buffer.size
    yield
    buffer.slice!(pos..buffer.size)
    end
    def my_helper(&block)
    buffer << erb(capture(buffer, &block), :layout => :my_layout)
    end
    end

    template :index do
    <<-eos
    <!DOCTYPE html>
    <html>
    <head><title>Block Helper Test</title></head>
    <body>
    <% my_helper do %>
    <p>This is the content in the block.</p>
    <% end %>
    </body>
    </html>
    eos
    end

    template :my_layout do
    <<-eos
    <div class="my-container">
    <p>This should be above the content in the block.</p>
    <%= yield %>
    <p>This should be below the content in the block.</p>
    </div>
    eos
    end

    get "/" do
    erb :index
    end