Class: Reins::View

Inherits:
Object
  • Object
show all
Includes:
Routes::UrlHelpers, Forms, Helpers
Defined in:
lib/reins/view.rb,
lib/reins/view/forms.rb,
lib/reins/view/helpers.rb

Defined Under Namespace

Modules: Forms, Helpers

Constant Summary collapse

DEFAULT_LAYOUT =
"application".freeze
VIEWS_ROOT =
"app/views".freeze

Constants included from Helpers

Helpers::VOID_ELEMENTS

Instance Method Summary collapse

Methods included from Forms

#form_with, #hidden_field, #label, #submit, #text_area, #text_field

Methods included from Helpers

#image_tag, #javascript_include_tag, #link_to, #stylesheet_link_tag, #tag, #url_for

Methods included from Routes::UrlHelpers

define_for, expand_path, reset!

Instance Method Details

#content_for(name, value = nil, &block) ⇒ Object



53
54
55
56
57
# File 'lib/reins/view.rb', line 53

def content_for(name, value = nil, &block)
  @sections ||= {}
  @sections[name.to_s] = block ? block.call : value
  nil
end

#evaluate(template, locals = {}) ⇒ Object

rubocop:enable Naming/AccessorMethodName



25
26
27
28
29
30
# File 'lib/reins/view.rb', line 25

def evaluate(template, locals = {}, &)
  eruby = Erubis::EscapedEruby.new(template)
  bind = binding
  locals.each { |k, v| bind.local_variable_set(k, v) }
  eval(eruby.src, bind) # rubocop:disable Security/Eval
end

#h(str) ⇒ Object



59
60
61
# File 'lib/reins/view.rb', line 59

def h(str)
  CGI.escapeHTML(str)
end

#render(*args, **kwargs) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/reins/view.rb', line 43

def render(*args, **kwargs)
  partial_path, render_locals, collection = parse_render_args(args, kwargs)

  if collection
    collection.map { |item| render_partial(partial_path, single_local(partial_path, item)) }.join
  else
    render_partial(partial_path, render_locals)
  end
end

#render_template(path, locals: {}, layout: :default) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/reins/view.rb', line 32

def render_template(path, locals: {}, layout: :default)
  @sections ||= {}
  inner = evaluate(read_template(path), locals)
  layout_name = resolve_layout(layout)
  return inner unless layout_name

  evaluate(read_template("layouts/#{layout_name}"), locals) do |section = nil|
    section ? @sections[section.to_s] : inner
  end
end

#set_vars(instance_vars) ⇒ Object

rubocop:disable Naming/AccessorMethodName



18
19
20
21
22
# File 'lib/reins/view.rb', line 18

def set_vars(instance_vars)
  instance_vars.each do |name, value|
    instance_variable_set(name, value)
  end
end