Class: Tilt::MustacheTemplate

Inherits:
Template
  • Object
show all
Defined in:
lib/sinatra/tilt.rb

Overview

Mustache is written and maintained by Chris Wanstrath. See: github.com/defunkt/mustache

When a scope argument is provided to MustacheTemplate#render, the instance variables are copied from the scope object to the Mustache view.

Instance Attribute Summary collapse

Attributes inherited from Template

#data, #file, #line, #options

Instance Method Summary collapse

Methods inherited from Template

#basename, #compile, #eval_file, #initialize, #name, #render

Constructor Details

This class inherits a constructor from Tilt::Template

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



446
447
448
# File 'lib/sinatra/tilt.rb', line 446

def engine
  @engine
end

Instance Method Details

#compile!Object



452
453
454
455
456
457
458
459
# File 'lib/sinatra/tilt.rb', line 452

def compile!
  Mustache.view_namespace = options[:namespace]
  @engine = options[:view] || Mustache.view_class(name)
  options.each do |key, value|
    next if %w[view namespace mustaches].include?(key.to_s)
    @engine.send("#{key}=", value) if @engine.respond_to? "#{key}="
  end
end

#evaluate(scope = nil, locals = {}, &block) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/sinatra/tilt.rb', line 461

def evaluate(scope=nil, locals={}, &block)
  instance = @engine.new

  # copy instance variables from scope to the view
  scope.instance_variables.each do |name|
    instance.instance_variable_set(name, scope.instance_variable_get(name))
  end

  # locals get added to the view's context
  locals.each do |local, value|
    instance[local] = value
  end

  # if we're passed a block it's a subview. Sticking it in yield
  # lets us use {{yield}} in layout.html to render the actual page.
  instance[:yield] = block.call if block

  instance.template = data unless instance.compiled?

  instance.to_html
end

#initialize_engineObject



448
449
450
# File 'lib/sinatra/tilt.rb', line 448

def initialize_engine
  require_template_library 'mustache' unless defined? ::Mustache
end