phlex-sinatra 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6281c981ddfb5118f6a6f0a38297dee0152e7052dc29aab21284de40814acfbb
4
- data.tar.gz: 4d56528737f86832f8113ae8da2c05b0b41ef0643d7dc8f8a36d435e0ec7f00a
3
+ metadata.gz: a78afcc1302f4df83e718925f4dd189fd6270a83f6a0bd1a3fd14c480e15131e
4
+ data.tar.gz: 3337dfdb6dc398e901222b50c1b51fb480070204298b406c486238b4c0c8a58c
5
5
  SHA512:
6
- metadata.gz: c818ef9cdee0f1bc70e2a8cdb98956c7e83313e2774d09330c6487a9c1f3f2b504c7aae5e57d5fefa7693311b609a3f94c16aa6caa63f18b76135f3c304d7309
7
- data.tar.gz: c67a8d2d7d4b3b279fa812eb173b6c854af7c87d587894a6907059238e7c3df21daec28d2d0b14a2a600961b68f39deffe98d150bc7a2f0dce5386779e081893
6
+ metadata.gz: ddc9255d6c867f6bd67c349df61e05c0bc79af68a3ede5be8217bee8ae42b6b6f08c40064f43d956eec2ab82007742608d948b071c3efe448c3ca1196c1a626a
7
+ data.tar.gz: 62e813a5c2523581b1eecc0fd3b02549d698454433f49618b5b3500684fc7fad8018b7a90d245b80711ce80bb98b4b8091cfdfe8ce8dc2c0fd26b7d2610fa6c6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## Version 0.4.0 - 2024-09-10
2
+
3
+ - Add support for wrapping a Phlex view in a layout. Pass `layout: true` to use Sinatra's default layout or specify the view by passing a symbol. Defaults to ERB and other Sinatra templating languages can be specified via the `layout_engine:` keyword.
4
+
1
5
  ## Version 0.3.0 - 2023-12-13
2
6
 
3
7
  - Add support for streaming a view. Pass `stream: true` to the `#phlex` helper so Phlex will use Sinatra's streaming capability.
data/Gemfile CHANGED
@@ -6,6 +6,7 @@ source 'https://rubygems.org'
6
6
  gemspec
7
7
 
8
8
  gem 'capybara'
9
+ gem 'haml'
9
10
  gem 'puma'
10
11
  gem 'rack-test'
11
12
  gem 'rake'
data/README.md CHANGED
@@ -24,7 +24,7 @@ You can now use Sinatra's `url()` helper method directly and its other methods (
24
24
 
25
25
  ```ruby
26
26
  class MyView < Phlex::HTML
27
- def template
27
+ def view_template
28
28
  h1 { 'Phlex / Sinatra integration' }
29
29
  p {
30
30
  a(href: url('/foo', false)) { 'link to foo' }
@@ -42,6 +42,42 @@ get '/foo' do
42
42
  end
43
43
  ```
44
44
 
45
+ ## Layout
46
+
47
+ If your entire view layer uses Phlex then layout will be a part of your component structure but maybe you've got an existing non-Phlex layout or you don't want to use Phlex for _everything_, in which case standard Sinatra layouts are supported.
48
+
49
+ Pass `layout: true` to wrap the Phlex output with Sinatra's default layout -- a file named "layout.erb" in the configured views directory (ERB is the default) -- or pass a symbol to specify the file:
50
+
51
+ ```ruby
52
+ get '/foo' do
53
+ # This Phlex view will be wrapped by `views/my_layout.erb`.
54
+ phlex MyView.new, layout: :my_layout
55
+ end
56
+ ```
57
+
58
+ Other [Sinatra templating languages](https://sinatrarb.com/intro.html#available-template-languages) can be specified via the `layout_engine` keyword:
59
+
60
+ ```ruby
61
+ get '/foo' do
62
+ # This Phlex view will be wrapped by `views/layout.haml`.
63
+ phlex MyView.new, layout: true, layout_engine: :haml
64
+ end
65
+ ```
66
+
67
+ ## Using Phlex in other templates
68
+
69
+ It's also possible to call `phlex` from within other views, for instance an ERB template:
70
+
71
+ ```erb
72
+ <%= phlex MyView.new %>
73
+ ```
74
+
75
+ A `layout` can also be passed:
76
+
77
+ ```erb
78
+ <%= phlex MyView.new, layout: :wrapper %>
79
+ ```
80
+
45
81
  ## Streaming
46
82
 
47
83
  Streaming a Phlex view can be enabled by passing `stream: true` which will cause Phlex to automatically write to the response after the closing `</head>` and buffer the remaining content:
@@ -58,7 +94,7 @@ You can also manually flush the contents of the buffer at any point using Phlex'
58
94
 
59
95
  ```ruby
60
96
  class Layout < Phlex::HTML
61
- def template(&)
97
+ def view_template(&block)
62
98
  doctype
63
99
  html {
64
100
  head {
@@ -70,14 +106,14 @@ class Layout < Phlex::HTML
70
106
  # Standard site header and navigation.
71
107
  render Header.new
72
108
 
73
- yield_content(&)
109
+ yield_content(&block)
74
110
  }
75
111
  }
76
112
  end
77
113
  end
78
114
 
79
115
  class MyView < Phlex::HTML
80
- def template
116
+ def view_template
81
117
  render Layout.new {
82
118
  # Knowing that this page can take a while to generate we can choose to
83
119
  # flush here so the browser can render the site header while downloading
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Sinatra
5
- VERSION = '0.3.0'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
  end
data/lib/phlex-sinatra.rb CHANGED
@@ -6,6 +6,7 @@ require_relative 'phlex/sinatra/version'
6
6
  module Phlex
7
7
  module Sinatra
8
8
  Error = Class.new(StandardError)
9
+ IncompatibleOptionError = Class.new(Error)
9
10
 
10
11
  class TypeError < Error
11
12
  MAX_SIZE = 32
@@ -37,18 +38,38 @@ end
37
38
 
38
39
  module Sinatra
39
40
  module Templates
40
- def phlex(obj, content_type: nil, stream: false)
41
+ def phlex(
42
+ obj,
43
+ content_type: nil,
44
+ layout: false,
45
+ layout_engine: :erb,
46
+ stream: false
47
+ )
41
48
  raise Phlex::Sinatra::TypeError.new(obj) unless obj.is_a?(Phlex::SGML)
42
49
 
43
- content_type ||= :svg if obj.is_a?(Phlex::SVG)
50
+ content_type ||= :svg if obj.is_a?(Phlex::SVG) && !layout
44
51
  self.content_type(content_type) if content_type
45
52
 
53
+ # Copy Sinatra's behaviour and interpret layout=true as meaning "use the
54
+ # default layout" - uses an internal Sinatra instance variable :s
55
+ layout = @default_layout if layout == true
56
+
46
57
  if stream
58
+ raise Phlex::Sinatra::IncompatibleOptionError.new(
59
+ 'streaming is not compatible with layout'
60
+ ) if layout
61
+
47
62
  self.stream do |out|
48
63
  obj.call(out, view_context: self)
49
64
  end
50
65
  else
51
- obj.call(view_context: self)
66
+ output = obj.call(view_context: self)
67
+
68
+ if layout
69
+ render(layout_engine, layout, { layout: false }) { output }
70
+ else
71
+ output
72
+ end
52
73
  end
53
74
  end
54
75
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlex-sinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Pickles
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-13 00:00:00.000000000 Z
11
+ date: 2024-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phlex
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  - !ruby/object:Gem::Version
65
65
  version: '0'
66
66
  requirements: []
67
- rubygems_version: 3.4.10
67
+ rubygems_version: 3.5.17
68
68
  signing_key:
69
69
  specification_version: 4
70
70
  summary: A Phlex adapter for Sinatra