Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

middleman

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
---
like image 780
Jan Drewniak Avatar asked Oct 01 '12 10:10

Jan Drewniak


3 Answers

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!

like image 69
Joshua Plicque Avatar answered Nov 06 '22 18:11

Joshua Plicque


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)

like image 25
ghempton Avatar answered Nov 06 '22 16:11

ghempton


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.

like image 3
Betabong Avatar answered Nov 06 '22 18:11

Betabong