phlex 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of phlex might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.rubocop.yml +21 -1
- data/Gemfile +26 -13
- data/README.md +1 -1
- data/SECURITY.md +1 -1
- data/bench.rb +7 -0
- data/config/sus.rb +15 -0
- data/docs/assets/application.css +6 -0
- data/docs/build.rb +2 -0
- data/docs/components/callout.rb +1 -1
- data/docs/components/code_block.rb +2 -2
- data/docs/components/code_span.rb +1 -1
- data/docs/components/example.rb +4 -4
- data/docs/components/heading.rb +2 -2
- data/docs/components/layout.rb +55 -32
- data/docs/components/markdown.rb +13 -28
- data/docs/components/nav/item.rb +1 -1
- data/docs/components/nav.rb +1 -1
- data/docs/components/tabs/tab.rb +1 -1
- data/docs/components/tabs.rb +1 -1
- data/docs/components/title.rb +2 -2
- data/docs/pages/application_page.rb +1 -1
- data/docs/pages/helpers.rb +5 -5
- data/docs/pages/library/collections.rb +4 -22
- data/docs/pages/rails/getting_started.rb +1 -1
- data/docs/pages/rails/helpers.rb +3 -1
- data/docs/pages/rails/layouts.rb +2 -2
- data/docs/pages/rails/rendering_views.rb +1 -1
- data/docs/pages/templates.rb +6 -6
- data/docs/pages/testing/capybara.rb +48 -0
- data/docs/pages/testing/getting_started.rb +44 -0
- data/docs/pages/testing/nokogiri.rb +83 -0
- data/docs/pages/testing/rails.rb +17 -0
- data/docs/pages/translations.rb +81 -0
- data/docs/pages/views.rb +56 -8
- data/fixtures/compiler_test_helpers.rb +19 -0
- data/fixtures/content.rb +60 -0
- data/fixtures/dummy/app/views/application_view.rb +8 -0
- data/fixtures/dummy/app/views/articles/form.rb +1 -1
- data/fixtures/dummy/app/views/card.rb +1 -1
- data/fixtures/dummy/app/views/comments/comment.rb +1 -1
- data/fixtures/dummy/app/views/comments/reaction.rb +1 -1
- data/fixtures/dummy/app/views/heading.rb +1 -1
- data/fixtures/layout.rb +5 -5
- data/fixtures/page.rb +18 -24
- data/fixtures/{test_helper.rb → rails_helper.rb} +3 -8
- data/fixtures/standard_element.rb +87 -0
- data/fixtures/view_helper.rb +1 -1
- data/fixtures/void_element.rb +31 -0
- data/lib/generators/phlex/collection/templates/collection.rb.erb +2 -1
- data/lib/generators/phlex/controller/USAGE +10 -0
- data/lib/generators/phlex/controller/controller_generator.rb +54 -0
- data/lib/generators/phlex/controller/templates/controller.rb.erb +10 -0
- data/lib/generators/phlex/controller/templates/view.rb.erb +14 -0
- data/lib/generators/phlex/layout/templates/layout.rb.erb +2 -1
- data/lib/generators/phlex/page/templates/page.rb.erb +3 -1
- data/lib/generators/phlex/table/templates/table.rb.erb +3 -1
- data/lib/generators/phlex/view/templates/view.rb.erb +7 -1
- data/lib/generators/phlex/view/view_generator.rb +9 -1
- data/lib/install/phlex.rb +10 -1
- data/lib/phlex/block.rb +2 -4
- data/lib/phlex/buffered.rb +6 -8
- data/lib/phlex/callable.rb +9 -0
- data/lib/phlex/collection.rb +2 -27
- data/lib/phlex/compiler/elements.rb +49 -0
- data/lib/phlex/compiler/generators/content.rb +103 -0
- data/lib/phlex/compiler/generators/element.rb +61 -0
- data/lib/phlex/compiler/nodes/base.rb +19 -0
- data/lib/phlex/compiler/nodes/call.rb +9 -0
- data/lib/phlex/compiler/nodes/command.rb +13 -0
- data/lib/phlex/compiler/nodes/fcall.rb +18 -0
- data/lib/phlex/compiler/nodes/method_add_block.rb +33 -0
- data/lib/phlex/compiler/nodes/vcall.rb +9 -0
- data/lib/phlex/compiler/optimizer.rb +66 -0
- data/lib/phlex/compiler/visitors/base.rb +15 -0
- data/lib/phlex/compiler/visitors/file.rb +23 -11
- data/lib/phlex/compiler/visitors/stable_scope.rb +28 -0
- data/lib/phlex/compiler/visitors/statements.rb +36 -0
- data/lib/phlex/compiler/visitors/view.rb +19 -0
- data/lib/phlex/compiler/visitors/view_method.rb +59 -0
- data/lib/phlex/compiler.rb +23 -3
- data/lib/phlex/elements.rb +57 -0
- data/lib/phlex/helpers.rb +59 -0
- data/lib/phlex/html/callbacks.rb +11 -0
- data/lib/phlex/html.rb +208 -47
- data/lib/phlex/markdown.rb +76 -0
- data/lib/phlex/rails/form.rb +67 -0
- data/lib/phlex/rails/helpers.rb +39 -2
- data/lib/phlex/rails.rb +10 -0
- data/lib/phlex/renderable.rb +9 -3
- data/lib/phlex/testing/capybara.rb +25 -0
- data/lib/phlex/testing/nokogiri.rb +24 -0
- data/lib/phlex/testing/rails.rb +19 -0
- data/lib/phlex/testing/view_helper.rb +15 -0
- data/lib/phlex/translation.rb +23 -0
- data/lib/phlex/turbo/frame.rb +21 -0
- data/lib/phlex/turbo/stream.rb +18 -0
- data/lib/phlex/version.rb +1 -1
- data/lib/phlex.rb +22 -24
- metadata +62 -14
- data/.rspec +0 -1
- data/fixtures/compilation/vcall.rb +0 -38
- data/lib/phlex/compiler/generators/standard_element.rb +0 -30
- data/lib/phlex/compiler/generators/void_element.rb +0 -29
- data/lib/phlex/compiler/optimizers/base_optimizer.rb +0 -34
- data/lib/phlex/compiler/optimizers/vcall.rb +0 -29
- data/lib/phlex/compiler/visitors/base_visitor.rb +0 -19
- data/lib/phlex/compiler/visitors/component.rb +0 -28
- data/lib/phlex/compiler/visitors/component_method.rb +0 -28
- data/lib/phlex/view.rb +0 -229
data/docs/pages/templates.rb
CHANGED
@@ -16,7 +16,7 @@ module Pages
|
|
16
16
|
|
17
17
|
render Example.new do |e|
|
18
18
|
e.tab "hello.rb", <<~RUBY
|
19
|
-
class Hello < Phlex::
|
19
|
+
class Hello < Phlex::HTML
|
20
20
|
def template
|
21
21
|
h1(class: "text-xl font-bold") { "👋 Hello World!" }
|
22
22
|
end
|
@@ -34,7 +34,7 @@ module Pages
|
|
34
34
|
|
35
35
|
render Example.new do |e|
|
36
36
|
e.tab "hello.rb", <<~RUBY
|
37
|
-
class Hello < Phlex::
|
37
|
+
class Hello < Phlex::HTML
|
38
38
|
def template
|
39
39
|
div(data: { controller: "hello" }) do
|
40
40
|
# ...
|
@@ -54,7 +54,7 @@ module Pages
|
|
54
54
|
|
55
55
|
render Example.new do |e|
|
56
56
|
e.tab "channel_controls.rb", <<~RUBY
|
57
|
-
class ChannelControls < Phlex::
|
57
|
+
class ChannelControls < Phlex::HTML
|
58
58
|
def template
|
59
59
|
input(
|
60
60
|
value: "1",
|
@@ -84,7 +84,7 @@ module Pages
|
|
84
84
|
|
85
85
|
render Example.new do |e|
|
86
86
|
e.tab "example.rb", <<~RUBY
|
87
|
-
class Example < Phlex::
|
87
|
+
class Example < Phlex::HTML
|
88
88
|
def template
|
89
89
|
template_tag do
|
90
90
|
img src: "hidden.jpg", alt: "A hidden image."
|
@@ -104,7 +104,7 @@ module Pages
|
|
104
104
|
|
105
105
|
render Example.new do |e|
|
106
106
|
e.tab "heading.rb", <<~RUBY
|
107
|
-
class Heading < Phlex::
|
107
|
+
class Heading < Phlex::HTML
|
108
108
|
def template
|
109
109
|
h1 do
|
110
110
|
strong { "Hello " }
|
@@ -125,7 +125,7 @@ module Pages
|
|
125
125
|
|
126
126
|
render Example.new do |e|
|
127
127
|
e.tab "links.rb", <<~RUBY
|
128
|
-
class Links < Phlex::
|
128
|
+
class Links < Phlex::HTML
|
129
129
|
def template
|
130
130
|
a(href: "/") { "Home" }
|
131
131
|
whitespace
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "minitest"
|
4
|
+
|
5
|
+
module Pages
|
6
|
+
module Testing
|
7
|
+
class Capybara < ApplicationPage
|
8
|
+
def template
|
9
|
+
render Layout.new(title: "Testing with Capybara") do
|
10
|
+
render Markdown.new(<<~MD)
|
11
|
+
# Testing with Capybara
|
12
|
+
|
13
|
+
Require `phlex/testing/capybara` and include `Phlex::Testing::Capybara::ViewHelper` to use [Capybara](http://teamcapybara.github.io/capybara/) matchers.
|
14
|
+
|
15
|
+
The `render` method will return a `Capybara::Node::Simple` and set the `page` attribute to the result.
|
16
|
+
MD
|
17
|
+
|
18
|
+
render Example.new do |e|
|
19
|
+
e.tab "test.rb", <<~RUBY
|
20
|
+
require "phlex/testing/capybara"
|
21
|
+
|
22
|
+
class TestExample < Minitest::Test
|
23
|
+
include Phlex::Testing::Capybara::ViewHelper
|
24
|
+
|
25
|
+
def test_example
|
26
|
+
render Example.new("Joel")
|
27
|
+
assert_select "h1", text: "Hello Joel"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
RUBY
|
31
|
+
|
32
|
+
e.tab "hello.rb", <<~RUBY
|
33
|
+
class Hello < Phlex::HTML
|
34
|
+
def initialize(name)
|
35
|
+
@name = name
|
36
|
+
end
|
37
|
+
|
38
|
+
def template
|
39
|
+
h1 { "Hello \#{@name}" }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
RUBY
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pages
|
4
|
+
module Testing
|
5
|
+
class GettingStarted < ApplicationPage
|
6
|
+
def template
|
7
|
+
render Layout.new(title: "Getting Started Testing Phlex Views") do
|
8
|
+
render Markdown.new(<<~MD)
|
9
|
+
# Getting Started
|
10
|
+
|
11
|
+
The `Phlex::Testing::ViewHelper` module defines `render` allowing you to render Phlex views directly in your tests and make assertions against the output.
|
12
|
+
MD
|
13
|
+
|
14
|
+
render Example.new do |e|
|
15
|
+
e.tab "test/test_hello.rb", <<~RUBY
|
16
|
+
require "phlex/testing/view_helper"
|
17
|
+
|
18
|
+
class TestHello < Minitest::Test
|
19
|
+
include Phlex::Testing::ViewHelper
|
20
|
+
|
21
|
+
def test_hello_output_includes_name
|
22
|
+
output = render Hello.new("Joel")
|
23
|
+
assert_equal "<h1>Hello Joel</h1>", output
|
24
|
+
end
|
25
|
+
end
|
26
|
+
RUBY
|
27
|
+
|
28
|
+
e.tab "hello.rb", <<~RUBY
|
29
|
+
class Hello < Phlex::HTML
|
30
|
+
def initialize(name)
|
31
|
+
@name = name
|
32
|
+
end
|
33
|
+
|
34
|
+
def template
|
35
|
+
h1 { "Hello \#{@name}" }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
RUBY
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pages
|
4
|
+
module Testing
|
5
|
+
class Nokogiri < ApplicationPage
|
6
|
+
def template
|
7
|
+
render Layout.new(title: "Testing with Nokogiri") do
|
8
|
+
render Markdown.new(<<~MD)
|
9
|
+
# Testing with Nokogiri
|
10
|
+
|
11
|
+
Phlex includes test helpers for working with rendered views as [Nokogiri](https://nokogiri.org) documents and fragments.
|
12
|
+
|
13
|
+
Nokogiri is not a dependency of Phlex, so you’ll need to install that separately to use this helper.
|
14
|
+
|
15
|
+
## Documents
|
16
|
+
|
17
|
+
If your view represents a whole HTML document, you can require `phlex/testing/nokogiri` and include the `Phlex::Testing::Nokogiri::DocumentHelper` module to render your view as `Nokogiri::Document` using the `render` method.
|
18
|
+
MD
|
19
|
+
|
20
|
+
render Example.new do |e|
|
21
|
+
e.tab "test.rb", <<~RUBY
|
22
|
+
require "phlex/testing/nokogiri"
|
23
|
+
|
24
|
+
class TestExample < Minitest::Test
|
25
|
+
include Phlex::Testing::Nokogiri::DocumentHelper
|
26
|
+
|
27
|
+
def test_example
|
28
|
+
output = render Example.new
|
29
|
+
assert_equal "Hello Joel", output.css("h1").text
|
30
|
+
end
|
31
|
+
end
|
32
|
+
RUBY
|
33
|
+
|
34
|
+
e.tab "hello.rb", <<~RUBY
|
35
|
+
class Hello < Phlex::HTML
|
36
|
+
def initialize(name)
|
37
|
+
@name = name
|
38
|
+
end
|
39
|
+
|
40
|
+
def template
|
41
|
+
h1 { "Hello \#{@name}" }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
RUBY
|
45
|
+
end
|
46
|
+
|
47
|
+
render Markdown.new(<<~MD)
|
48
|
+
## Fragments
|
49
|
+
|
50
|
+
If your view represents a fragment (partial), you can require `phlex/testing/nokogiri` and include the `Phlex::Testing::Nokogiri::FragmentHelper` module to render your view as `Nokogiri::Fragment` with the `render` method.
|
51
|
+
MD
|
52
|
+
|
53
|
+
render Example.new do |e|
|
54
|
+
e.tab "test.rb", <<~RUBY
|
55
|
+
require "phlex/testing/nokogiri"
|
56
|
+
|
57
|
+
class TestExample < Minitest::Test
|
58
|
+
include Phlex::Testing::Nokogiri::FragmentHelper
|
59
|
+
|
60
|
+
def test_example
|
61
|
+
output = render Example.new("Joel")
|
62
|
+
assert_equal "Hello Joel", output.css("h1").text
|
63
|
+
end
|
64
|
+
end
|
65
|
+
RUBY
|
66
|
+
|
67
|
+
e.tab "hello.rb", <<~RUBY
|
68
|
+
class Hello < Phlex::HTML
|
69
|
+
def initialize(name)
|
70
|
+
@name = name
|
71
|
+
end
|
72
|
+
|
73
|
+
def template
|
74
|
+
h1 { "Hello \#{@name}" }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
RUBY
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pages
|
4
|
+
module Testing
|
5
|
+
class Rails < ApplicationPage
|
6
|
+
def template
|
7
|
+
render Layout.new(title: "Testing Phlex Views in Rails") do
|
8
|
+
render Markdown.new(<<~MD)
|
9
|
+
# Testing Phlex views in Rails
|
10
|
+
|
11
|
+
When you include `Phlex::Testing::Rails::ViewHelper`, views rendered in the test will have a view context, so they can use Rails helpers.
|
12
|
+
MD
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Pages
|
4
|
+
class Translations < ApplicationPage
|
5
|
+
def initialize
|
6
|
+
I18n.backend.store_translations(
|
7
|
+
"pt-BR", {
|
8
|
+
hello: "Olá",
|
9
|
+
views: { feedback: { welcome_message: { hello: "Olá" } } }
|
10
|
+
}
|
11
|
+
)
|
12
|
+
I18n.locale = "pt-BR"
|
13
|
+
end
|
14
|
+
|
15
|
+
def template
|
16
|
+
render Layout.new(title: "Translations") do
|
17
|
+
render Markdown.new(<<~MD)
|
18
|
+
# Translations
|
19
|
+
|
20
|
+
Phlex has built-in support for translations with the **[I18n Gem](https://github.com/ruby-i18n/i18n)**.
|
21
|
+
|
22
|
+
Just include `Phlex::Translation` in your view and use the `translate` method to access a translation.
|
23
|
+
MD
|
24
|
+
|
25
|
+
render Example.new do |e|
|
26
|
+
e.tab "welcome_message.rb", <<~RUBY
|
27
|
+
class WelcomeMessage < Phlex::HTML
|
28
|
+
include Phlex::Translation
|
29
|
+
|
30
|
+
def template
|
31
|
+
h1 { translate("hello") }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
RUBY
|
35
|
+
|
36
|
+
e.tab "pt-PR.yml", <<~YAML, syntax: :yaml
|
37
|
+
pt-BR:
|
38
|
+
hello: "Olá"
|
39
|
+
YAML
|
40
|
+
|
41
|
+
e.execute "WelcomeMessage.new.call"
|
42
|
+
end
|
43
|
+
|
44
|
+
render Markdown.new(<<~MD)
|
45
|
+
## Implicit scoopes
|
46
|
+
|
47
|
+
Start your translate key with a `.` to use the name of the view as an implicit scope.
|
48
|
+
MD
|
49
|
+
|
50
|
+
render Example.new do |e|
|
51
|
+
e.tab "welcome_message.rb", <<~RUBY
|
52
|
+
module Views
|
53
|
+
module Feedback
|
54
|
+
class WelcomeMessage < Phlex::HTML
|
55
|
+
include Phlex::Translation
|
56
|
+
|
57
|
+
def template
|
58
|
+
h1 { translate(".hello") }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
RUBY
|
64
|
+
|
65
|
+
e.tab "pt-BR.yml", <<~YAML, syntax: :yaml
|
66
|
+
pt-BR:
|
67
|
+
views:
|
68
|
+
feedback:
|
69
|
+
welcome_message:
|
70
|
+
hello: Olá
|
71
|
+
YAML
|
72
|
+
|
73
|
+
e.execute <<~RUBY
|
74
|
+
Views::Feedback::WelcomeMessage.translation_path = 'views.feedback.welcome_message'
|
75
|
+
Views::Feedback::WelcomeMessage.new.call
|
76
|
+
RUBY
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/docs/pages/views.rb
CHANGED
@@ -9,12 +9,12 @@ module Pages
|
|
9
9
|
|
10
10
|
Phlex Views are Ruby objects that represent your app's user interface — from pages and layouts and nav-bars, to headings and buttons and links.
|
11
11
|
|
12
|
-
You can create a view class by subclassing `Phlex::
|
12
|
+
You can create a view class by subclassing `Phlex::HTML` and defining a `template` instance method.
|
13
13
|
MD
|
14
14
|
|
15
15
|
render Example.new do |e|
|
16
16
|
e.tab "hello.rb", <<~RUBY
|
17
|
-
class Hello < Phlex::
|
17
|
+
class Hello < Phlex::HTML
|
18
18
|
def template
|
19
19
|
h1 { "👋 Hello World!" }
|
20
20
|
end
|
@@ -36,7 +36,7 @@ module Pages
|
|
36
36
|
|
37
37
|
render Example.new do |e|
|
38
38
|
e.tab "hello.rb", <<~RUBY
|
39
|
-
class Hello < Phlex::
|
39
|
+
class Hello < Phlex::HTML
|
40
40
|
def initialize(name:)
|
41
41
|
@name = name
|
42
42
|
end
|
@@ -58,7 +58,7 @@ module Pages
|
|
58
58
|
|
59
59
|
render Example.new do |e|
|
60
60
|
e.tab "example.rb", <<~RUBY
|
61
|
-
class Example < Phlex::
|
61
|
+
class Example < Phlex::HTML
|
62
62
|
def template
|
63
63
|
render Hello.new(name: "Joel")
|
64
64
|
render Hello.new(name: "Alexandre")
|
@@ -67,7 +67,7 @@ module Pages
|
|
67
67
|
RUBY
|
68
68
|
|
69
69
|
e.tab "hello.rb", <<~RUBY
|
70
|
-
class Hello < Phlex::
|
70
|
+
class Hello < Phlex::HTML
|
71
71
|
def initialize(name:)
|
72
72
|
@name = name
|
73
73
|
end
|
@@ -89,7 +89,7 @@ module Pages
|
|
89
89
|
|
90
90
|
render Example.new do |e|
|
91
91
|
e.tab "card.rb", <<~RUBY
|
92
|
-
class Card < Phlex::
|
92
|
+
class Card < Phlex::HTML
|
93
93
|
def template(&content)
|
94
94
|
article(class: "drop-shadow") do
|
95
95
|
yield_content(&content)
|
@@ -99,7 +99,7 @@ module Pages
|
|
99
99
|
RUBY
|
100
100
|
|
101
101
|
e.tab "example.rb", <<~RUBY
|
102
|
-
class Example < Phlex::
|
102
|
+
class Example < Phlex::HTML
|
103
103
|
def template
|
104
104
|
render Card.new do
|
105
105
|
h1 { "👋 Hello!" }
|
@@ -123,13 +123,61 @@ module Pages
|
|
123
123
|
Since the block of content was the only thing we need in the `<article>` element, we could have just passed the content block to the element instead.
|
124
124
|
|
125
125
|
```ruby
|
126
|
-
class Card < Phlex::
|
126
|
+
class Card < Phlex::HTML
|
127
127
|
def template(&content)
|
128
128
|
article(class: "drop-shadow", &content)
|
129
129
|
end
|
130
130
|
end
|
131
131
|
```
|
132
132
|
MD
|
133
|
+
|
134
|
+
render Markdown.new(<<~MD)
|
135
|
+
## Registering custom elements
|
136
|
+
|
137
|
+
You can register custom elements with the `register_element` macro. The custom element will only be available in the view where it is registered and subclasses of that view.
|
138
|
+
MD
|
139
|
+
|
140
|
+
render Example.new do |e|
|
141
|
+
e.tab "example.rb", <<~RUBY
|
142
|
+
class Example < Phlex::HTML
|
143
|
+
register_element :trix_editor
|
144
|
+
|
145
|
+
def template
|
146
|
+
trix_editor input: "content", autofocus: true
|
147
|
+
end
|
148
|
+
end
|
149
|
+
RUBY
|
150
|
+
|
151
|
+
e.execute "Example.new.call"
|
152
|
+
end
|
153
|
+
|
154
|
+
render Markdown.new(<<~MD)
|
155
|
+
## Callbacks
|
156
|
+
|
157
|
+
Prepend the `Phlex::HTML::Callbacks` module, and if you define `#before_rendering_template` and/or `#after_rendering_template` method in your view, they will be called immediately before and after your template is rendered.
|
158
|
+
MD
|
159
|
+
|
160
|
+
render Example.new do |e|
|
161
|
+
e.tab "example.rb", <<~RUBY
|
162
|
+
class Example < Phlex::HTML
|
163
|
+
prepend Phlex::HTML::Callbacks
|
164
|
+
|
165
|
+
def before_rendering_template
|
166
|
+
h1 { "Hello" }
|
167
|
+
end
|
168
|
+
|
169
|
+
def template
|
170
|
+
h2 { "World" }
|
171
|
+
end
|
172
|
+
|
173
|
+
def after_rendering_template
|
174
|
+
h3 { "Bye" }
|
175
|
+
end
|
176
|
+
end
|
177
|
+
RUBY
|
178
|
+
|
179
|
+
e.execute "Example.new.call"
|
180
|
+
end
|
133
181
|
end
|
134
182
|
end
|
135
183
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CompilerTestHelpers
|
4
|
+
# @return Array
|
5
|
+
def compile(view)
|
6
|
+
@compiler = Phlex::Compiler.new(view)
|
7
|
+
output = []
|
8
|
+
|
9
|
+
mock(@compiler) do |m|
|
10
|
+
m.before(:redefine) { output << _1 }
|
11
|
+
end
|
12
|
+
|
13
|
+
@compiler.call
|
14
|
+
|
15
|
+
output.map! do |method|
|
16
|
+
Phlex::Compiler::Formatter.format("", SyntaxTree.parse(method))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/fixtures/content.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Fixtures
|
4
|
+
module Content
|
5
|
+
class BareString < Phlex::HTML
|
6
|
+
def template
|
7
|
+
h1 { "Hello" }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Symbol < Phlex::HTML
|
12
|
+
def template
|
13
|
+
h1 { :hello }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Float < Phlex::HTML
|
18
|
+
def template
|
19
|
+
h1 { 1.2 }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Integer < Phlex::HTML
|
24
|
+
def template
|
25
|
+
h1 { 1 }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Variable < Phlex::HTML
|
30
|
+
def template
|
31
|
+
greeting = "Hello"
|
32
|
+
h1 { greeting }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class InstanceVariable < Phlex::HTML
|
37
|
+
def template
|
38
|
+
h1 { @hello }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class NestedTags < Phlex::HTML
|
43
|
+
def template
|
44
|
+
article {
|
45
|
+
h1 { "Inside" }
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class NonMutatingNestedContent < Phlex::HTML
|
51
|
+
def template
|
52
|
+
div { say_hello }
|
53
|
+
end
|
54
|
+
|
55
|
+
def say_hello
|
56
|
+
"Hello"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/fixtures/layout.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Example
|
4
|
-
class LayoutComponent < Phlex::
|
4
|
+
class LayoutComponent < Phlex::HTML
|
5
5
|
def initialize(title: "Example")
|
6
6
|
@title = title
|
7
7
|
end
|
@@ -9,7 +9,7 @@ module Example
|
|
9
9
|
def template(&block)
|
10
10
|
html do
|
11
11
|
head do
|
12
|
-
title @title
|
12
|
+
title { @title }
|
13
13
|
meta name: "viewport", content: "width=device-width,initial-scale=1"
|
14
14
|
link href: "/assets/tailwind.css", rel: "stylesheet"
|
15
15
|
end
|
@@ -17,9 +17,9 @@ module Example
|
|
17
17
|
body class: "bg-zinc-100" do
|
18
18
|
nav class: "p-5", id: "main_nav" do
|
19
19
|
ul do
|
20
|
-
li(class: "p-5") { a "
|
21
|
-
li(class: "p-5") { a
|
22
|
-
li(class: "p-5") { a
|
20
|
+
li(class: "p-5") { a(href: "/") { "Home" } }
|
21
|
+
li(class: "p-5") { a(href: "/about") { "About" } }
|
22
|
+
li(class: "p-5") { a(href: "/contact") { "Contact" } }
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
data/fixtures/page.rb
CHANGED
@@ -1,37 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Example
|
4
|
-
class Page < Phlex::
|
4
|
+
class Page < Phlex::HTML
|
5
5
|
def template
|
6
6
|
render LayoutComponent.new do
|
7
|
-
h1 "Hi"
|
7
|
+
h1 { "Hi" }
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
table id: "test", class: "a b c d e f g" do
|
10
|
+
tr do
|
11
|
+
td id: "test", class: "a b c d e f g" do
|
12
|
+
span { "Hi" }
|
13
13
|
end
|
14
|
-
end
|
15
|
-
end
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
td id: "test", class: "a b c d e f g" do
|
16
|
+
span { "Hi" }
|
17
|
+
end
|
18
|
+
|
19
|
+
td id: "test", class: "a b c d e f g" do
|
20
|
+
span { "Hi" }
|
21
|
+
end
|
22
|
+
|
23
|
+
td id: "test", class: "a b c d e f g" do
|
24
|
+
span { "Hi" }
|
23
25
|
end
|
24
|
-
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
tr class: "a b c d e f g", id: "something" do
|
29
|
-
10.times do
|
30
|
-
td class: "f g h i j k l" do
|
31
|
-
span "Test"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
27
|
+
td id: "test", class: "a b c d e f g" do
|
28
|
+
span { "Hi" }
|
35
29
|
end
|
36
30
|
end
|
37
31
|
end
|