在 Rails 中使用 content_for 包含内联 JavaScript
我正在使用 content_for 和 yeild 将 javascript 文件注入布局的底部,但我想知道包含内联 javascript 的最佳实践是什么。具体来说,我想知道将脚本类型声明放在哪里:
<% content_for :javascript do %>
<script type="text/javascript"> ... </script>
<% end %>
或者
<% content_for :javascript do %> ... <% end %>
<script type="text/javascript">
<%= yield :javascript %>
</script>
<% end %>
包含多个声明是否不好
...我现在使用第一个选项,想知道在一个视图中 。有时我的部分原因会导致这种情况。
I am using content_for and yeild to inject javascript files into the bottom of my layout but am wondering what the best practice is for including inline javascript. Specifically I'm wondering where the put the script type declaration:
<% content_for :javascript do %>
<script type="text/javascript"> ... </script>
<% end %>
or
<% content_for :javascript do %> ... <% end %>
<script type="text/javascript">
<%= yield :javascript %>
</script>
<% end %>
I am using the first option now and wondering if it is bad to include multiple
...
declarations within one view. Sometimes I have partials that lead to this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我更喜欢让布局的yield看起来像这样:
然后在视图中你可以写:
在部分_sub_view_with_javascript.html.erb中你也可以写:
我对这种方法的推理是yield和content_for位于不同的文件中。为每个 content_for 添加脚本标记并不干燥,但它允许语法突出显示器识别每个文件中语言的变化并为我提供帮助。
如果您在单个文件中对同一符号(在我们的例子中为:javascript)有多个 content_for 调用,我会考虑将它们全部合并到顶部,但它非常适合与部分一起使用。
HTML 非常乐意拥有任意数量的脚本块。唯一可能的问题是,在使用 Firebug 等开发工具中的代码时,需要花费更多时间来找到适合您的函数的脚本块。仅当我需要设置 javascript 断点进行调试时,才会发生这种情况。
I much prefer to have the layout's yield look like this:
Then in a view you can write:
And in the partial _sub_view_with_javascript.html.erb you can also write:
My reasoning for this approach is that the yield and content_for are in different files. It is not DRY to put the script tag in for every content_for but it allows the syntax highlighter to recognize the change in language in each file and helps me there.
If you have multiple content_for calls in a single file to the same symbol (in our case, :javascript) I would consider consolidating them all to the top one, but it's perfect for use with partials.
And HTML is perfectly happy to have as many script blocks as you would like. The only possible gotcha is when working with the code in developer tools like firebug, it takes a little more time to find the right script block for your function. This only happens for me when I need to set a javascript breakpoint to debug.
如果您确实想保留 < 的数量,这里有一个解决方案脚本>在您的 html 中添加最少的标签,并且仍然能够让您的 IDE 突出显示 javascript。如果您只想在 html 中调用一个 $(document).ready() ,或者使用 facebook js api 在加载 api 时调用 js 等,那么它对于 jquery 非常有用...
layout_helper.rb :
application.html.erb :
任何视图文件:
此解决方案使我能够在 IDE 中保持代码的组织性和可读性,因为我不需要为每个 js 代码创建部分内容。即使它对最终用户没有任何改变,html 结果也会更干净。
Here's a solution if you really want to keep the number of < script> tag minimal in your html and still be able to have your IDE highlight javascript. It's really useful with jquery if you want only one $(document).ready() call in your html or with facebook js api to call js when the api is loaded, etc...
layout_helper.rb :
application.html.erb :
any view file:
This solution enable me to keep my code organise and readable in my IDE since I don't need to create partial for every js code. Even if it change nothing for the end user the html result is cleaner.