SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Where's my title at? dreaz's Avatar
    Join Date
    Apr 2004
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to pass titles and other stuff to layouts

    I was just wondering, what's everyone favourite way to pass stuff like titles and stuff like that to layouts?

    Do you set it on the view or on the controller method? Do you use content_for or a simple variable? How would you go about passing some kind of info that will be used on all methods of the controller (for example, controller PeopleController should tell the layout to also use the CSS people.css)?

    Leave your thoughts...

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For CSS, I use this little helper:

    Code:
      def stylesheet_include_tag(*sources)
        if sources.include?(:controller)
          sources.delete(:controller)
          sources.push(controller_stylesheet_source) if stylesheet_exists(controller_stylesheet_source)
        end
        if sources.include?(:action)
          sources.delete(:action)
          sources.push(action_stylesheet_source) if stylesheet_exists(action_stylesheet_source)
        end
        if sources.include?(:defaults)
          sources.delete(:defaults)
          sources.unshift('application')
          sources.push(controller_stylesheet_source) if stylesheet_exists(controller_stylesheet_source)
          sources.push(action_stylesheet_source) if stylesheet_exists(action_stylesheet_source)
        end
        sources.collect { |source|
          path = "/stylesheets/#{source}.css"
          tag('link', { 'type' => 'text/css', 'rel' => 'stylesheet', 'href' => path})
        }.join("\n")
      end
    
    protected
          def controller_stylesheet_source
          params[:controller]
        end
        
        def action_stylesheet_source
          [params[:controller], params[:action]].join("_")
        end
        
        def stylesheet_path(source)
          "#{RAILS_ROOT}/public/stylesheets/#{source}.css"
        end
        
        def stylesheet_exists(source)
          File.exists?(stylesheet_path(source))
        end
    You can put the above in your ApplicationHelper or a separate file and include it in your ApplicationHelper.

    You can now use stylesheet_include_tag in your layout - it takes three options; :controller, :action and :defaults.

    If you add :controller, it will look for /public/stylesheets/controllername.css and if it finds one it will automatically include it.

    If you add :action, it will look for /public/stylesheets/controllername_actionname.css and if it finds one it will automatically include it.

    Finally, there is :defaults will look for both controller and action stylesheets, as well as a stylesheet called application.css.

    By using the above helper you don't need to worry about specifying stylesheets - just create one using the above naming schemes and it will be included.

  3. #3
    Where's my title at? dreaz's Avatar
    Join Date
    Apr 2004
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath
    You can put the above in your ApplicationHelper or a separate file and include it in your ApplicationHelper.

    You can now use stylesheet_include_tag in your layout - it takes three options; :controller, :action and :defaults.

    If you add :controller, it will look for /public/stylesheets/controllername.css and if it finds one it will automatically include it.

    If you add :action, it will look for /public/stylesheets/controllername_actionname.css and if it finds one it will automatically include it.

    Finally, there is :defaults will look for both controller and action stylesheets, as well as a stylesheet called application.css.

    By using the above helper you don't need to worry about specifying stylesheets - just create one using the above naming schemes and it will be included.
    Thanks Luke, that's really handy

    What about titles, to be used on the <title> tags for example? I'm thinking the best way to go is set it on the top of the view file, like this:

    Code:
    <% @page_title = 'List People' %>
    
    normal view stuff...
    Or would it best to set it on the controller and why?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd set it in the view. Text is UI too, so it belongs in the view.

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2005
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For CSS, I add an ID to the body based on the action and controller name, ala:

    Code:
    <body id=<%= controller.controller_name %>_<%= controller.action_name %>">
    If the action was named new and the controller was named article, to reference something on that particular page I would use:

    Code:
    #article_new{ ... }
    For page titles, I believe the best approach is:

    Code:
    <title><%= @page_title or "Generic Title" %></title>
    As long as that is in your layout you can set the page title from any action or controller by giving @page_title a value.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •