Merb simple_format helper

Merb does not have a simple_format helper like in Rails so you can do the following.

Add this to your Application Helper

1
2
3
4
5
6
7
8
9
10
11
      def simple_format(text, html_options={})
        text = text.to_s.dup
        text.gsub!(/\r\n?/, "\n")
        text = text.split(/\n\n+/)
        formatted_text = ""
        text.each do |line|
          line.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />')
          formatted_text << tag(:p, line, html_options) + "\n\n"
        end
        formatted_text
      end

Use in your view
<%= simple_format(@page.body) %>
<%= simple_format(@page.body, :class => ‘hello’) %>

Thanks goes to matteti for enlighting me on the fact that I could start with the rails simple_format helper and configure as necessary.

Comments

  1. Matt Aimonetti | November 27, 2008

    No problem Scott :)