Created
February 11, 2011 04:12
-
-
Save Gregg/821906 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # if we want to effectively leverage SEO on our website, we'll want to provide custom title, description, and keywords on some of the pages... one way we can do this is: | |
| # <!DOCTYPE html> | |
| # <html> | |
| # <head> | |
| # <title>Big Store <%= @title %></title> | |
| # <meta name="description" content="<%= @description || "Selling the best chaps ..." %>"> | |
| # <meta name ="keywords" content="<%= @keywords || "chaps, cowboy shop, leather" %>"> | |
| class ProductsController < ApplicationController | |
| def index | |
| @product = Product.find(params[:id]) | |
| @title = @product.name | |
| @description = @product.description | |
| @keywords = @product.tags.join(",") | |
| end | |
| end | |
| # However, it kinda feels a little sloppy to put view concerns inside your controller | |
| # Here's an alternative | |
| # <!DOCTYPE html> | |
| # <html> | |
| # <head> | |
| # <title>Big Store <%= yield(:title) %></title> | |
| # <meta name="description" content="<%= yield(:description) || "Selling the best chaps ..." %>"> | |
| # <meta name ="keywords" content="<%= yield(:keywords) || "chaps, cowboy shop, leather" %>"> | |
| # Now if we want to set title, description, or keywords, we can do so from anywhere in our views | |
| # <% | |
| # content_for(:title) { @product.name } | |
| # content_for(:description) { @product.description } | |
| # content_for(:keywords) { @product.tags.join(",") } | |
| # %> | |
| # it might be worthwhile to extract these into helpers... In our helper: | |
| def title(title) | |
| content_for(:title) { title } | |
| end | |
| def description(description) | |
| content_for(:description) { description } | |
| end | |
| def keywords(input) | |
| content_for(:keywords) { input } | |
| end | |
| # Now in our view... all we have to do is | |
| # <% | |
| # title @product.name | |
| # description @product.description | |
| # keywords @product.tags.join(",") | |
| # %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment