Trouble with Dynamic Pages / Proxies

Hey All,

I’m trying to generate pages from some local data files and can’t seem to figure out what I’m doing wrong. This is for a new portfolio website I’m building and first time trying to use this feature.

The data file lives in /data/work.yml and looks something like this (with more fields, but keeping it simple for now):

- name: Client 1
  slug: client-1
- name: Client 2
  slug: client-2
- name: Client 3
  slug: client-3

My config.rb looks like this (from the thoughtbot middleman template):

activate :aria_current
activate :autoprefixer

set :css_dir, "assets/stylesheets"
set :fonts_dir, "assets/fonts"
set :images_dir, "assets/images"
set :js_dir, "assets/javascripts"
set :markdown,
  autolink: true,
  fenced_code_blocks: true,
  footnotes: true,
  highlight: true,
  smartypants: true,
  strikethrough: true,
  tables: true,
  with_toc_data: true
set :markdown_engine, :redcarpet

page "/*.json", layout: false
page "/*.txt", layout: false
page "/*.xml", layout: false

activate :meta_tags
activate :directory_indexes

data.work.each do |work|
  proxy "/work/#{work.slug}/index.html", "/layouts/work.erb",
    locals: { work_name: work }
end

configure :development do
  activate :livereload do |reload|
    reload.no_swf = true
  end
end

configure :production do
  activate :asset_hash
  activate :gzip
  activate :minify_css
  activate :minify_html
  activate :minify_javascript
end

Every time I try running middleman I get the following:

{redacted}/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/middleman-core-4.2.1/lib/middleman-core/sitemap/extensions/proxies.rb:89:in `target_resource': Path work/client-1/index.html proxies to unknown file layouts/work.erb:["assets/fonts/.keep", "assets/fonts/ionicons.eot", "assets/fonts/ionicons.svg", "assets/fonts/ionicons.ttf", "assets/fonts/ionicons.woff", {etc etc} (RuntimeError)

Can anyone lend a hand? Have been banging my head on this for a while : )

  1. Have you defined a template file /layouts/work.erb for your proxies?
  2. I’m not sure if this can be located in the layouts directory, which is supposed to host the global layouts. Try to move it to work directory just to test if it helps.
  3. Do you intentionally left the template to be rendered as proxy too? If not, then ad ignore: true at the end of argument list:
proxy "/work/#{work.slug}/index.html", "/layouts/work.erb", locals: { work_name: work }, ignore: true

as per documentation.

  1. Just out of curiosity, shouldn’t it be: locals: { work_name: work.name }?
1 Like

Yeah, you were right. I just figured this out about an hour ago.

Turns out proxy layouts cannot exist in the layouts folder and have to be moved elsewhere. They also should have the .html.erb as the extension vs .html or .erb. The following changes made this work. Apparently this is a bug: https://github.com/middleman/middleman/issues/1226

  1. Moved layouts/work.erb to templates/work.html.erb
  2. Changed config code to look like this: (I accidentally left _name in there when I posted - I want access to all the yml variables vs one)
data.work.each do |work|
  proxy "/work/#{work.slug}/index.html", "/templates/work.html",
  locals: { work: work },
  ignore: true
end

Now it works! Hopefully helpful to someone else who runs into this issue. Thanks for the help!

Nice to have it sorted out. But one more question: the template file is work.erb.html or work.html.erb? The docs suggest the latter.

The latter, work.html.erb.