HAML Filters in the Helper

Helper functions can receive the block that they yielddisplay in the block. Sometimes I would like this block to be specified with a filter. For example:

= doc_page title: 'FAQ' do
  :markdown
    # Welcome to the *FAQ*

This is not so dry as we always write doc_page and markdown together. Can I get a helper method to accept a block and pass it through a HAML filter. Sort of:

= doc_page title: 'FAQ' do
  # Welcome to the *FAQ*

In this fantasy doc_page, this is a helper method that performs some settings and then transfers the content using markdowns, which allows us to declare :markdowneverywhere and make the world a DRYer place.

+4
source share
3 answers

. , redcarpet , .

:

= doc_page title: 'FAQ', :markdown do
  ### my markdown

= doc_page title: 'FAQ' do
  normal html

doc_page :

def doc_page(title, markup=:html)

  content = yield

  if markup == :markdown
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
    content = markdown.render(content)
  end

  content
end

, . haml.

+4

tilt (api haml ) , haml. , - ( ).

markdown_template = Tilt['md'].new { "# this is markdown code" }
markdown_template.render

Tilt docs. , .

+2

, , haml - preprocessor, :

= doc_page title: 'FAQ' do
  # Welcome to the *FAQ*
  %a href="/" link

ruby, :

concat(doc_page title: 'FAQ' do
  # Welcome to the *FAQ*
  concat('<a href="/">link</a>')
end)
+2

All Articles