Haml - how do I force tag attributes to use double quotes only AND how do I order tag attributes the way I want?

Keyframe picture Keyframe · Jun 8, 2009 · Viewed 7.9k times · Source

I use staticmatic for templates I use later with PHP. There is an odd situation where some tag attributes have single quotes, while some have double quotation marks. I would like all of them to have double quotes exclusively (not that it matters I guess, but I want them like that!)

For example, haml code:

!!! XML
%html{html_attrs('hr-HR')}
  %head
    %title Some title
    %meta{'http-equiv' => 'Content-Type', :content => 'text/html; charset=utf-8'}/
    %meta{'name' => "description", :content => 'Some title - YO!'}/
    = stylesheets
    = javascripts('test', :other)
  %body
    = yield

produces following:

<?xml version='1.0' encoding='utf-8' ?>
<html lang='hr-HR' xml:lang='hr-HR' xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>Some title</title>
    <meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
    <meta content='Some title - YO!' name='description' />
    <link href="stylesheets/application.css" media="all" rel="stylesheet" type="text/css"/><link href="stylesheets/grid.css" media="all" rel="stylesheet" type="text/css"/><link href="stylesheets/text.css" media="all" rel="stylesheet" type="text/css"/>
    <script language="javascript" src="javascripts/test.js" type="text/javascript"></script><script language="javascript" src="javascripts/other.js" type="text/javascript"></script>

  </head>
  <body>
    <h1>some body stuff!</h1>
    utf test šđčćž ŠĐČĆŽ
  </body>
</html>

note that it doesn't matter if I use single quotes or double quotes in haml code, I always get the same output!

Also, it seems that haml->html output sorts tag attributes alphabetically, not the way I have ordered them in haml. I suspect this has something to do with ruby arrays, but I'm not sure since I don't/can't use Ruby apart from haml in staticmatic. How could I have them ordered the same as I have ordered them in ruby array in haml code?

Answer

Robert K picture Robert K · Jun 8, 2009

Try the following:

Haml::Template.options[:attr_wrapper] = '"'