The key to this process is in the data/projects.yml file. Here is an excerpt:
projects:
-
title: Awesome Project 1
slug: project_1
images:
- awesomeproject1.png
meta_description: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
description: |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tristique ut quam non fermentum. In a sodales mi. Ut non ullamcorper ante, sit amet sagittis diam. Ut turpis orci, malesuada a lacus eu, fringilla faucibus felis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam nec est at nibh mattis mattis eget sit amet leo. Cras tristique vestibulum nisi. Fusce iaculis venenatis metus, vitae luctus nunc blandit eget. Integer et nunc sed augue varius varius eu et elit. Suspendisse eu nisl venenatis, vehicula mi eu, rhoncus mauris. Integer magna diam, sodales a volutpat vel, cursus posuere nisi.
-
title: Awesome Project 2
slug: project_2
images:
- awesomeproject2.png
- awesomeproject2_mobile.png
- awesomeproject2_detail.png
meta_description: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
description: |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tristique ut quam non fermentum. In a sodales mi. Ut non ullamcorper ante, sit amet sagittis diam. Ut turpis orci, malesuada a lacus eu, fringilla faucibus felis. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam nec est at nibh mattis mattis eget sit amet leo. Cras tristique vestibulum nisi. Fusce iaculis venenatis metus, vitae luctus nunc blandit eget. Integer et nunc sed augue varius varius eu et elit. Suspendisse eu nisl venenatis, vehicula mi eu, rhoncus mauris. Integer magna diam, sodales a volutpat vel, cursus posuere nisi.
In this file I am able to define as many meta attributes as I need. One important note: Because this is my own work, I can safely assume that all attributes will always be present, therefore I do not check for attribute existence in the hash.
I am using traditional slug style URLs. When config.rb loops through all of the records in projects.yml, it assigns the url with the slug. It also points to the specific layout that I am using and passes the attribute has in as a local variable:
data.work.projects.each do |project|
proxy "/#{project.slug}.html", "/project.html", :locals => { :project => project }, :ignore => true
end
I then apply the values of the attribute hash to the meta tags in the head section of the layout file like this excerpt:
%title Middleman Rocks! | #{project['title']}
%meta{content: project['meta_description'], name: "description"}/
%meta{content: "Middleman Rocks! | #{project['title']}", property: "og:title"}/
%meta{content: "http://middlemanrocks.com/#{project['slug']}", property: "og:url"}/
%meta{content: "http://middlemanrocks.com/img/#{project.images.first}", property: "og:image"}/
%meta{content: project['meta_description'], property: "og:description"}/
%link{href: "http://middlemanrocks.com/#{project['slug']}", rel: "canonical"}/
This could easily be extracted into a helper, I just didn’t need to DRY it up for this project.
Regarding Canonical URL’s, because this is a simple site, I use the generated slug for each page as the Canonical URL. It would also be an option to add a cannonical_url attribute to the YAML file and have that populate the meta tag.
Hope this helps!