Daniel,
Looks cool.
I do the same sort of thing fairly extensively with these two helper
methods, which are a bit different:
# a way of identifying features the layout needs to load
def require_page_support(*scr)
@page_scripts ||= {}
scr.each { |s| @page_scripts[s] = true }
end
# used in the layout to query the need for features
def load_page_support?(*scr)
@page_scripts ||= {}
scr.inject(false) {|matches, s| matches || @page_scripts[s] }
end
Then, in my layout, I have things like this, for example for lightbox:
<% if load_page_support? :lightbox -%>
<%= javascript_include_tag "lightbox" %>
<%= stylesheet_link_tag ''lightbox'' %>
<% end -%>
The lightbox link helper then just does something like:
# lightbox support
def link_to_lightbox(content, action, opts={})
require_page_support :lightbox
opts.merge!(:class => ''lbOn'')
link_to(content, action, opts)
end
If any of this is any use, feel free to pinch it.
Daniel N wrote:> Hi all,
>
> This plugin helps to include javascript and stylesheets only when you need
> to.
>
> Currently if you want the :defaults javascripts to be included, you include
> them in the layout and they are called in in all pages that use that
layout,
> even if it''s essentially a javascript static page.
>
> With this you can specify that a layout, or a deeply nested partial
requires
> a particular javascript file, or stylesheet. This will be then included in
> the head tag of the layout without duplicates. eg you can have a partial
> iterating over a collection that calls a particular js file every
iteration,
> but only one javascript_include_tag call will be made for that file.
>
> From the README
>
> = resource_on_demand
>
> Allows inclusion of external javascript and stylesheets in the head tag,
> from anywhere in a view, including nested partials
>
> Usage
> -------
>
> Within the head tag of the layout
>
> <%= include_on_demand %>
>
> Then from anywhere in you views or partials
>
> <% demand_javascript "my", "javascript",
"files", :defaults %>
>
> *note do not use <%= use <%
>
> This will add specified javascripts and to the head tag without duplicates.
> The arguments are the same as
> used in javascript_include_tag
>
> <% demand_stylesheets %> is similar, but the arguments are the same
as used
> in
> stylesheet_link_tag
> ------------------
>
> This is my first plugin so any kind of feedback is welcome :)
>
> You can download it at
>
> http://svn.devjavu.com/liquid/resource_on_demand
>
> Cheers
> Daniel
>
> ------=_Part_45814_26011008.1164895781913
> Content-Type: text/html; charset=ISO-8859-1
> X-Google-AttachSize: 1736
>
> Hi all,<br><br>This plugin helps to include javascript and
stylesheets only when you need to. <br><br>Currently if
you want the :defaults javascripts to be included, you include them in the
layout and they are called in in all pages that use that layout, even if
it''s essentially a javascript static page.
> <br><br>With this you can specify that a layout, or a deeply
nested partial requires a particular javascript file, or stylesheet.
This will be then included in the head tag of the layout without
duplicates. eg you can have a partial iterating over a collection that
calls a particular js file every iteration, but only one javascript_include_tag
call will be made for that file.
> <br><br>From the README<br><br> =
resource_on_demand<br><br>Allows inclusion of external javascript
and stylesheets in the head tag, from anywhere in a view, including nested
partials<br><br>Usage<br>-------<br><br>Within the
head tag of the layout
> <br><br><%= include_on_demand
%><br><br>Then from anywhere in you views or
partials<br><br><% demand_javascript "my",
"javascript", "files", :defaults
%><br><br>*note do not use <%= use <%
> <br><br>This will add specified javascripts and to the head tag
without duplicates. The arguments are the same as<br>used in
javascript_include_tag<br><br><% demand_stylesheets %>
is similar, but the arguments are the same as used in
>
<br>stylesheet_link_tag<br>------------------<br><br>This
is my first plugin so any kind of feedback is welcome :)<br><br>You
can download it at <br><br><a
href="http://svn.devjavu.com/liquid/resource_on_demand">http://svn.devjavu.com/liquid/resource_on_demand
> </a><br><br>Cheers<br>Daniel<br>
>
> ------=_Part_45814_26011008.1164895781913--
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---