Inline ruby in :javascript haml tag? [duplicate]

非 Y 不嫁゛ 提交于 2019-12-03 00:58:18
Arnaud Le Blanc

You can use the string interpolation syntax (#{...}) :

:javascript
   var tab = #{@tab}

Take care of correctly escaping, however.

This is a terrible way to structure code.

Inline JavaScript is bad enough. Inline JavaScript with ERb inside puts you straight into hell.

JavaScript should be in external static files. That way the browser can cache them, and you can use JavaScript as a real programming language.

In your case, I'd recommend something like:

(Haml file)

#tab= @tab

(CSS file)

#tab { display: none }

(Javascript file)

var Tab = $('#tab').innerHTML();

That way everything stays static and maintainable, and you're not mixing JS and HTML.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!