Skip to content
Browse files

Automagic attr_readers for ivars you set in Sinatra routes.

This lets us have Sinatra routes like this:

    get '/badge' do
      @job = Jobs::Job.first
      mustache :badge, :layout => false

That work with a badge.mustache like this:

    {{# job}}
      <a class="job-link" href="{{url_with_host}}" target="_blank">
        <strong class="job-company">{{company}}</strong> is hiring a
        <strong class="job-title">{{title}}</strong> in
        <span class="job-location">{{location}}</span>
    {{/ job}}
  • Loading branch information...
1 parent d692d78 commit e48a818800032b7187e2bfb0fd70df20ed800cea @defunkt defunkt committed
Showing with 5 additions and 0 deletions.
  1. +5 −0 lib/mustache/sinatra.rb
5 lib/mustache/sinatra.rb
@@ -88,6 +88,11 @@ def mustache(template, options={}, locals={})
# Copy instance variables set in Sinatra to the view
instance_variables.each do |name|
instance.instance_variable_set(name, instance_variable_get(name))
+ # Automagic attr_readers for ivars you set in Sinatra routes.
+ if !instance.respond_to?(name)
+ (class << instance; self end).send(:attr_reader, name.to_s.sub('@',''))
+ end
# Render with locals.

0 comments on commit e48a818

Please sign in to comment.
Something went wrong with that request. Please try again.