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
---
Here's the simplest, modular solution.
In your layout, throw in:
%title= 'Your Site Title | ' + @title
@title is a ruby instance variable that will be available to the current page.
In your current page, throw in (dont forget the dash):
- @title = 'Your Page Title'
Then you should be good to go!
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)
I've found another way to work around that limitation. Instead of going for the page data, get the frontmatter data from the metadata:
%title = current_page.metadata[:page]['title']
It's not as nice and short as getting the data through current_page.data, but I've only managed to change the metadata:
proxy newpath, oldpath, :page => { 'title' => newtitle }
I'm not sure why but if I remember correct I had to also set the metadata resource afterwards on ready:
resource.add_metadata :page => { 'title' => newtitle }
I still think/hope that there must be a better solution. It's kind of totally weird that we can't set frontmatter data for proxy pages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With