如何将 Javascript 文件导入 Haml 视图? [英] How do import a Javascript file into a Haml view?

查看:37
本文介绍了如何将 Javascript 文件导入 Haml 视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Haml 视图中包含一些 Javascript 功能,但我不希望它插入到每个视图中.我是否必须将 Javascript 文件放在 public/javascripts 中,或者我可以将它放在 app/assets/javascripts 中以使其对用户隐藏,并且仍然从 haml 视图文件中引用?

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?

推荐答案

你应该使用

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

如果它特定于一个地方,你应该使用 content_for

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

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

然后在你看来

- content_for :javascripts do
  = javascript_include_tag "my_js_file"

这篇关于如何将 Javascript 文件导入 Haml 视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆