Skip to content

Instantly share code, notes, and snippets.

@tcmacdonald
Created October 30, 2012 14:54
Show Gist options
  • Save tcmacdonald/3980678 to your computer and use it in GitHub Desktop.
Save tcmacdonald/3980678 to your computer and use it in GitHub Desktop.
Pulling front matter from Rails views template.
require 'active_support/concern'
module FrontMatter
extend ActiveSupport::Concern
included do
helper_method :front_matter
end
def front_matter
@front_matter ||= read_front_matter
end
def read_front_matter
full_path = "#{Rails.root}/app/views/#{request[:controller]}/#{request[:action]}.html.erb"
@content = File.read(full_path)
if @content =~ /^(<%# ---\s+)(.*?)(\s+--- %>)/m
@front_matter = YAML.load($2).symbolize_keys
remove_instance_variable(:@content)
end
@front_matter ||= {}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment