Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 618
Keyframe Avatar asked Jun 08 '09 21:06

Keyframe


3 Answers

Try the following:

Haml::Template.options[:attr_wrapper] = '"'
like image 111
Robert K Avatar answered Oct 11 '22 13:10

Robert K


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.

like image 28
Natalie Weizenbaum Avatar answered Oct 11 '22 14:10

Natalie Weizenbaum


Quote from: http://haml.info/docs/yardoc/file.REFERENCE.html#options

Haml understands various configuration options that affect its performance and output.

In Rails, options can be set by setting the Haml::Template.options hash in an initializer:

# config/initializers/haml.rb

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

Outside Rails, you can set them by configuring them globally in Haml::Options.defaults:

Haml::Options.defaults[:format] = :html5

Finally, you can also set them by passing an options hash to [Haml::Engine#initialize][1]. For the complete list of available options, please see [Haml::Options][2].

[1]: http://haml.info/docs/yardoc/Haml/Engine.html#initialize-instance_method

[2]: http://haml.info/docs/yardoc/Haml/Options.html

like image 22
Roman M. Koss Avatar answered Oct 11 '22 13:10

Roman M. Koss