13

I am using slim for templating and ruby on rails(just started using them). The only problem I am facing is: there is no formatting for the html rendered. i.e no line breaks, no indentation. I can understand it can be a little tricky for slim to render formatting intrinsically.

Is there anyway to render properly formatted html?

SoWhat
  • 5,564
  • 2
  • 28
  • 59
  • 2
    slim compressing html is actually a feature. as @Deefour suggested, you can tell slim to pretty print html, but you should really use your developer tools to inspect html and only serve compressed files to your users. – rubiii Feb 24 '13 at 13:21

3 Answers3

21

From the docs:

Slim::Engine.set_default_options pretty: true

or directly

Slim::Engine.default_options[:pretty] = true

To expand a bit, as @rubiii mentioned in the comments this is a feature of Slim. For the same reasons it's good practice to minify and compress Javascript and CSS in production Slim strips unecessary whitespace from the HTML it produces without this :pretty option set to true.

If you have some config/initializers/slim.rb file you can configure the :pretty option dynamically by checking the environment.

Slim::Engine.set_default_options pretty: Rails.env.development?

Otherwise you should set this option to true only in config/environments/development.rb, leaving it false in production.

deefour
  • 34,974
  • 7
  • 97
  • 90
  • The :pretty option is super slow! So be sure to only enable in development, like @Deefour says. – xentek Jul 24 '13 at 21:59
-1

Just add data-force-encoding="✓" attribute to the body tag. That will make Rails to send email as quoted printable (trick is to use UTF8 char in fact). See: https://github.com/slim-template/slim/issues/123

ixti
  • 144
  • 2
  • 5
-1

Is there anyway to render properly formatted html?

HTML can be 'properly formatted' technically speaking, without line breaks. Look into 'HTML validation' if you are curious.

As for Slim, line breaks are one of the few points you have to do some extra work. Where many people use the HTML <br> tag, I prefer using $/ which is the ruby variable corresponding to the local line seperator. I prefer this becasue the <br> element is outmoded and though slim doesn't produce any output rendering it, it's bad practice to use the actual tag. The line ending variable is also more famliar and reliable for me personally :)

Actually another great reason to use $/ is that it seamlessly fits into other ruby coding patterns and is more agnostic overall. Check out this template as an example.

Steve Benner
  • 1,679
  • 22
  • 26