Generate extra data for resources

Hello! I would like to develop a custom extension that would add some data to specific resources.

The extra data should be accessible to view, like current_page.data[:extra-data].

I am confused how to do that. Could you guys point me to the right direction?

Set with something like:

current_page.add_metadata( {'body_class' => 'blog'} )

Read with

current_page.metadata['body_class']

There are four predefined ‘categories’ of data/metadata: options, locals, page and blocks. You can use them like this:

current_page.add_metadata( :options => { :layout => 'sea' } )

Sometimes it is possible to use these predefined categories to overwrite stuff set in front matter (ie the stuff that’s normally accessed with current_page.data). For example,

current_page.add_metadata :options => { :layout => layout }

will override the setting of layout in front matter. But my experience is that this is needs to be checked from case to case, since it depends on if the developer has remembered to check not just data but also metadata (for example, you can not use this to overwrite the page title in the blog module).

Note that Middleman sometimes uses symbols and sometimes strings as keys.

1 Like

You can also do this inside templates, so if you have i hierarchy of templates one can set (using haml) the value:

`- current_page.add_metadata( {‘body_class’ => ‘blog’} )``

and a template higher up in the hierarchy can read it

- current_page.metadata['body_class']

thus making it easier to use general templates for the higher levels.

Wow thanks @tommysundstrom! These are more than enough to get started!