Skip to content

Commit

Permalink
Automagic attr_readers for ivars you set in Sinatra routes.
Browse files Browse the repository at this point in the history
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
defunkt committed Feb 24, 2011
1 parent d692d78 commit e48a818
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/mustache/sinatra.rb
Expand Up @@ -88,6 +88,11 @@ def mustache(template, options={}, locals={})
# Copy instance variables set in Sinatra to the view # Copy instance variables set in Sinatra to the view
instance_variables.each do |name| instance_variables.each do |name|
instance.instance_variable_set(name, instance_variable_get(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 end


# Render with locals. # Render with locals.
Expand Down

0 comments on commit e48a818

Please sign in to comment.