Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a variable to template in Middleman 2

Tags:

ruby

middleman

I have a Middleman project and I need to emulate a logged-in user.

Simple enough - would be fine to set some global variable like @user = 1 in config.rb, code everything for the logged-in user, then set the variable to 0 and code everything for the logged-out user putting if's everywhere

I'm not a Ruby coder so I don't understand where to hook in. So: how can I set a global app variable in a middleman's config.rb?

like image 678
Guard Avatar asked Mar 25 '12 14:03

Guard


1 Answers

You can set variable on specific paths using the page helper:

page "/my-page.html", :locals => { :is_logged_in => true }

If you wanted to use a single template, which include an if statement to handle content changes based on is_logged_in, you would use a page proxy:

page "/my-page-logged-in.html", :proxy => "/my-page.html", :locals => { :is_logged_in => true }
page "/my-page-logged-out.html", :proxy => "/my-page.html", :locals => { :is_logged_in => false }

For direct variables, use set:

set :is_logged_in, true

In template:

<%= is_logged_in %>
like image 71
Thomas Reynolds Avatar answered Sep 20 '22 12:09

Thomas Reynolds