Passing dynamic content to a template in Middleman

I create a static site using Middleman, which has a portfolio section of all the latest client projects. Thumbnail images of projects in the 3 X 3 gallery will be displayed in the portfolio section, and clicking on them will open their jointly responding html page inside the lightbox.

The layout for the pages inside the light bar is the same, and not the layout of each individual page, I thought that for Middleman the content submitted from the yaml data file (projects.yml) would be processed using [link.

Here is what I have in the config.rb file

### # Page options, layouts, aliases and proxies ### # A path which all have the same layout with_layout :popup do page "/projects/*" end # Proxy (fake) files # page "/this-page-has-no-template.html", :proxy => "/template-file.html" do # @which_fake_page = "Rendering a fake page with a variable" # end data.projects.details.each do |pd| proxy "/projects/#{pd[:client_name]}.html", "/projects/template.html", locals: { project: pd }, ignore: true end 
+4
source share
1 answer

Okay, so after some digging, I came across two posts that helped me understand how dynamic pages work with a middleman. (Unfortunately, not many examples of doco and Middleman for dynamic pages are really basic)

http://benfrain.com/understanding-middleman-the-static-site-generator-for-faster-prototyping/ http://forum.middlemanapp.com/discussion/134/best-way-to-use-yaml- same-html-but-parameter-driven-data-fixed / p1

My decision...

data / projects.yml (contains project information)

 details: - client: "Company X" title: "Company X Event" video_url: "" logo: - "logo_companyx.gif" image_path: "/img/projects/companyx" total_images: 10 content: "<p>Blah blah blah</p>" responsibilities: "<li>Something</li> <li>Some task</li>" 

config.rb:

 data.projects.details.each do |pd| proxy "/projects/#{pd[:client]}.html", "/projects/template.html", :layout => false, :locals => { :project => pd }, :ignore => true end 

The trick with the fragment above transfers the entire project data object to the template through a proxy server using locales and sets the layout to false so that it does not inherit the default site layout (since I - or the client rather - want to display them in a pop-up lightbox )

The last step of the process was to create /projects/template.html.erb (in the source folder), declaring the following at the top of the template

 <% p = locals[:project] %> 

This allowed me to output every property of the p object in template.html.erb.

eg:

 <%= p[:title] %> 

I hope this helps someone, as it took me several days to play, and ANY search on the Internet, for example, or tips.

+13
source

All Articles