How to set HAML format to :xhtml to get XHTML Doctype?

I’ve come back to a middleman project after some time, maybe a year. I’ve loaded the latest Middleman (3.0.14). But now when I build, the ‘!!!’ at the top of layout.html.haml produces

<!DOCTYPE html>

instead of

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

as it used to.

HAML’s documentation says (at Haml::Options::format)

  • format – Determines the output format. The default is :html5. The other options are :html4 and :xhtml. If the output is set to XHTML, then Haml automatically generates self-closing tags and wraps the output of the Javascript and CSS-like filters inside CDATA. When the output is set to :html5 or :html4, XML prologs are ignored. In all cases, an appropriate doctype is generated from !!!.

So how do I set Haml’s format to :xhtml in Middleman? In Rails, one apparently puts this in config/initializers/haml.rb

Haml::Template.options[:format] = :xhtml

But this doesn’t work in config.rb.

You can do it defining in your layout:

!!! Strict

More details can be found here:
http://haml.info/docs/yardoc/file.REFERENCE.html#doctype_

!! Strict

produces only

<!DOCTYPE html>

with the latest version of Haml. As the Haml doc says:

You can also specify the specific doctype after the !!! When the :format is set to :xhtml the following doctypes are supported: (and then lists ‘Strict’ and others)

and the default for Haml’s :format seems to be :html5.

Still wondering: how do I set the Haml format option to :xhtml in Middleman?

You need three exclamation points:

!!! Strict

More details can be found here:
http://haml.info/docs/yardoc/file.REFERENCE.html#doctype_

Regarding you config.rb file have you added the following:

set :haml, :format => :xhtml
1 Like