I want to include a partial in the layout that might not exist. :o)
Here's an example: Some flows have specific CSS or JavaScript and I'd
like to automatically include those in the HTML <head> (via the
layout), so it would be great to have an optional file in called
_head.rhtml in the views that need it -- but not all views.
You could always create a partial at app/views/shared/_head.rhtml, and
include it on every page. Inside that partial you could place:
case params[:controller]
when :application
...
end
when :accounts
...
end
end
Same effect as what Daniel said, but at least all your JS/CSS stuff is
kept in one file (and you could be tricky and keep common JS/CSS above
or below the case statement to make sure it's included on every page,
leaving the blocks inside the case for the specialized JS/CSS).
Thanks for both of your suggestions. With that direction I came up
with a helper that would give me even more control.
Here's how it works. I have a directory called "shared" directly
under views -- this directory is for global partials. I can also have
a partial in the controller view directory with the same name as one
in the shared directory. For example, if I use it like this:
<pre>
<%= shared :head %>
</pre>
The helper will start by looking in the controller view directory for
"_head.rhtml" (i.e. views/users/_head.rhtml) and if it's not there
load up the one in the shared directory (i.e. views/shared/
_head.rhtml). You can also have it include both with the :both
option:
<pre>
<%= shared :head, :both => true %>
</pre>
This way "user" pages can include more scripts and CSS in the head
than the default "shared" set. Here's my code, let me know what you
think: