phlex 0.5.3 β 1.0.0.rc2
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/Gemfile +2 -20
- data/config/sus.rb +0 -5
- data/lib/phlex/buffered.rb +2 -0
- data/lib/phlex/collection.rb +2 -0
- data/lib/phlex/elements.rb +1 -5
- data/lib/phlex/experimental.rb +10 -0
- data/lib/phlex/helpers.rb +23 -10
- data/lib/phlex/html.rb +72 -86
- data/lib/phlex/table.rb +1 -0
- data/lib/phlex/turbo/frame.rb +2 -0
- data/lib/phlex/turbo/stream.rb +2 -0
- data/lib/phlex/version.rb +1 -1
- data/lib/phlex.rb +2 -19
- metadata +6 -166
- data/Procfile.dev +0 -3
- data/Rakefile +0 -8
- data/config.ru +0 -9
- data/docs/assets/application.css +0 -32
- data/docs/assets/logo.png +0 -0
- data/docs/build.rb +0 -30
- data/docs/components/callout.rb +0 -9
- data/docs/components/code_block.rb +0 -26
- data/docs/components/code_span.rb +0 -9
- data/docs/components/example.rb +0 -32
- data/docs/components/heading.rb +0 -9
- data/docs/components/layout.rb +0 -91
- data/docs/components/markdown.rb +0 -25
- data/docs/components/nav/item.rb +0 -33
- data/docs/components/nav.rb +0 -6
- data/docs/components/tabs/tab.rb +0 -28
- data/docs/components/tabs.rb +0 -30
- data/docs/components/title.rb +0 -9
- data/docs/page_builder.rb +0 -39
- data/docs/pages/application_page.rb +0 -7
- data/docs/pages/helpers.rb +0 -97
- data/docs/pages/index.rb +0 -27
- data/docs/pages/library/collections.rb +0 -83
- data/docs/pages/rails/getting_started.rb +0 -57
- data/docs/pages/rails/helpers.rb +0 -55
- data/docs/pages/rails/layouts.rb +0 -61
- data/docs/pages/rails/migrating.rb +0 -37
- data/docs/pages/rails/rendering_views.rb +0 -35
- data/docs/pages/rails_integration.rb +0 -58
- data/docs/pages/templates.rb +0 -144
- data/docs/pages/testing/capybara.rb +0 -48
- data/docs/pages/testing/getting_started.rb +0 -44
- data/docs/pages/testing/nokogiri.rb +0 -83
- data/docs/pages/testing/rails.rb +0 -17
- data/docs/pages/translations.rb +0 -81
- data/docs/pages/views.rb +0 -184
- data/fixtures/compiler_test_helpers.rb +0 -19
- data/fixtures/content.rb +0 -60
- data/fixtures/dummy/app/assets/config/manifest.js +0 -0
- data/fixtures/dummy/app/components/comment_component.html.erb +0 -14
- data/fixtures/dummy/app/components/comment_component.rb +0 -8
- data/fixtures/dummy/app/components/reaction_component.html.erb +0 -3
- data/fixtures/dummy/app/components/reaction_component.rb +0 -7
- data/fixtures/dummy/app/controllers/articles_controller.rb +0 -4
- data/fixtures/dummy/app/controllers/comments_controller.rb +0 -4
- data/fixtures/dummy/app/views/application_view.rb +0 -8
- data/fixtures/dummy/app/views/articles/form.rb +0 -15
- data/fixtures/dummy/app/views/articles/index.html.erb +0 -14
- data/fixtures/dummy/app/views/articles/new.html.erb +0 -1
- data/fixtures/dummy/app/views/card.rb +0 -15
- data/fixtures/dummy/app/views/comments/comment.rb +0 -25
- data/fixtures/dummy/app/views/comments/index.html.erb +0 -3
- data/fixtures/dummy/app/views/comments/reaction.rb +0 -17
- data/fixtures/dummy/app/views/comments/show.html.erb +0 -3
- data/fixtures/dummy/app/views/heading.rb +0 -9
- data/fixtures/dummy/config/database.yml +0 -3
- data/fixtures/dummy/config/routes.rb +0 -5
- data/fixtures/dummy/config/storage.yml +0 -3
- data/fixtures/dummy/db/schema.rb +0 -6
- data/fixtures/dummy/log/.gitignore +0 -1
- data/fixtures/dummy/public/favicon.ico +0 -0
- data/fixtures/rails_helper.rb +0 -11
- data/fixtures/standard_element.rb +0 -87
- data/fixtures/void_element.rb +0 -31
- data/lib/generators/phlex/collection/USAGE +0 -8
- data/lib/generators/phlex/collection/collection_generator.rb +0 -13
- data/lib/generators/phlex/collection/templates/collection.rb.erb +0 -16
- data/lib/generators/phlex/controller/USAGE +0 -10
- data/lib/generators/phlex/controller/controller_generator.rb +0 -54
- data/lib/generators/phlex/controller/templates/controller.rb.erb +0 -10
- data/lib/generators/phlex/controller/templates/view.rb.erb +0 -14
- data/lib/generators/phlex/layout/USAGE +0 -8
- data/lib/generators/phlex/layout/layout_generator.rb +0 -13
- data/lib/generators/phlex/layout/templates/layout.rb.erb +0 -31
- data/lib/generators/phlex/page/USAGE +0 -8
- data/lib/generators/phlex/page/page_generator.rb +0 -13
- data/lib/generators/phlex/page/templates/page.rb.erb +0 -13
- data/lib/generators/phlex/table/USAGE +0 -8
- data/lib/generators/phlex/table/table_generator.rb +0 -14
- data/lib/generators/phlex/table/templates/table.rb.erb +0 -11
- data/lib/generators/phlex/view/USAGE +0 -8
- data/lib/generators/phlex/view/templates/view.rb.erb +0 -14
- data/lib/generators/phlex/view/view_generator.rb +0 -21
- data/lib/install/phlex.rb +0 -39
- data/lib/phlex/block.rb +0 -16
- data/lib/phlex/compiler/elements.rb +0 -49
- data/lib/phlex/compiler/formatter.rb +0 -91
- data/lib/phlex/compiler/generators/content.rb +0 -103
- data/lib/phlex/compiler/generators/element.rb +0 -61
- data/lib/phlex/compiler/nodes/base.rb +0 -19
- data/lib/phlex/compiler/nodes/call.rb +0 -9
- data/lib/phlex/compiler/nodes/command.rb +0 -13
- data/lib/phlex/compiler/nodes/fcall.rb +0 -18
- data/lib/phlex/compiler/nodes/method_add_block.rb +0 -33
- data/lib/phlex/compiler/nodes/vcall.rb +0 -9
- data/lib/phlex/compiler/optimizer.rb +0 -66
- data/lib/phlex/compiler/visitors/base.rb +0 -15
- data/lib/phlex/compiler/visitors/file.rb +0 -29
- data/lib/phlex/compiler/visitors/stable_scope.rb +0 -28
- data/lib/phlex/compiler/visitors/statements.rb +0 -36
- data/lib/phlex/compiler/visitors/view.rb +0 -19
- data/lib/phlex/compiler/visitors/view_method.rb +0 -59
- data/lib/phlex/compiler.rb +0 -70
- data/lib/phlex/html/callbacks.rb +0 -11
- data/lib/phlex/markdown.rb +0 -76
- data/lib/phlex/rails/engine.rb +0 -10
- data/lib/phlex/rails/form.rb +0 -67
- data/lib/phlex/rails/helpers.rb +0 -118
- data/lib/phlex/rails/layout.rb +0 -15
- data/lib/phlex/rails.rb +0 -11
- data/lib/phlex/renderable.rb +0 -47
- data/lib/phlex/testing/capybara.rb +0 -25
- data/lib/phlex/testing/nokogiri.rb +0 -24
- data/lib/phlex/testing/rails.rb +0 -19
- data/lib/phlex/translation.rb +0 -23
- data/lib/tasks/phlex_tasks.rake +0 -11
- data/package-lock.json +0 -1195
- data/package.json +0 -5
- data/tailwind.config.js +0 -7
data/docs/pages/helpers.rb
DELETED
@@ -1,97 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Pages
|
4
|
-
class Helpers < ApplicationPage
|
5
|
-
def template
|
6
|
-
render Layout.new(title: "Helpers") do
|
7
|
-
render Markdown.new(<<~MD)
|
8
|
-
# Helpers
|
9
|
-
|
10
|
-
## Conditional tokens and classes
|
11
|
-
|
12
|
-
The `tokens` method helps you define conditional HTML attribute tokens (such as CSS classes). It accepts a splat of tokens that should always be output as well as optional keyword arguments for conditional tokens.
|
13
|
-
|
14
|
-
The keyword arguments allow you to specify under which conditions certain tokens are applicable. The keys are the conditions and the values are the tokens. Conditions can be Procs which are evaluated, or Symbols that map to an instance method. The `:active?` Symbol, for example, maps to the `active?` instance method.
|
15
|
-
|
16
|
-
Here we have a `Link` view that produces an `<a>` tag with the CSS class `nav-item`. If the link is _active_, we also apply the CSS class `nav-item-active`.
|
17
|
-
MD
|
18
|
-
|
19
|
-
render Example.new do |e|
|
20
|
-
e.tab "link.rb", <<~RUBY
|
21
|
-
class Link < Phlex::HTML
|
22
|
-
def initialize(text, to:, active:)
|
23
|
-
@text = text
|
24
|
-
@to = to
|
25
|
-
@active = active
|
26
|
-
end
|
27
|
-
|
28
|
-
def template
|
29
|
-
a(href: @to, class: tokens("nav-item",
|
30
|
-
active?: "nav-item-active")) { @text }
|
31
|
-
end
|
32
|
-
|
33
|
-
private
|
34
|
-
|
35
|
-
def active? = @active
|
36
|
-
end
|
37
|
-
RUBY
|
38
|
-
|
39
|
-
e.tab "example.rb", <<~RUBY
|
40
|
-
class Example < Phlex::HTML
|
41
|
-
def template
|
42
|
-
nav do
|
43
|
-
ul do
|
44
|
-
li { render Link.new("Home", to: "/", active: true) }
|
45
|
-
li { render Link.new("About", to: "/about", active: false) }
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
RUBY
|
51
|
-
|
52
|
-
e.execute "Example.new.call"
|
53
|
-
end
|
54
|
-
|
55
|
-
render Markdown.new(<<~MD)
|
56
|
-
You can also use the `classes` helper method to create a token list of classes. Since this method returns a hash, e.g. `{ class: "your CSS classes here" }`, you can destructure it into a `class:` keyword argument using the `**` prefix operator.
|
57
|
-
MD
|
58
|
-
|
59
|
-
render Example.new do |e|
|
60
|
-
e.tab "link.rb", <<~RUBY
|
61
|
-
class Link < Phlex::HTML
|
62
|
-
def initialize(text, to:, active:)
|
63
|
-
@text = text
|
64
|
-
@to = to
|
65
|
-
@active = active
|
66
|
-
end
|
67
|
-
|
68
|
-
def template
|
69
|
-
a(href: @to, **classes("nav-item",
|
70
|
-
active?: "nav-item-active")) { @text }
|
71
|
-
end
|
72
|
-
|
73
|
-
private
|
74
|
-
|
75
|
-
def active? = @active
|
76
|
-
end
|
77
|
-
RUBY
|
78
|
-
|
79
|
-
e.tab "example.rb", <<~RUBY
|
80
|
-
class Example < Phlex::HTML
|
81
|
-
def template
|
82
|
-
nav do
|
83
|
-
ul do
|
84
|
-
li { render Link.new("Home", to: "/", active: true) }
|
85
|
-
li { render Link.new("About", to: "/about", active: false) }
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
RUBY
|
91
|
-
|
92
|
-
e.execute "Example.new.call"
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
end
|
data/docs/pages/index.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Pages
|
4
|
-
class Index < ApplicationPage
|
5
|
-
def template
|
6
|
-
render Layout.new(title: "Introduction to Phlex, a fast, object-oriented view framework for Ruby") do
|
7
|
-
render Markdown.new(<<~MD)
|
8
|
-
# Introduction
|
9
|
-
|
10
|
-
Phlex is a framework for building fast, reusable, testable views in pure Ruby.
|
11
|
-
|
12
|
-
## Better developer experience π
|
13
|
-
|
14
|
-
Phlex views are plain old Ruby objects. View classes are just Ruby classes, templates are just methods, and HTML tags are just method calls. If you know how to define a method that calls another method, you pretty much already know how to use Phlex.
|
15
|
-
|
16
|
-
## Better safety π₯½
|
17
|
-
|
18
|
-
Phlex view templates render in an isolated execution context where only the instance variables and methods for the specific view are exposed.
|
19
|
-
|
20
|
-
## Better performance π₯
|
21
|
-
|
22
|
-
Rendering a Phlex view is ~4.35Γ faster than an ActionView partial and ~2Γ faster than ViewComponent component.
|
23
|
-
MD
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,83 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Pages
|
4
|
-
module Library
|
5
|
-
class Collections < ApplicationPage
|
6
|
-
def template
|
7
|
-
render Layout.new(title: "Getting started with Rails") do
|
8
|
-
render Markdown.new <<~MD
|
9
|
-
# Collections
|
10
|
-
|
11
|
-
Phlex comes with an abstract pattern for views that represent collections of resources β lists, grids, tables, etc. Collections have two parts: one part wraps the whole collection, the other part is repeated once for each item in that collection.
|
12
|
-
|
13
|
-
When you include `Phlex::Collection` in a `Phlex::HTML`, the `template` and `initialize` methods are defined for you. You don't need to define these. Instead, you define a `collection_template` and `item_template`.
|
14
|
-
|
15
|
-
## Collection template
|
16
|
-
|
17
|
-
The `collection_template` method should accept a content block which is used to yield the items. We can yield this block or pass it to another element, such as `<ul>`.
|
18
|
-
|
19
|
-
```ruby
|
20
|
-
def collection_template(&)
|
21
|
-
ul(&)
|
22
|
-
end
|
23
|
-
```
|
24
|
-
|
25
|
-
## Item template
|
26
|
-
|
27
|
-
From the `item_template` method, you can access a single item with the `@item` instance variable.
|
28
|
-
|
29
|
-
```ruby
|
30
|
-
def item_template
|
31
|
-
li { @item }
|
32
|
-
end
|
33
|
-
```
|
34
|
-
|
35
|
-
## Rendering a collection
|
36
|
-
|
37
|
-
Putting it all together, we can create a `List` view that renders each item in an `<li>`, all wrapped up in an outer `<ul>`.
|
38
|
-
|
39
|
-
We can render the list by passing any Enumerable as the `collection` keyword argument. Here, we pass the array `["A", "B", "C"]`.
|
40
|
-
MD
|
41
|
-
|
42
|
-
render Example.new do |e|
|
43
|
-
e.tab "list.rb", <<~RUBY
|
44
|
-
class List < Phlex::HTML
|
45
|
-
include Phlex::Collection
|
46
|
-
|
47
|
-
def collection_template(&content)
|
48
|
-
ul(&content)
|
49
|
-
end
|
50
|
-
|
51
|
-
def item_template
|
52
|
-
li { @item }
|
53
|
-
end
|
54
|
-
end
|
55
|
-
RUBY
|
56
|
-
|
57
|
-
e.tab "example.rb", <<~RUBY
|
58
|
-
class Example < Phlex::HTML
|
59
|
-
def template
|
60
|
-
render List.new(
|
61
|
-
collection: ["A", "B", "C"]
|
62
|
-
)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
RUBY
|
66
|
-
|
67
|
-
e.execute "Example.new.call"
|
68
|
-
end
|
69
|
-
|
70
|
-
render Markdown.new <<~MD
|
71
|
-
## Rendering a single item
|
72
|
-
|
73
|
-
Sometimes you need to render one item of a collection on its own. This is especially handy if you're using Hotwire to append an item to the end of an existing collection. You can render an individual item without the collection wrapper by passing an `item` keyword argument to the collection view.
|
74
|
-
|
75
|
-
```ruby
|
76
|
-
render List.new(item: "A")
|
77
|
-
```
|
78
|
-
MD
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
@@ -1,57 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Pages
|
4
|
-
module Rails
|
5
|
-
class GettingStarted < ApplicationPage
|
6
|
-
def template
|
7
|
-
render Layout.new(title: "Getting started with Rails") do
|
8
|
-
render Markdown.new <<~MD
|
9
|
-
# Getting started with Phlex on Rails
|
10
|
-
|
11
|
-
While Phlex can be used in any Ruby project, it's especially great with [Rails](https://rubyonrails.org). But before we get into the details, it's important to understand that Phlex is very different from [ActionView](https://guides.rubyonrails.org/action_view_overview.html) and [ViewComponent](https://viewcomponent.org).
|
12
|
-
|
13
|
-
In Phlex, _layouts_, _pages_ and _components_ (or "partials") are the same thing. Phlex Views are Ruby objects that represent every piece of your app's user interface, from pages and layouts and nav-bars, to headings and buttons and links. They're not templates like ERB files in ActionView / ViewComponent; they are just Ruby objects.
|
14
|
-
|
15
|
-
It might feel a bit weird at first, but you'll soon realise how weird it was writing procedural templates in ERB while every other part of your app was object-oriented Ruby.
|
16
|
-
|
17
|
-
## Setup
|
18
|
-
|
19
|
-
If you haven't installed Phlex already, you'll need to add it to your Gemfile. This is a good place to require `phlex/rails`.
|
20
|
-
|
21
|
-
```ruby
|
22
|
-
gem "phlex", require: "phlex/rails"
|
23
|
-
```
|
24
|
-
|
25
|
-
Now run `bundle install`. Once that's finished, you'll want to run the setup script: `bin/rails phlex:install`.
|
26
|
-
|
27
|
-
This script will:
|
28
|
-
|
29
|
-
1. update `config/application.rb` to include `/app` in your auto-load paths;
|
30
|
-
2. generate `views/application_view.rb`.
|
31
|
-
|
32
|
-
Like `ApplicationRecord`, `ApplicationView` is your base view which all your other views should inherit from.
|
33
|
-
|
34
|
-
## Naming conventions
|
35
|
-
|
36
|
-
We recommend using the Zeitwerk conventions for naming. For example, your Articles index page, would be called `Views::Articles::Index` and live in `app/views/articles/index.rb`.
|
37
|
-
|
38
|
-
## View generator
|
39
|
-
|
40
|
-
You can generate new views with the `rails g phlex:view` command.
|
41
|
-
|
42
|
-
For example, running `rails g phlex:view Articles::Index` will create `app/views/articles/index.rb` with the following:
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
module Views
|
46
|
-
class Articles::Index < ApplicationView
|
47
|
-
def template
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
```
|
52
|
-
MD
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
data/docs/pages/rails/helpers.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "phlex/rails"
|
4
|
-
|
5
|
-
module Pages
|
6
|
-
module Rails
|
7
|
-
class Helpers < ApplicationPage
|
8
|
-
def template
|
9
|
-
render Layout.new(title: "Using Rails helpers in Phlex views") do
|
10
|
-
render Markdown.new <<~MD
|
11
|
-
# Using Rails helpers in Phlex views
|
12
|
-
|
13
|
-
## The `helpers` proxy
|
14
|
-
|
15
|
-
You can use the `helpers` proxy to access any Rails helper from a Phlex view. For example, you can use the `#t` helper for translations:
|
16
|
-
|
17
|
-
```ruby
|
18
|
-
module Views
|
19
|
-
class Hello < ApplicationView
|
20
|
-
delegate :t, to: :helpers
|
21
|
-
|
22
|
-
def template
|
23
|
-
h1 do
|
24
|
-
t "hello"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
```
|
30
|
-
|
31
|
-
## Layout helpers
|
32
|
-
|
33
|
-
Rails tag helpers return strings which makes them less than ideal to use from Phlex. Including `Phlex::Rails::Layout` gives you access to the following Rails helper proxies which immediately output to the buffer:
|
34
|
-
|
35
|
-
#{(Phlex::Rails::Layout.instance_methods - Module.methods).sort.map { "1. `#{_1}`" }.join("\n")}
|
36
|
-
|
37
|
-
Using these is equvalent to passing the output of the original Rails helpers to `raw`, e.g:
|
38
|
-
|
39
|
-
```ruby
|
40
|
-
unsafe_raw helpers.javascript_include_tag
|
41
|
-
```
|
42
|
-
|
43
|
-
## Including proxies
|
44
|
-
|
45
|
-
The following modules can be included for direct access to these helpers:
|
46
|
-
|
47
|
-
#{Phlex::Rails::Helpers.constants.sort.map { "1. `Phlex::Rails::Helpers::#{_1}`" }.join("\n")}
|
48
|
-
|
49
|
-
Note, helpers that produce HTML are adapted to output to the buffer directly.
|
50
|
-
MD
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
data/docs/pages/rails/layouts.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Pages
|
4
|
-
module Rails
|
5
|
-
class Layouts < ApplicationPage
|
6
|
-
def template
|
7
|
-
render Layout.new(title: "Getting started with Rails") do
|
8
|
-
render Markdown.new <<~MD
|
9
|
-
# Layouts in Rails
|
10
|
-
|
11
|
-
Rails has an implicit layouts feature that automatically wraps views in a layout template, usually `views/layouts/application.html.erb`. When using Phlex, you'll probably want to by-pass this feature completely. Here's why:
|
12
|
-
|
13
|
-
Sometimes you need to pass some argument to the layout, such as a page title that needs to be rendered in a `<title>` tag in the HTML `<head>`. Rails lets you do this from another part of the view using the `content_for` feature, but this pattern precludes defining an explicit interface for your layout through its initializer.
|
14
|
-
|
15
|
-
If layouts are Phlex views, they can be rendered just like any other view and can require that `title` argument from their initializer. The trick is the page view renders its content into the layout view.
|
16
|
-
MD
|
17
|
-
|
18
|
-
render Example.new do |e|
|
19
|
-
e.tab "layout.rb", <<~RUBY
|
20
|
-
module Views
|
21
|
-
class Layout < Phlex::HTML
|
22
|
-
def initialize(title:)
|
23
|
-
@title = title
|
24
|
-
end
|
25
|
-
|
26
|
-
def template(&)
|
27
|
-
html do
|
28
|
-
head do
|
29
|
-
title { @title }
|
30
|
-
end
|
31
|
-
|
32
|
-
body(&)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
RUBY
|
38
|
-
|
39
|
-
e.tab "index.rb", <<~RUBY
|
40
|
-
module Views
|
41
|
-
class Index < Phlex::HTML
|
42
|
-
def template
|
43
|
-
render Layout.new(title: "Hello") do
|
44
|
-
h1 { "π" }
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
RUBY
|
50
|
-
|
51
|
-
e.execute "Views::Index.new.call"
|
52
|
-
end
|
53
|
-
|
54
|
-
render Markdown.new <<~MD
|
55
|
-
If you're using a Phlex view as a layout, you'll want to disable layouts from your Rails controller. You can do this by adding `layout false` to your controller. In a new app, you'll probably want to add this to the `ApplicationController`.
|
56
|
-
MD
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Pages
|
4
|
-
class Rails::Migrating < ApplicationPage
|
5
|
-
def template
|
6
|
-
render Layout.new(title: "Migrating to Phlex in Rails") do
|
7
|
-
render Markdown.new <<~MD
|
8
|
-
# Migrating an existing Rails app to Phlex
|
9
|
-
|
10
|
-
Whether you currently use ActionView or ViewComponent with ERB, HAML or Slim, you can start using Phlex in your Rails app today without a big rewrite.
|
11
|
-
|
12
|
-
## You can render Phlex views into existing templates
|
13
|
-
|
14
|
-
Phlex views implement the _renderable_ interface for Rails, which means they can be rendered from a controller or another view template β even ViewComponent templates. This means you can gradually migrate specific pages and components to Phlex without having to change anything else.
|
15
|
-
|
16
|
-
## You can render ActionView partials and ViewComponent components in Phlex views
|
17
|
-
|
18
|
-
That's right, the `render` method in Phlex doesn't only work with Phlex views. You can use it to render ActionView partials and ViewComponent components too.
|
19
|
-
|
20
|
-
Say you have an `articles/index.html.erb` template that renders a list of articles using the `articles/_article.html.erb` partial. You can convert the index template to an `Articles::Index` Phlex view while continuing to render the same ActionView partial inside it.
|
21
|
-
|
22
|
-
## Use an ERB β Phlex converter
|
23
|
-
|
24
|
-
The ERB β Phlex converter, [Phlexing](https://www.phlexing.fun), can do the heavy-lifting but it won't help you architect your components / design system. Take your time, converting things piece-by-piece.
|
25
|
-
|
26
|
-
If you're using ViewComponent, you might find you can convert components to Phlex views without even changing any call-sites.
|
27
|
-
|
28
|
-
## Save the layout 'til last
|
29
|
-
|
30
|
-
After everything I said about layouts in the previous section, I'll let you in on a little secret: Phlex actually does support `content_for` in one direction. You can't yield a `content_for` block in Phlex, but you can assign one.
|
31
|
-
|
32
|
-
If you're working on an established Rails app, the layout is probably the last thing you should convert. Just include `Phlex::Rails::Helpers::ContentFor` in your `ApplicationView` and you'll be able to render Phlex views into existing ActionView layouts and assign `content_for` blocks too.
|
33
|
-
MD
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Pages
|
4
|
-
class Rails::RenderingViews < ApplicationPage
|
5
|
-
def template
|
6
|
-
render Layout.new(title: "Getting started with Rails") do
|
7
|
-
render Markdown.new <<~MD
|
8
|
-
# Rendering Phlex views in Rails
|
9
|
-
|
10
|
-
You can render a `Phlex::HTML` from your Rails controller actions as well as other views, and even from ActionView / ViewComponent templates.
|
11
|
-
|
12
|
-
Instead of implicitly rendering an ERB template with automatic access to all your controller instance variables, you need to explicitly render Phlex views from your controller action methods.
|
13
|
-
|
14
|
-
Doing this allows you to design views without implicit dependencies on controller instance variables, making them much easier to test and reuse and reason about.
|
15
|
-
|
16
|
-
```ruby
|
17
|
-
class ArticlesController < ApplicationController
|
18
|
-
def index
|
19
|
-
render Views::Articles::Index.new(
|
20
|
-
articles: Article.all.load_async
|
21
|
-
)
|
22
|
-
end
|
23
|
-
|
24
|
-
def show
|
25
|
-
render Views::Articles::Show.new(
|
26
|
-
article: Article.find(params[:id])
|
27
|
-
)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
```
|
31
|
-
MD
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,58 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Pages
|
4
|
-
class RailsIntegration < ApplicationPage
|
5
|
-
def template
|
6
|
-
render Layout.new(title: "Ruby on Rails integration") do
|
7
|
-
render Markdown.new(<<~MD)
|
8
|
-
# Ruby on Rails integration
|
9
|
-
|
10
|
-
## Installation
|
11
|
-
|
12
|
-
To install Phlex into your Rails application, you can run the `bin/rails phlex:install` command.
|
13
|
-
|
14
|
-
## View generator
|
15
|
-
|
16
|
-
You can generate new views with the `rails g phlex:view` command.
|
17
|
-
|
18
|
-
For example, running `rails g phlex:view Card` will create the following file:
|
19
|
-
MD
|
20
|
-
|
21
|
-
render CodeBlock.new(<<~RUBY, syntax: :ruby)
|
22
|
-
# app/views/card.rb
|
23
|
-
|
24
|
-
module Views
|
25
|
-
class Card < ApplicationView
|
26
|
-
def template
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
RUBY
|
31
|
-
|
32
|
-
render Markdown.new(<<~MD)
|
33
|
-
## Helpers
|
34
|
-
|
35
|
-
You can use the `helpers` proxy to access helpers within your views.
|
36
|
-
|
37
|
-
For example, you can use the `#t` helper for translations:
|
38
|
-
MD
|
39
|
-
|
40
|
-
render CodeBlock.new(<<~RUBY, syntax: :ruby)
|
41
|
-
# app/views/hello.rb
|
42
|
-
|
43
|
-
module Views
|
44
|
-
class Hello < ApplicationView
|
45
|
-
delegate :t, to: :helpers
|
46
|
-
|
47
|
-
def template
|
48
|
-
h1 do
|
49
|
-
t "hello"
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
RUBY
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
data/docs/pages/templates.rb
DELETED
@@ -1,144 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Pages
|
4
|
-
class Templates < ApplicationPage
|
5
|
-
def template
|
6
|
-
render Layout.new(title: "Templates in Phlex") do
|
7
|
-
render Markdown.new(<<~MD)
|
8
|
-
# Working with templates
|
9
|
-
|
10
|
-
In Phlex, templates are just methods that call other methods that add things to the output buffer. When you call the method `h1`, an `<h1>` tag is buffered for output.
|
11
|
-
|
12
|
-
## Tag attributes
|
13
|
-
|
14
|
-
You can add attributes to HTML tags by passing keyword arguments to the tag methods.
|
15
|
-
MD
|
16
|
-
|
17
|
-
render Example.new do |e|
|
18
|
-
e.tab "hello.rb", <<~RUBY
|
19
|
-
class Hello < Phlex::HTML
|
20
|
-
def template
|
21
|
-
h1(class: "text-xl font-bold") { "π Hello World!" }
|
22
|
-
end
|
23
|
-
end
|
24
|
-
RUBY
|
25
|
-
|
26
|
-
e.execute "Hello.new.call"
|
27
|
-
end
|
28
|
-
|
29
|
-
render Markdown.new(<<~MD)
|
30
|
-
## Hash attributes
|
31
|
-
|
32
|
-
If you pass a `Hash` as an attribute value, the hash will be flattened with a dash between each section.
|
33
|
-
MD
|
34
|
-
|
35
|
-
render Example.new do |e|
|
36
|
-
e.tab "hello.rb", <<~RUBY
|
37
|
-
class Hello < Phlex::HTML
|
38
|
-
def template
|
39
|
-
div(data: { controller: "hello" }) do
|
40
|
-
# ...
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
RUBY
|
45
|
-
|
46
|
-
e.execute "Hello.new.call"
|
47
|
-
end
|
48
|
-
|
49
|
-
render Markdown.new(<<~MD)
|
50
|
-
## Boolean attributes
|
51
|
-
|
52
|
-
When `true`, the attribute will be rendered without a value; when _falsy_, the attribute isnβt rendered at all. You can still use the strings `"true"` and `"false"` as values for non-boolean attributes.
|
53
|
-
MD
|
54
|
-
|
55
|
-
render Example.new do |e|
|
56
|
-
e.tab "channel_controls.rb", <<~RUBY
|
57
|
-
class ChannelControls < Phlex::HTML
|
58
|
-
def template
|
59
|
-
input(
|
60
|
-
value: "1",
|
61
|
-
name: "channel",
|
62
|
-
type: "radio",
|
63
|
-
checked: true
|
64
|
-
)
|
65
|
-
|
66
|
-
input(
|
67
|
-
value: "2",
|
68
|
-
name: "channel",
|
69
|
-
type: "radio",
|
70
|
-
checked: false
|
71
|
-
)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
RUBY
|
75
|
-
|
76
|
-
e.execute "ChannelControls.new.call"
|
77
|
-
end
|
78
|
-
|
79
|
-
render Markdown.new(<<~MD)
|
80
|
-
## The template tag
|
81
|
-
|
82
|
-
Because the `template` method is used to define the view template itself, you'll need to use the method `template_tag` if you want to to render an HTML `<template>` tag.
|
83
|
-
MD
|
84
|
-
|
85
|
-
render Example.new do |e|
|
86
|
-
e.tab "example.rb", <<~RUBY
|
87
|
-
class Example < Phlex::HTML
|
88
|
-
def template
|
89
|
-
template_tag do
|
90
|
-
img src: "hidden.jpg", alt: "A hidden image."
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
RUBY
|
95
|
-
|
96
|
-
e.execute "Example.new.call"
|
97
|
-
end
|
98
|
-
|
99
|
-
render Markdown.new(<<~MD)
|
100
|
-
## Stand-alone text
|
101
|
-
|
102
|
-
You can output text content without wrapping it in an element by using the `text` helper method.
|
103
|
-
MD
|
104
|
-
|
105
|
-
render Example.new do |e|
|
106
|
-
e.tab "heading.rb", <<~RUBY
|
107
|
-
class Heading < Phlex::HTML
|
108
|
-
def template
|
109
|
-
h1 do
|
110
|
-
strong { "Hello " }
|
111
|
-
text "World!"
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
RUBY
|
116
|
-
|
117
|
-
e.execute "Heading.new.call"
|
118
|
-
end
|
119
|
-
|
120
|
-
render Markdown.new(<<~MD)
|
121
|
-
## Whitespace
|
122
|
-
|
123
|
-
The example output on this site is formatted for readability, but there is usually no whitespace between HTML tags in the output. If you need to add some whitespace, you can use the `whitespace` helper. This is useful for adding space between _inline_ elements to allow them to wrap.
|
124
|
-
MD
|
125
|
-
|
126
|
-
render Example.new do |e|
|
127
|
-
e.tab "links.rb", <<~RUBY
|
128
|
-
class Links < Phlex::HTML
|
129
|
-
def template
|
130
|
-
a(href: "/") { "Home" }
|
131
|
-
whitespace
|
132
|
-
a(href: "/about") { "About" }
|
133
|
-
whitespace
|
134
|
-
a(href: "/contact") { "Contact" }
|
135
|
-
end
|
136
|
-
end
|
137
|
-
RUBY
|
138
|
-
|
139
|
-
e.execute "Links.new.call"
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|