Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
    end

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>
      </a>
    {{/ job}}
  • Loading branch information...
commit e48a818800032b7187e2bfb0fd70df20ed800cea 1 parent d692d78
@defunkt defunkt authored
Showing with 5 additions and 0 deletions.
  1. +5 −0 lib/mustache/sinatra.rb
View
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
end
# Render with locals.
Please sign in to comment.
Something went wrong with that request. Please try again.