Created
October 22, 2009 21:45
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.box | |
%h2= title | |
.body= block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.first | |
%ul | |
%li The first column contains a list where the | |
%li third point can be chosen freely: | |
%li= block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.third | |
%cite{:style => "color: red;"}= block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- 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