Rails 全局 content_for
示例:
我有 2 个部分 _map.haml 和 _bigmap.haml。
:: _map.haml
- content_for :map do
%script{:type => "text/javascript", :src => "http://maps.google.com/maps/api/js?sensor=true"}
...
:: _bigmap.haml
- content_for :bigmap do
%script{:type => "text/javascript", :src => "http://maps.google.com/maps/api/js?sensor=true"}
...
在我的布局中,我将 JavaScript 包含到
= yield(:map)
= yield(:bigmap)
问题 1 中: 这意味着谷歌库将被包含两次。我该如何处理这个问题,以便库始终只加载一次? A也许正在考虑查看海勒?
问题2: 是否可以有一个全局 content_for 字段,其中每个部分都将其内容附加到其中? 谢谢。
Example:
I have 2 partial _map.haml and _bigmap.haml.
:: _map.haml
- content_for :map do
%script{:type => "text/javascript", :src => "http://maps.google.com/maps/api/js?sensor=true"}
...
:: _bigmap.haml
- content_for :bigmap do
%script{:type => "text/javascript", :src => "http://maps.google.com/maps/api/js?sensor=true"}
...
In my layout I include javascripts into
= yield(:map)
= yield(:bigmap)
QUESTION 1:
This means google library will be included twice. How can I handle this so the library is always loaded only once? A was thinking of view heler maybe?
QUESTION 2:
Is it possible to have a global content_for field where each partial appends it's content to it?
Thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将
inject_js
方法添加到您的应用程序帮助程序中,以便在视图中使用:然后在您的应用程序视图中:
以及在任何使用 bigmap:
或常规地图的视图中:
因为
inject_js
使用@javascripts.uniq
,Google 库只会加载一次。inject_js
代码取自 tog 的 tog_core< /a>.还有其他方法(inject_css 等)。You can add a
inject_js
method into your application helper for use in views:Then in your application view:
and in any view that uses bigmap:
or regular map:
Because
inject_js
uses@javascripts.uniq
, the Google library will only be loaded once.inject_js
code taken from tog's tog_core. There are other methods there as well (inject_css, etc).