Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do import a Javascript file into a Haml view?

I want to include some Javascript functionality in a Haml view, but I don't want it inserted into every view. Do I have to put the Javascript file in public/javascripts or can I put it in app/assets/javascripts to keep it hidden from user, and still reference from within the haml view file?

How would you do both these options if they are possible?

like image 701
Leila Hamon Avatar asked Jul 12 '12 15:07

Leila Hamon


People also ask

How add JavaScript to HAML?

To include JavaScript directly in a Haml view, use the nifty :javascript filter. Haml sees the contents as JavaScript and wraps them in a script tag when rendered as HTML. :javascript alert('foo'); The script tag will be output exactly where you put it in the view.

What is HAML file?

Haml (HTML Abstraction Markup Language) is a templating system that is designed to avoid writing inline code in a web document and make the HTML cleaner.

What is HAML in CSS?

Haml is a markup language that's used to cleanly and simply describe the HTML of any web document, without the use of inline code. Haml functions as a replacement for inline page templating systems such as PHP, ERB, and ASP.


1 Answers

You should just use

!!!
%html
  %head
    = javascript_include_tag "my_js_file"

if it's specific to one place, you should use content_for

!!!
%html
  %head
    = yield(:javascripts)

And then in your view

- content_for :javascripts do
  = javascript_include_tag "my_js_file"
like image 145
Draiken Avatar answered Sep 28 '22 11:09

Draiken