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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With