Module: Angelo::Tilt::ERB

Included in:
Base
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
# File 'lib/angelo/tilt/erb.rb', line 15

def self.included base
  base.extend ClassMethods
end

Instance Method Details

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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/angelo/tilt/erb.rb', line 90

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



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/angelo/tilt/erb.rb', line 118

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