class Stache::Mustache::View

A Convienent Base Class for the views. Subclass this for autoloading magic with your templates.

Attributes

view[RW]
virtual_path[RW]

Public Class Methods

h()
Alias for: helpers
helpers() click to toggle source
# File lib/stache/mustache/view.rb, line 61
def helpers
  Stache::ViewContext.current
end
Also aliased as: h

Public Instance Methods

context() click to toggle source
# File lib/stache/mustache/view.rb, line 10
def context
  # Use the faster context instead of the original mustache one
  @context ||= FasterContext.new(self)
end
h()
Alias for: helpers
helpers() click to toggle source
# File lib/stache/mustache/view.rb, line 55
def helpers
  self.class.helpers
end
Also aliased as: h
method_missing(method, *args, &block) click to toggle source
# File lib/stache/mustache/view.rb, line 15
def method_missing(method, *args, &block)
  view.send(method, *args, &block)
end
partial(name) click to toggle source

Redefine where Stache::View templates locate their partials

# File lib/stache/mustache/view.rb, line 34
def partial(name)
  cache_key = :"#{virtual_path}/#{name}"

  # Try to resolve template from cache
  template_cached = ::Stache.template_cache.read(cache_key, :namespace => :partials, :raw => true)
  curr_template   = template_cached || Stache::Mustache::CachedTemplate.new(
    begin # Try to resolve the partial template
      template_finder(name, true)
    rescue ActionView::MissingTemplate
      template_finder(name, false)
    end.source
  )

  # Store the template
  unless template_cached
    ::Stache.template_cache.write(cache_key, curr_template, :namespace => :partials, :raw => true)
  end

  curr_template
end
respond_to?(method, include_private=false) click to toggle source
Calls superclass method
# File lib/stache/mustache/view.rb, line 19
def respond_to?(method, include_private=false)
  super(method, include_private) || view.respond_to?(method, include_private)
end
virtual_path=(path) click to toggle source
# File lib/stache/mustache/view.rb, line 23
def virtual_path=(path)
  @virtual_path = path
  #
  # Since the addition to the lookup_context only depends on the virtual_path,
  # do it here instead of inside the partial.
  #
  current_dir   = Stache.template_base_path.join(path.split("/")[0..-2].join("/"))
  lookup_context.view_paths << current_dir unless lookup_context.view_paths.include?(current_dir)
end

Protected Instance Methods

template_finder(name, partial) click to toggle source
# File lib/stache/mustache/view.rb, line 68
def template_finder(name, partial)
  if ActionPack::VERSION::MAJOR == 3 && ActionPack::VERSION::MINOR < 2
    lookup_context.find(name, [], partial)
  else # Rails 3.2 and higher
    lookup_context.find(name, [], partial, [], { formats: [:html], handlers: [:mustache] })
  end
end