Module: Angelo::Tilt::ERB

Defined in:
lib/angelo/tilt/erb.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_LAYOUT =
'layout.%s.erb'
DEFAULT_TYPE =
:html
LAYOUTS_DIR =
'layouts'
ACCEPT_ALL =
'*/*'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

hrm, sneaky



15
16
17
18
19
20
21
22
# File 'lib/angelo/tilt/erb.rb', line 15

def self.included base

  # TODO: remove at 0.4
  warn "[DEPRECATED] Angelo::Tilt::ERB will be included by default in angelo >= 0.4"
  raise "Angelo requires Tilt >= 2.0, you have #{::Tilt::VERSION}" unless ::Tilt::VERSION.to_i >= 2

  base.extend ClassMethods
end

Instance Method Details

#erb(view, opts = {locals: {}}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/angelo/tilt/erb.rb', line 63

def erb view, opts = {locals: {}}
  type = opts[:type] || template_type
  content_type type
  locals = Hash === opts[:locals] ? opts[:locals] : {}
  render = case view
           when String
             ->{ view }
           when Symbol
             ->{self.class.templates(type)[view].render self, locals}
           end
  case opts[:layout]
  when false
    render[]
  when Symbol
    if lt = self.class.layout_templates(type)[opts[:layout]]
      lt.render self, locals, &render
    else
      raise ArgumentError.new "unknown layout - :#{opts[:layout]}"
    end
  else
    if self.class.default_layout(type)
      self.class.default_layout(type).render self, locals, &render
    else
      render[]
    end
  end
end

#template_typeObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/angelo/tilt/erb.rb', line 91

def template_type
  accept = request.headers[ACCEPT_REQUEST_HEADER_KEY]
  mt = if accept.nil? or accept == ACCEPT_ALL
         MIME::Types[headers[CONTENT_TYPE_HEADER_KEY]]
       else
         MIME::Types[request.headers[ACCEPT_REQUEST_HEADER_KEY]]
       end
  mt.first.extensions.first.to_sym
rescue
  DEFAULT_TYPE
end