I have a Middleman application that uses Frontmatter data heavily. Instead populating a template file with Markdown, I have pretty much everything defined in the Frontmatter and then the layout file handles all the formatting.
Because erb is not evaluated before the Frontmatter is parsed (gripes!), I needed a way to do some processing on the current_page.data
values in a layout.
First Some Background…
I have a helper method for looking up pages using their type
and id
Frontmatter attributes. Something like the following:
# /helpers/page_helpers.rb
module PageHelpers
def get_page(type, id)
# searches the sitemap for resource that has the matching :type and :id Frontmatter values
end
end
A simplified version of two page templates with their Frontmatter:
source/topics/something-cool.html.erb
:
---
type: topic
id: something_cool
title: This is Something Cool
---
source/contact/chat.html.erb
:
---
type: contact
id: chat
title: Chat Now
intro: Fill out the form below to chat with us. Or you can learn more here.
---
Ok, Now the Issue
I would now like the ability to link to the /topic/something-cool
page from within the Frontmatter of the contact/chat
page. Something like the following:
---
type: contact
id: chat
title: Chat Now
intro: Fill out the form below to chat with us. Or you can [link type="topic" id="something_cool"]learn more here.[/link]
---
I found the Shortcode gem and thought it may be a good fit. In my layout I would have another helper that would handle parsing these defined shortcodes.
After including the gem in my Gemfile
I included the following in config.rb
:
Shortcode.setup do |config|
config.template_path = 'shortcodes'
config.block_tags = [:link]
config.helpers = [Padrino::Helpers]
end
I also have the following shortcode template defined in shortcodes/link.erb
:
<%= link_to @content, get_page(@attributes[:type], @attributes[:id]) %>
I have access to the link_to
method in my shortcode template because I have included the Padrino::Helpers
module in my shortcode configuration. However, I’m not seeing how I can include my PageHelpers
module. I tried the following:
Shortcode.setup do |config|
...
config.helpers = [Padrino::Helpers, PageHelpers]
end
But I get the following when starting up the server:
$ middleman server
.../config.rb:154:in `block in initialize': uninitialized constant Middleman::Application::MiddlemanApplication1::PageHelpers (NameError)
My Questions
- How can I tell the
Shortcode
config about thePageHelpers
modules (and others located in thehelpers/*.rb
directory? - I also have a simple custom extension which I have defined in
config.rb
. It provides a helper method. How would I also tell myShortcode
config about this helper method?