Module: Serve::RenderHelpers

Included in:
ViewHelpers
Defined in:
lib/serve/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#render(partial, options = {}) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/serve/view_helpers.rb', line 119

def render(partial, options={})
  if partial.is_a?(Hash)
    options = options.merge(partial)
    partial = options.delete(:partial)
  end
  template = options.delete(:template)
  case
  when partial
    render_partial(partial, options)
  when template
    render_template(template)
  else
    raise "render options not supported #{options.inspect}"
  end
end

#render_partial(partial, options = {}) ⇒ Object



135
136
137
# File 'lib/serve/view_helpers.rb', line 135

def render_partial(partial, options={})
  render_template(partial, options.merge(partial: true))
end

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



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/serve/view_helpers.rb', line 139

def render_template(template, options={})
  path = parser.template_path
  if template =~ %r{^/}
    template = template[1..-1]
    path = @root_path
  end
  filename = template_filename(path, template, partial: options[:partial])
  if filename && File.file?(filename)
    parser.parse(File.read(filename), File.extname(filename).split(".").last, options[:locals])
  else
    raise "File does not exist #{filename.inspect}"
  end
end