Module: Phlex::Sinatra

Defined in:
lib/phlex-sinatra.rb,
lib/phlex/sinatra/version.rb

Defined Under Namespace

Modules: SGMLOverrides Classes: TypeError

Constant Summary collapse

Error =
Class.new(StandardError)
ArgumentError =
Class.new(Error)
VERSION =
'0.5.0'

Instance Method Summary collapse

Instance Method Details

#phlex(obj, content_type: nil, layout: false, layout_engine: :erb, stream: false) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/phlex-sinatra.rb', line 32

def phlex(
  obj,
  content_type: nil,
  layout: false,
  layout_engine: :erb,
  stream: false
)
  raise TypeError.new(obj) unless obj.is_a?(SGML)

  if layout && stream
    raise ArgumentError.new('streaming is not compatible with layout')
  end

  content_type ||= :svg if obj.is_a?(SVG) && !layout
  self.content_type(content_type) if content_type

  # Copy Sinatra's behaviour and interpret layout=true as meaning "use the
  # default layout" - uses an internal Sinatra instance variable :s
  layout = @default_layout if layout == true

  if stream
    self.stream do |out|
      obj.call(out, context: self)
    end
  else
    output = obj.call(context: self)

    if layout
      render(layout_engine, layout, { layout: false }) { output }
    else
      output
    end
  end
end