[ANN] resouce_on_demand plugin

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 :slight_smile:

You can download it at

http://svn.devjavu.com/liquid/resource_on_demand

Cheers
Daniel

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.

On 12/1/06, Michael H. [email protected] wrote:

def require_page_support(*scr)
Then, in my layout, I have things like this, for example for lightbox:
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.

Thanx for the input. Now that I’ve had a sleep on it I’m not sure that
I’m
cool with the method names that I used. I think I may change these to
require_javascript instead of demand_javascript.

I’ll have a good look at how your helper methods work when I get home.

Cheers