Class: Deas::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/deas/template.rb

Direct Known Subclasses

Partial

Defined Under Namespace

Classes: Partial, Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sinatra_call, name, options = nil) ⇒ Template

Returns a new instance of Template.



9
10
11
12
13
14
15
16
# File 'lib/deas/template.rb', line 9

def initialize(sinatra_call, name, options = nil)
  @sinatra_call, @name, @options = sinatra_call, name.to_sym, (options || {})
  @options[:scope] = @sinatra_call.settings.deas_template_scope.new(@sinatra_call)

  (@options.delete(:layout) || @options.delete(:layouts) || []).tap do |l|
    @layouts = l.compact.map(&:to_sym)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/deas/template.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/deas/template.rb', line 7

def options
  @options
end

Instance Method Details

#engine(template_name) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/deas/template.rb', line 18

def engine(template_name)
  return 'erb' if @sinatra_call.settings.views.nil?

  views_path = Pathname.new(@options[:views] || @sinatra_call.settings.views)
  template = Dir.glob("#{views_path.join(template_name.to_s)}.*").first.to_s
  File.extname(template)[1..-1] || 'erb'
end

#render(&block) ⇒ Object

builds render-blocks like:

erb :main_layout do
  erb :second_layout do
    erb :user_index
  end
end


34
35
36
37
38
39
40
# File 'lib/deas/template.rb', line 34

def render(&block)
  template_names = [ @layouts, @name ].flatten.reverse
  top_render_proc = template_names.inject(block) do |render_proc, name|
    proc{ @sinatra_call.send(engine(name), name, @options.dup, &render_proc) }
  end
  top_render_proc.call
end