Access expose in layout

Hello,

I exposed information about if a user is logged in in the base action.rb. I can access those exposes in the template, but not the layout. Is there. a trick I am missing to access the exposed variables in the layout?

# app/view.rb
    expose :authenticated
    expose :current_user

In the individual page the values render

#app/templates/health/status.html.erb
<% if authenticated %>
  <%= current_user.display_name %>
<% else %>
  <%= link_to "Login", routes.path(:user_login) %>
<% end %>

In the layout I get an error about an unknown variable authenticated

#app/templates/layouts/app.html.erb
<% if authenticated %>
  <%= current_user.display_name %>
<% else %>
  <%= link_to "Login", routes.path(:user_login) %>
<% end %>

Hey Carolyn, it’s been a while. :wave:

Yes, you need to use the Context for this. Here’s an example (pay attention to how I define the #page_title method) in my Milestoner gem which uses Hanami View for static site generation (i.e. release notes). This then gets used in the page.html.erb layout.

Hi Brooke!

Thank you! The context was exactly what I needed.

1 Like