Module: Sinatra::Templates

Defined in:
lib/phlex-sinatra.rb

Instance Method Summary collapse

Instance Method Details

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



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
66
67
68
69
70
71
72
73
74
# File 'lib/phlex-sinatra.rb', line 41

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

  content_type ||= :svg if obj.is_a?(Phlex::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
    raise Phlex::Sinatra::IncompatibleOptionError.new(
      'streaming is not compatible with layout'
    ) if layout

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

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