17

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 Answers 2

30

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

Sign up to request clarification or add additional context in comments.

Comments

1

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.