Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HAML filters in a helper

Helper functions can receive a block which they yield to render the block. Sometimes I'd want that block to be spec'd with a filter. So for example:

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

This is not so DRY as we are always writing doc_page and markdown together. Can I make the helper method accept a block and pass it through a HAML filter. Something like:

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

In this fantasy, doc_page is a helper method that does some setup stuff and then passes the content through markdown, saving us the need to declare :markdown everywhere and making the world a DRYer place.

like image 782
mahemoff Avatar asked Nov 06 '14 11:11

mahemoff


Video Answer


1 Answers

Currently it is not possible to use filters in helpers. An alternative approach would be to use redcarpet to parse the markdown and then pass the output to a helper.

an example would be:

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

= doc_page title: 'FAQ' do
  normal html

The implementation of the doc_page would be something like this:

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

This would solve your problem, as you define your markdown filter in the helper. And you don't need an extra indentation level in your haml.

like image 85
Vince V. Avatar answered Sep 21 '22 20:09

Vince V.