Skip to content

Instantly share code, notes, and snippets.

@tow8ie
Created October 22, 2009 21:45
Show Gist options
  • Save tow8ie/216366 to your computer and use it in GitHub Desktop.
Save tow8ie/216366 to your computer and use it in GitHub Desktop.
Another very hacky proof-of-concept of template abstractions by multi-block helper functions. This time in a more concrete implementation as Rails helpers.
.box
%h2= title
.body= block
.first
%ul
%li The first column contains a list where the
%li third point can be chosen freely:
%li= block
.third
%cite{:style => "color: red;"}= block
.teaser
%h3 This is a teaser
.content= block
module ApplicationHelper
def partial_with_block(partial, block_content, locals_except_block_content={})
concat(render(
:partial => partial,
:locals => locals_except_block_content.merge(:block => block_content)
))
end
def simple_content(&block)
capture(&block)
end
def complex_content(type, &block)
capture {block.call(type.new(self)).emit}
end
def teaser(&block)
partial_with_block(
'teaser',
simple_content(&block)
)
end
def box(title, &block)
partial_with_block(
'box',
complex_content(Column, &block),
{:title => title}
)
end
class Column
def initialize(ah)
@app_helper = ah
end
def first(&content)
@first = @app_helper.capture(&content)
self
end
def second(&content)
@second = @app_helper.capture(&content)
self
end
def third(&content)
@third = @app_helper.capture(&content)
self
end
def emit
@app_helper.concat(@app_helper.render(:partial => 'column_first', :locals => {:block => @first}))
@app_helper.concat "<pre>#{@second}</pre>"
@app_helper.concat(@app_helper.render(:partial => 'column_third', :locals => {:block => @third}))
end
end
end
- box "The title of the box" do |col|
- col.third do
%p This should be a red cite in the third column.
- col.first do
So here I am!
- col.second do
Some Text that should be preformatted.
- teaser do
%p This must be inside a teaser.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment