Skip to content

Instantly share code, notes, and snippets.

@fguillen
Created July 30, 2011 13:48
Show Gist options
  • Save fguillen/1115542 to your computer and use it in GitHub Desktop.
Save fguillen/1115542 to your computer and use it in GitHub Desktop.
Mustache filter
# 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>
# 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