Skip to content

Commit 616d79a

Browse files
committed
More stuff
1 parent d5a807c commit 616d79a

File tree

6 files changed

+82
-9
lines changed

6 files changed

+82
-9
lines changed

β€Žcompare/slim.md

+39
Original file line numberDiff line numberDiff line change
@@ -1 +1,40 @@
11
# Phlex vs Slim
2+
3+
[Slim](https://slim-template.github.io)’s goal is reduce templating syntax to just the essential parts without becoming cryptic.
4+
5+
We can’t really make a direct comparison between Phlex and Slim becuase Slim is not a component framework and Phlex is not a templating language. However, there are some similarities between the two that we can look at. We can also get into why Phlex is designed around this idea of a β€œcomponent”.
6+
7+
### Minimal templating syntax
8+
9+
If you’re using Slim, there’s a good chance you wanted something with a more minimal syntax than ERB, especially when it comes to switching between the HTML parts and the Ruby parts, such as an `if` conditional.
10+
11+
Phlex too has a pretty minimal syntax, with one significant difference: the Phlex syntax is just Ruby. You don’t need to learn anything else, you already know it. Modules, classes, methods, arguments, blocks. That’s it.
12+
13+
Because it’s just Ruby, the transition between the Ruby code and the template code is even more seamless. There’s no transition because it’s all Ruby. One factor to consider is Ruby doesn’t have significant whitespace.
14+
15+
### Components and abstraction
16+
17+
Phlex is designed around the idea of a β€œcomponent”. A component is a Ruby class that represents a small part of the page. Extracting small components helps keep your user experience consistent and makes your code much easier to maintain in the long run.
18+
19+
Additionally, because Phlex is just Ruby, you can start by extracting methods.
20+
21+
```ruby
22+
def MyButton(...)
23+
button(class: "my button classes", ...)
24+
end
25+
```
26+
27+
When you realise you need more options, you can upgrade that to a class.
28+
29+
```ruby
30+
class MyButton < ApplicationComponent
31+
def initialize(style:, color:)
32+
@style = style
33+
@color = color
34+
end
35+
36+
def view_template(&)
37+
button(class: [@style, @color], &)
38+
end
39+
end
40+
```

β€Žhandbook/attributes.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,21 @@ h1(data: { controller: "hello" }) { "Hello!" }
4848

4949
## Attribute values
5050

51-
You’ve seen how string values work. Symbols behave the same way. You’ve also seen how to nest attributes with hashes. But Phlex allows a few other types of attribute value.
51+
### Symbols
52+
53+
Like keys, if you use symbols for values, Phlex will convert them to strings, replacing underscores `_` with dashes `-`.
54+
55+
::: code-group
56+
57+
```ruby [component]
58+
div(contenteditable: :plaintext_only)
59+
```
60+
61+
```html [output]
62+
<div contenteditable="plaintext-only"></div>
63+
```
64+
65+
:::
5266

5367
### Arrays and sets
5468

@@ -84,7 +98,9 @@ textarea(disabled: false)
8498
:::
8599

86100
::: tip
87-
Some HTML attributes such as `contenteditable` require you to pass `"true"` or `"false"` as a string. These are not technically β€œboolean” attributes, they're β€œenumerated” attributes. The distinction is subtle but important.
101+
Some HTML attributes such as `contenteditable` require you to pass `"true"` or `"false"` as strings. These are not really _boolean_ attributes even though they look similar; they’re technically _β€œenumerated”_ attributes.
102+
103+
According to [the MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable), `contenteditable` accepts `"true"`, `"false"` or `"plaintext-only"`. The presence of this third option explains why `contenteditable` is not a boolean attribute. It also means new modes could be added in the future without breaking existing code.
88104

89105
:::
90106

@@ -130,8 +146,24 @@ a(
130146
<a class="button active">πŸ‘‹ Hello World!</a>
131147
```
132148

149+
:::
150+
133151
In this example, the `button` class is always added, while the `active` and `disabled` classes are conditional. You can read `=>` as β€œif”.
134152

153+
Phlex also ignores `nil` values, so another way you could write this is:
154+
155+
```ruby
156+
a(
157+
class: [
158+
("button"),
159+
("active" if is_active),
160+
("disabled" if is_disabled)
161+
]
162+
) { "Click me" }
163+
```
164+
165+
The parentheses around `"button"` here are not strictly necessary because it’s not paired with a conditional, but they make the code more consistent. Also, this last technique works for any attribute, not just `class`.
166+
135167
### `style`
136168

137169
Like `class`, the `style` attribute has special behaviour. If you pass a Hash to `style`, Phlex will convert it to a CSS string:

β€Žhandbook/helpers.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Helpers

β€Žhandbook/testing.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Testing

β€Žindex.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ hero:
1616

1717
features:
1818
- title: Pure, beautiful Ruby
19-
icon: πŸ’Ž
20-
details: Phlex gives you HTML semantics in Ruby syntax so you can use your existing skills designing object-oriented views. Plus, you get to use tools like RubyLSP, Rubocop and Simplecov.
19+
icon: πŸ§‘β€πŸ³
20+
details: Phlex gives you HTML semantics in Ruby so you can use your existing skills designing object-oriented views. Plus, you get to use tools like RubyLSP, Rubocop and Simplecov.
2121
- title: Fast enough
2222
icon: πŸš€
23-
details: Phlex renders HTML at over 1gbps on a MacBook Pro and unlike Rails partials, it doesn’t slow down the more components you extract.
23+
details: Phlex renders HTML at ~1gbps per core on a MacBook Pro (M3 Max) and it doesn’t slow down the more components you extract.
2424
- title: Rails integration
2525
icon: πŸš‚
2626
details: Phlex works great with Rails. It supports all Rails helpers and plays nicely with ViewComponent, ActionView, Stimulus, Turbo and Tailwind.
2727
- title: Structural safety
2828
icon: πŸ›‘οΈ
29-
details: Phlex is designed to prevent cross-site-scripting (XSS) attacks by default.
29+
details: Phlex is designed to structurally prevent cross-site-scripting (XSS) attacks by default.
3030
- title: Sensible isolation
3131
icon: πŸ§ͺ
3232
details: Phlex components only depend on the data you pass in, making them easier to test and reuse.
@@ -39,7 +39,7 @@ features:
3939
- title: Selective rendering
4040
icon: πŸ”Ž
4141
details: You can render a view targeting a specific DOM ID. Phlex only does the work to render just the parts you want. This is great for partial Hotwire updates like Turbo Frames.
42-
- title: Streamable
42+
- title: Streaming
4343
icon: 🌊
44-
details: Phlex can stream responses to boost time to first byte (TTFB). In some cases users can see static content before the database has even responded.
44+
details: Phlex can stream responses to boost time to first byte (TTFB). In some cases users can see the first static content before the database has even responded.
4545
---

β€Žproject/community.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
## Articles πŸ“
2121

22-
- [Phlex not ERB](https://judoscale.com/blog/phlex-not-erb)
22+
- [Say No To Partials And Helpers For A Maintainable Rails Front-End](https://judoscale.com/blog/phlex-not-erb)
2323
- [A Tale of two Phlexes](https://blog.willcosgrove.com/a-tale-of-two-phlexes)
2424

2525
## Third-party component libraries πŸ“š

0 commit comments

Comments
Β (0)