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. Does my question make sense? - Jeremy --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mozmonkey wrote:> 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.One way would be to simply wrap the includes with a conditional: <head> <% if params[:controller].eql?(''certain_controller'') %> <%= javascript_include_tag ''some-javascript'' %> <% end %> ... </head> That sort of code can get pretty ugly, so I''d put the whole thing into a helper. I''m sure someone here has a better way though... ^_^ -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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).
On Oct 7, 3:24 pm, Daniel Waite
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:> Mozmonkey wrote:
> > 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.
>
> One way would be to simply wrap the includes with a conditional:
>
> <head>
> <% if params[:controller].eql?(''certain_controller'')
%>
> <%= javascript_include_tag ''some-javascript'' %>
> <% end %>
> ...
> </head>
>
> That sort of code can get pretty ugly, so I''d put the whole thing
into a
> helper. I''m sure someone here has a better way though... ^_^
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
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:
<pre>
#
# Retreive a partial that can be local to the view or in the shared
directory.
#
# = EXAMPLE - For a sidebar
# shared/_sidebar.rhtml - Default global sidebar
# search/_sidebar.rhtml - Used for views under search
# welcome/ (no sidebar.rhtml) - Pages under welcome/ use shared/
_sidebar.rhtml
#
# = OPTIONS
# * :both=>true: This includes both the global and local version (if
it exists). The global will always be included first
#
def shared(name, options=false)
content = ""
name = name.to_s
file = "_#{name}.rhtml";
# Render global
if options && options[:both]
content = render(:file => "shared/#{file}")
end
# Check local
if File.exist?("#{File.dirname(__FILE__)}/../views/
#{params[:controller]}/#{file}")
content << render(:partial => name)
elsif !options || !options[:both]
content = render(:file => "shared/#{file}")
end
return content
end
</pre>
On Oct 8, 8:05 am, Andrew Bloom
<akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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).
>
> On Oct 7, 3:24 pm, Daniel Waite
<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
> wrote:
>
> > Mozmonkey wrote:
> > > 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.
>
> > One way would be to simply wrap the includes with a conditional:
>
> > <head>
> > <% if
params[:controller].eql?(''certain_controller'') %>
> > <%= javascript_include_tag ''some-javascript''
%>
> > <% end %>
> > ...
> > </head>
>
> > That sort of code can get pretty ugly, so I''d put the whole
thing into a
> > helper. I''m sure someone here has a better way though... ^_^
> > --
> > Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Simpliest way I have found is to do this:- <%=render :partial=>"side_menu" rescue ""-%> This way if you have a _side_menu.rhtml file in the controller then it will use it otherwise display nothing. On Oct 9, 7:26 am, Mozmonkey <JeremyMail...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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: > > <pre> > # > # Retreive a partial that can be local to the view or in the shared > directory. > # > # = EXAMPLE - For a sidebar > # shared/_sidebar.rhtml - Default global sidebar > # search/_sidebar.rhtml - Used for views under search > # welcome/ (no sidebar.rhtml) - Pages under welcome/ use shared/ > _sidebar.rhtml > # > # = OPTIONS > # * :both=>true: This includes both the global and local version (if > it exists). The global will always be included first > # > def shared(name, options=false) > > content = "" > name = name.to_s > file = "_#{name}.rhtml"; > > # Render global > if options && options[:both] > content = render(:file => "shared/#{file}") > end > > # Check local > if File.exist?("#{File.dirname(__FILE__)}/../views/ > #{params[:controller]}/#{file}") > content << render(:partial => name) > elsif !options || !options[:both] > content = render(:file => "shared/#{file}") > end > > return content > end > </pre> > > On Oct 8, 8:05 am, Andrew Bloom <akbl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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). > > > On Oct 7, 3:24 pm, Daniel Waite <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > wrote: > > > > Mozmonkey wrote: > > > > 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. > > > > One way would be to simply wrap the includes with a conditional: > > > > <head> > > > <% if params[:controller].eql?(''certain_controller'') %> > > > <%= javascript_include_tag ''some-javascript'' %> > > > <% end %> > > > ... > > > </head> > > > > That sort of code can get pretty ugly, so I''d put the whole thing into a > > > helper. I''m sure someone here has a better way though... ^_^ > > > -- > > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---