Module: Sinatra::Templates

Included in:
Base
Defined in:
lib/sinatra/base.rb

Instance Method Summary collapse

Instance Method Details

#builder(template = nil, options = {}, &block) ⇒ Object



271
272
273
274
275
276
# File 'lib/sinatra/base.rb', line 271

def builder(template=nil, options={}, &block)
  require 'builder' unless defined? ::Builder
  options, template = template, nil if template.is_a?(Hash)
  template = lambda { block } if template.nil?
  render :builder, template, options
end

#erb(template, options = {}) ⇒ Object



234
235
236
237
# File 'lib/sinatra/base.rb', line 234

def erb(template, options={})
  require 'erb' unless defined? ::ERB
  render :erb, template, options
end

#haml(template, options = {}) ⇒ Object



249
250
251
252
253
# File 'lib/sinatra/base.rb', line 249

def haml(template, options={})
  require 'haml' unless defined? ::Haml
  options[:options] ||= self.class.haml if self.class.respond_to? :haml
  render :haml, template, options
end

#lookup_layout(engine, options) ⇒ Object



218
219
220
221
222
223
224
225
226
# File 'lib/sinatra/base.rb', line 218

def lookup_layout(engine, options)
  return if options[:layout] == false
  options.delete(:layout) if options[:layout] == true
  template = options[:layout] || :layout
  data = lookup_template(engine, template, options)
  [template, data]
rescue Errno::ENOENT
  nil
end

#lookup_template(engine, template, options = {}) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/sinatra/base.rb', line 201

def lookup_template(engine, template, options={})
  case template
  when Symbol
    if cached = self.class.templates[template]
      lookup_template(engine, cached, options)
    else
      ::File.read(template_path(engine, template, options))
    end
  when Proc
    template.call
  when String
    template
  else
    raise ArgumentError
  end
end

#render(engine, template, options = {}) ⇒ Object



190
191
192
193
194
195
196
197
198
199
# File 'lib/sinatra/base.rb', line 190

def render(engine, template, options={})
  data = lookup_template(engine, template, options)
  output = __send__("render_#{engine}", template, data, options)
  layout, data = lookup_layout(engine, options)
  if layout
    __send__("render_#{engine}", layout, data, options) { output }
  else
    output
  end
end

#render_builder(template, data, options, &block) ⇒ Object



278
279
280
281
282
283
284
285
286
# File 'lib/sinatra/base.rb', line 278

def render_builder(template, data, options, &block)
  xml = ::Builder::XmlMarkup.new(:indent => 2)
  if data.respond_to?(:to_str)
    eval data.to_str, binding, '<BUILDER>', 1
  elsif data.kind_of?(Proc)
    data.call(xml)
  end
  xml.target!
end

#render_erb(template, data, options, &block) ⇒ Object



239
240
241
242
243
244
245
246
247
# File 'lib/sinatra/base.rb', line 239

def render_erb(template, data, options, &block)
  data = data.call if data.kind_of? Proc
  instance = ::ERB.new(data)
  locals = options[:locals] || {}
  locals_assigns = locals.to_a.collect { |k,v| "#{k} = locals[:#{k}]" }
  src = "#{locals_assigns.join("\n")}\n#{instance.src}"
  eval src, binding, '(__ERB__)', locals_assigns.length + 1
  instance.result(binding)
end

#render_haml(template, data, options, &block) ⇒ Object



255
256
257
258
# File 'lib/sinatra/base.rb', line 255

def render_haml(template, data, options, &block)
  engine = ::Haml::Engine.new(data, options[:options] || {})
  engine.render(self, options[:locals] || {}, &block)
end

#render_sass(template, data, options, &block) ⇒ Object



266
267
268
269
# File 'lib/sinatra/base.rb', line 266

def render_sass(template, data, options, &block)
  engine = ::Sass::Engine.new(data, options[:sass] || {})
  engine.render
end

#sass(template, options = {}, &block) ⇒ Object



260
261
262
263
264
# File 'lib/sinatra/base.rb', line 260

def sass(template, options={}, &block)
  require 'sass' unless defined? ::Sass
  options[:layout] = false
  render :sass, template, options
end

#template_path(engine, template, options = {}) ⇒ Object



228
229
230
231
232
# File 'lib/sinatra/base.rb', line 228

def template_path(engine, template, options={})
  views_dir =
    options[:views_directory] || self.options.views || "./views"
  "#{views_dir}/#{template}.#{engine}"
end