Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between application.haml and application.html.haml?

Since many days I tried to understand why a simple link like this one :

link_to 'My Link', my_path(format: :js), remote: true

was always returning full HTML document instead of executing javascript located in my file.js.erb :

alert('hello world')

[...]

After hours of debugging I found why:

When I rename my main layout file such as: application.haml it renders full HTML document :

Started GET "/my_path/2.js" for 127.0.0.1 at 2016-03-05 12:28:20 +0100
Processing by MyController#show as JS
  Rendered my_path/show.js.erb within layouts/application (0.1ms)
  Rendered layouts/_sidebar.html.erb (18.9ms)
  Rendered layouts/_headbar.haml (0.5ms)
  Rendered layouts/_flash_messages.html.haml (0.2ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 102ms (Views: 59.3ms | ActiveRecord: 2.9ms)

When I rename my main layout file such as: application.html.haml it executes javascript properly and runs my hello world popup :

Started GET "/my_path/8.js" for 127.0.0.1 at 2016-03-05 12:28:34 +0100
Processing by MyController#show as JS
  Rendered my_path/show.js.erb (0.1ms)
Completed 200 OK in 24ms (Views: 21.8ms | ActiveRecord: 0.4ms)

Why is there a difference in the javascript behavior according the different filenames of my layout ?

like image 564
Léo Avatar asked Mar 05 '16 11:03

Léo


People also ask

What is the difference between HTML and Haml?

A hypertext markup language (HTML) is the primary language for developing web pages. HTML5 is a new version of HTML with new functionalities with markup language with Internet technologies. Language in HTML does not have support for video and audio. HTML5 supports both video and audio.

Is Haml better than HTML?

HAML is just a templating language that gets transformed into HTML (e.g. same final output). If you find the HAML syntax to be easier than HTML then go for it. However IMHO - abstracting away what actual elements you are generating just makes applying CSS and doing JavaScript navigation that much more difficult.

What are HTML Haml files?

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. Haml gives the flexibility to have some dynamic content in HTML.

How do you write Haml in HTML?

In Haml, we write a tag by using the percent sign and then the name of the tag. This works for %strong , %div , %body , %html ; any tag you want. Then, after the name of the tag is = , which tells Haml to evaluate Ruby code to the right and then print out the return value as the contents of the tag.


1 Answers

As BroiSatse said:

This is not javascript behaviour, it is how rails searches for templates. First it search for <action_name>.<templating-engine> files, then for <action_name>.<format><templateing-engine>. So when you have generic template without the format, it will be taken for all the formats.

like image 199
Léo Avatar answered Sep 30 '22 15:09

Léo