Module: Sinatra::RenderPlugin::RenderHelpers

Defined in:
lib/sinatra/render_plugin/render_helpers.rb

Instance Method Summary collapse

Instance Method Details

#erb_template(template_path, options = {}) ⇒ Object

Renders a erb template based on the relative path erb_template ‘users/new’



6
7
8
# File 'lib/sinatra/render_plugin/render_helpers.rb', line 6

def erb_template(template_path, options={})
  render_template template_path, options.merge(:template_engine => :erb)
end

#haml_template(template_path, options = {}) ⇒ Object

Renders a haml template based on the relative path haml_template ‘users/new’



12
13
14
# File 'lib/sinatra/render_plugin/render_helpers.rb', line 12

def haml_template(template_path, options={})
  render_template template_path, options.merge(:template_engine => :haml)
end

#partial(template, options = {}) ⇒ Object Also known as: render_partial

Partials implementation which includes collections support partial ‘photo/_item’, :object => @photo partial ‘photo/_item’, :collection => @photos



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sinatra/render_plugin/render_helpers.rb', line 27

def partial(template, options={})
  options.reverse_merge!(:locals => {}, :layout => false)
  path = template.to_s.split(File::SEPARATOR)
  object_name = path[-1].to_sym
  path[-1] = "_#{path[-1]}"
  template_path = File.join(path)
  raise 'Partial collection specified but is nil' if options.has_key?(:collection) && options[:collection].nil?
  if collection = options.delete(:collection)
    options.delete(:object)
    counter = 0
    collection.collect do |member|
      counter += 1
      options[:locals].merge!(object_name => member, "#{object_name}_counter".to_sym => counter)
      render_template(template_path, options.merge(:layout => false))
    end.join("\n")
  else
    if member = options.delete(:object)
      options[:locals].merge!(object_name => member)
    end
    render_template(template_path, options.merge(:layout => false))
  end
end

#render_template(template_path, options = {}) ⇒ Object

Renders a template from a file path automatically determining rendering engine render_template ‘users/new’ options = { :template_engine => ‘haml’ }



19
20
21
22
# File 'lib/sinatra/render_plugin/render_helpers.rb', line 19

def render_template(template_path, options={})
  template_engine = options.delete(:template_engine) || resolve_template_engine(template_path)
  render template_engine.to_sym, template_path.to_sym, options
end