17
votes

When including javascript or CSS in HAML you would normally have to do the following to include CSS:

%link{:type => "text/css", :rel => "stylesheet", :href => "/css/mycss.css"}

And for javascript:

%script{:type => "text/javascript", :src => "/js/myscript.js"}

I was wondering if HAML does not have a short way of including these tags (to get content from a source of course, not inline), that omits the need for the type and rel attributes, since these are invariable anyway.

Note that Ruby on Rails provides this feature via a function, but I'm not using rails.

2

2 Answers

28
votes

You don't need the script's type attribute, and you can use the html syntax

%script(src="/js/myscript.js")

you could always create a "helper" to generate it if you feel like it

1
votes

I think what Ven was talking about might be something like...

# For CSS files...
def styletag(:sheet_name)
  "<link rel='stylesheet' href='/styles/#{:sheet_name}.css'>"
end

# For js files...
def jstag(:script_name)
  "<script src='/js/#{:script_name}.js'></script>"
end

Then, in your template, you can use them something like this:

- styletag "mystyles"
- jstag "myscript"