views:

1042

answers:

2

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'&gt;
  <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?

+3  A: 

Try the following:

Haml::Template.options[:attr_wrapper] = '"'
The Wicked Flea
where would I put that in staticmatic? configuration.rb in src/ under project made by staticmatic isn't it. Since I have little clue about ruby yet, I don't know where to put that configuration.
Keyframe
ok, I've monkey patched render.rb in staticmatic to include that option, it is all " now. Thanks!
Keyframe
+1  A: 

Haml does indeed order attributes alphabetically, and this is indeed a consequence of Ruby's parser. In the future, attributes may be ordered in document order as much as possible, but that's not likely to happen until Haml 2.2 or later.

nex3
I wonder if it is possible at all? I know it doesn't matter, the ordering of attributes, but it just doesn't feel right when looking at the code. I could prepare a helper and include that, but why would I use haml at all then, right?
Keyframe
The issue stems from how Haml compiles templates. It converts the templates first into Ruby code which runs to produce a string of HTML. It tries to parse the attribute hash before compiling (recently it's gotten better at this - it should be able to parse any attribute hash that doesn't include dynamic values), but if it can't, it inserts the hash as a literal Ruby hash in the compiled code. Since Ruby hashes don't preserve order (at least in Ruby 1.8), this means that the resulting attributes are in an undefined order. Then in order to make the output deterministic, Haml alphabetizes them.
nex3
Since Haml parses static attributes without dumping them to Ruby, it may be possible to output those in template order. The problem then becomes figuring out what to do with some static and some dynamic attributes. This is a solvable problem, and will probably be addressed eventually, but the next Haml version (2.2) is drawing close to release so it may be pushed off until version 2.4.
nex3