Module: Sinatra::Bldr::Helpers

Defined in:
lib/sinatra/bldr.rb

Instance Method Summary collapse

Instance Method Details

#bldr(template, opts = {}, &block) ⇒ Object

Wrapper for Tilt’s ‘render` method

We use this to properly set the scope the template gets rendered within to a ‘Bldr::Node` object and pass in local variables.

Examples:

Render a template in a file

get '/users/:id' do
  user = User.find(params['id'])
  bldr :'users/public.bldr', :locals => {:user => user}
end

Parameters:

  • template (String, Symbol)

    the template to render Can be a relative file location or a string template. The template may also be passed in as the block argument to this method, in which case, template argument is nil.

  • opts (Hash) (defaults to: {})

    a hash of options

Options Hash (opts):

  • :locals (Hash)

    a hash of local variables to be used in the template



25
26
27
28
29
30
31
32
33
# File 'lib/sinatra/bldr.rb', line 25

def bldr(template, opts = {}, &block)
  opts[:parent] = self
  opts[:scope] = ::Bldr::Node.new(nil, opts.merge(:views => (settings.views || "./views")))

  locals = opts.delete(:locals) || {}

  # @todo add support for alternate formats, like plist
  MultiJson.encode render(:bldr, template, opts, locals, &block).result
end