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.
You can add a inject_js method into your application helper for use in views:
def inject_js
@javascripts.uniq.collect{ |js|
javascript_include_tag js
}.join("\n")
end
Then in your application view:
%html
%head
...
= inject_js
and in any view that uses bigmap:
- @javascripts << 'http://maps.google.com/maps/api/js?sensor=true'
- @javascripts << 'bigmap'
or regular map:
- @javascripts << 'http://maps.google.com/maps/api/js?sensor=true'
- @javascripts << 'bigmap'
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With