Class: Snack::View
- Inherits:
-
Object
- Object
- Snack::View
- Defined in:
- lib/snack.rb
Instance Method Summary collapse
-
#initialize(template) ⇒ View
constructor
A new instance of View.
- #partial(path, locals = {}) ⇒ Object
-
#render ⇒ Object
return a view body or nil if adequate template cannot be found.
Constructor Details
#initialize(template) ⇒ View
Returns a new instance of View.
70 71 72 |
# File 'lib/snack.rb', line 70 def initialize(template) @template = template end |
Instance Method Details
#partial(path, locals = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/snack.rb', line 74 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 |
#render ⇒ Object
return a view body or nil if adequate template cannot be found
87 88 89 90 91 92 93 94 95 |
# File 'lib/snack.rb', line 87 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 |