Created
July 27, 2012 02:49
-
-
Save jhjguxin/3185931 to your computer and use it in GitHub Desktop.
Rails mete info on template (seo setting) base on BBTangCMS
This file contains 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
<!DOCTYPE html> | |
<html lang="zh-CN"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<!--%= yield(:title)%--> | |
<!--%= yield(:description)%--><!--%= yield(:keywords)%--> | |
<%= render_page_title %> | |
<%= raw("<meta name='keywords' content='#{@meta_keywords}' />") if @meta_keywords.present? %> | |
<%= raw("<meta name='description' content='#{@meta_description}' />") if @meta_description.present? %> | |
..... |
This file contains 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
# encoding: utf-8 | |
class ApplicationController < ActionController::Base | |
before_filter :set_seo_meta | |
def set_seo_meta(title = '',meta_keywords = '', meta_description = '') | |
g_obj = guessed_object | |
if g_obj.present? | |
[:title, :name, :nickname].each do |col| | |
@page_title = g_obj.send col if g_obj.respond_to? col | |
end | |
@meta_keywords = g_obj.tag_list.join(",") if g_obj.respond_to? :tag_list | |
@meta_description = g_obj.summary if g_obj.respond_to? :summary | |
@meta_description = g_obj.description if g_obj.respond_to? :description | |
end | |
if title.present? | |
@page_title = "#{title}" | |
end | |
@meta_keywords = meta_keywords if meta_keywords.present? | |
@meta_description = meta_description if meta_description.present? | |
end | |
def guessed_object | |
@model_class = params[:controller].classify.constantize if params[:controller].classify.class_exists? | |
@model_class = params[:controller].gsub(/\w+([\/]+)/,"").classify.constantize if params[:controller].gsub(/\w+([\/]+)/,"").classify.class_exists? | |
if @model_class.present? | |
@model_class.where(id: params[:id]).first if params[:id].present? | |
#guessed_instance_variable_name = "@#{@model_class.name.singularize.downcase}" | |
#if self.instance_variable_defined? guessed_instance_variable_name | |
# self.instance_variable_get guessed_instance_variable_name | |
#end | |
end | |
end | |
end |
This file contains 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
# encoding: utf-8 | |
module ApplicationHelper | |
def render_page_title() | |
default_title = "#{breadcrumbs.items.collect{|item| item.first}.join('--')} | #{Setting.app_name}" | |
if @page_title.present? | |
@page_title ="#{@page_title}@" + default_title | |
else | |
@page_title = default_title | |
end | |
content_tag("title", @page_title, nil, false) | |
end | |
#set the title for the page | |
def title(page_title) | |
content_for(:title) do | |
raw "<title>#{page_title} - #{Setting.app_name}</title>" | |
end | |
end | |
#set the description for the page | |
def description(page_description) | |
content_for(:description) do | |
raw "<meta name=\"description\" content=\"#{page_description}\" />\n" | |
end | |
end | |
#set the keywords for the page | |
def keywords(page_keywords) | |
content_for(:keywords) do | |
raw "<meta name=\"keywords\" content=\"#{page_keywords}\" />\n" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment