Class: Snack::View

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

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ View

Returns a new instance of View.



72
73
74
# File 'lib/snack.rb', line 72

def initialize(template)
  @template = template
end

Instance Method Details

#partial(path, locals = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/snack.rb', line 76

def partial(path, locals = {})
  filepath = File.join File.dirname(@template), path.to_s
  # insert a '_' before the filename before we search
  _partial = Dir["#{filepath.sub(/\/(?!.*\/)/, '/_')}*"].first

  if _partial
    Tilt.new(_partial).render(self, locals)
  else
    raise "-: Snack :- Unable to locate partial at: '#{filepath}'"
  end
end

#renderObject

return a view body or nil if adequate template cannot be found



89
90
91
92
93
94
95
96
97
# File 'lib/snack.rb', line 89

def render
  template_body = Tilt.new(@template).render(self)
  if @layout
    layout = Dir[File.join(File.dirname(@template), @layout) + '*'].first
    raise "-: Snack :- Unable to locate layout at: '#@layout'" unless layout
    @body = Tilt.new(layout).render(self) { template_body }
  end
  @body || template_body
end