Short way to include CSS and javascript tags with HAML

MarioDS picture MarioDS · May 2, 2013 · Viewed 19.4k times · Source

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.

Answer

Ven picture Ven · May 2, 2013

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