Frontmatter in layouts?

Can you set frontmatter in layouts? I’d like to set frontmatter values that are shared across many page templates without having to add frontmatter to each template individually.

Wouldn’t the local data work for you ? See: http://middlemanapp.com/advanced/local-data/

I was looking for another option since I’m investigating some contributed extensions that are looking for data in the frontmatter. If local data is best, then I guess I’ll have to do some customization to work with that.

The layout comes into play quite late during the rendering process, normally after the extensions has done their job, so that’s probably not a good place for your data. (For an overview of execution order, see http://thirteen23.com/garage/2012/09/middleman-extensions-and-execution-order/ )

Maybe a better strategy would be to write a simple extension that adds the data to the page, and make sure that your extension is activated before the contributed extensions you’re interested in. You would add your data in the manipulate_resource_list(resources) method.

See http://tech.bellycard.com/blog/custom-extensions-with-middleman/
and http://middlemanapp.com/advanced/custom/#toc_5 (Important: This function must return the resources list. This is not obvious from the code example in the documentation:

  def manipulate_resource_list(resources)
    resources.each do |resource|
      resource.destination_path.gsub!("original", "new")
    end

    resources       # <- Add this
  end

Also be aware that Middleman kind of has two places where frontmatter data is stored:

resource.data will give you the frontmatter read from the file. But this is read only, so if you want to manipulate or add frontmatter, you need to work with resource.metadata. I have however seen some cases where the extension directly reads the .data, ignoring the metadata, so if your extension does not have the intended effect this may be one place to look for an explanation.

1 Like