Class: Deas::Template
- Inherits:
-
Object
- Object
- Deas::Template
- Defined in:
- lib/deas/template.rb
Direct Known Subclasses
Defined Under Namespace
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #engine(template_name) ⇒ Object
-
#initialize(sinatra_call, name, options = nil) ⇒ Template
constructor
A new instance of Template.
-
#render(&block) ⇒ Object
builds render-blocks like:.
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, = nil) @sinatra_call, @name, @options = sinatra_call, name.to_sym, ( || {}) @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
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/deas/template.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/deas/template.rb', line 7 def @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 |