Search code examples
rubymiddleman

Using MIddleman 3.0 - How do I set individual page titles on dynamic pages?


I'm putting together a simple portfolio site in middleman. I'm generate the 'work' pages dynamically based on local YAML data. This is in the config.rb:

data.work.projects.each do |project|
  page "/work/#{project[0]}.html", :proxy => "project_template.html" do
    @project = project
  end
end

For SEO purposes, I would like each one of these dynamically generated pages to have a unique page title and description.

The title is currently set in the layout file like this

  %title
    = current_page.data.title

and I know I can use frontmatter to set current_page variables like this

---
title: "Recent Work - "
---

And I can stick that into my project_template.haml, but is there any way to get something like this to work?

---
title: "Recent Work - " + @project.title
---

Solution

  • Instead of setting the title in the frontmatter (like you are doing), you could use content_for.

    E.g. in the layout:

    %title= yield_content(:title)

    And in the template of the dynamic page:

    - content_for(:title, @project.title)