Created
July 30, 2011 13:48
-
-
Save fguillen/1115542 to your computer and use it in GitHub Desktop.
Mustache filter
This file contains hidden or 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
# Wanting to use the _filter_ feature of _Liquid_ in _Mustache_?, no problem: | |
# To MarkDown filter in Mustache | |
require 'rubygems' | |
require 'mustache' | |
require 'rdiscount' | |
class MyView < Mustache | |
def to_md( text ) | |
RDiscount.new( text ).to_html | |
end | |
end | |
my_view = MyView.new | |
puts( | |
Mustache.render( | |
"{{#my_view}}{{#to_md}}MarkDown *rules*!.{{/to_md}}{{/my_view}}", | |
{ :my_view => my_view } | |
) | |
) | |
# => <p>MarkDown <em>rules</em>!.</p> |
This file contains hidden or 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
# Maybe you need to feed the filter with dinamic content, | |
# maybe an attribute of a Mustache object: | |
require 'rubygems' | |
require 'mustache' | |
require 'rdiscount' | |
class MyView < Mustache | |
def initialize( text ) | |
@text = text | |
end | |
def text | |
@text | |
end | |
def to_md( text ) | |
RDiscount.new( render( text ) ).to_html # especial attention to the 'render' call. | |
end | |
end | |
my_view = MyView.new( "MarkDown *rules*!." ) | |
puts( | |
Mustache.render( | |
"{{#my_view}}{{#to_md}}{{text}}{{/to_md}}{{/my_view}}", | |
{ :my_view => my_view } | |
) | |
) | |
# => <p>MarkDown <em>rules</em>!.</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment