Zabba
Zabba

Reputation: 65517

How to minify HTML?

Is there some tool (or Rails itself) that can minify HTML (like what Jammit does for CSS and JS files) ?

Secondarily, what is the best practice here, and is it even worth minifying the html? (this is for a site that will be served to mobile phones, so keeping weight down is important)

Upvotes: 14

Views: 10844

Answers (3)

austincheney
austincheney

Reputation: 1115

You can use http://prettydiff.com/?m=minify&html to minify your HTML.

Minifying HTML is extremely complicated and easily misunderstood. True minification involves removing comments and all unnecessary white space from the syntax, which would include an white space in your content, so be sure you are using a tool that knows what it is doing.

Upvotes: 1

Josh Delsman
Josh Delsman

Reputation: 3042

Well, you can remove most white space by using the HAML gem and the following lines in your config/application.rb file:

Haml.init_rails(binding)
Haml::Template.options[:format] = :html5
Haml::Template.options[:ugly] = true

More information: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#ugly-option

Upvotes: 13

Bruno Rohée
Bruno Rohée

Reputation: 3534

Enabling compression at HTTP level will serve you much more than minifying HTML, however tidy is good to apply transformations to HTML, including removing extraneous spaces , comments, etc...

Upvotes: 9

Related Questions