Skip to content

Commit eb1498f

Browse files
committed
Change partial syntax from -partial suffix to _ prefix
1 parent f176216 commit eb1498f

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

README.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Thanks to Hotwire, it's now possible to build sophisticated server-rendered user
1313

1414
With Theo, you can render a partial using HTML-like syntax:
1515
```html
16-
<button-partial size="large" label%="label" />
16+
<_button size="large" label%="label" />
1717
```
1818

1919

@@ -51,9 +51,9 @@ is equivalent to:
5151

5252
Rendering a partial in ERB requires switching between HTML markup and Ruby code, and the `render` verb makes it difficult to imagine a page as a component structure.
5353

54-
In Theo, you render a partial by writing a tag with `-partial` suffix, for example:
54+
In Theo, you render a partial by writing a tag with `_` prefix, for example:
5555
```html
56-
<button-partial size="large" />`
56+
<_button size="large" />`
5757
```
5858
is equivalent to:
5959
```erb
@@ -62,9 +62,9 @@ is equivalent to:
6262

6363
Naturally, partials can also include content, e.g.:
6464
```html
65-
<button-partial size="large">
65+
<_button size="large">
6666
Create
67-
</button-partial>
67+
</_button>
6868
```
6969

7070
> [!TIP]
@@ -75,7 +75,7 @@ Naturally, partials can also include content, e.g.:
7575

7676
You can render a collection of partials as follows:
7777
```html
78-
<widget-partial collection%="widgets" />
78+
<_widget collection%="widgets" />
7979
```
8080
which is equivalent to:
8181
```erb
@@ -84,26 +84,26 @@ which is equivalent to:
8484

8585
You can also customize the local variable name via the `as` attribute, e.g.:
8686
```html
87-
<widget-partial collection%="widgets" as="item" />
87+
<_widget collection%="widgets" as="item" />
8888
```
8989

9090
#### Boolean attributes
9191

9292
If an attribute has no value, you can omit it, for example:
9393
```html
94-
<button-partial disabled />
94+
<_button disabled />
9595
```
9696
is equivalent to:
9797
```html
98-
<button-partial disabled="" />
98+
<_button disabled="" />
9999
```
100100

101101

102102
#### Path
103103

104104
To render a partial from another folder, use the 'path' attribute, e.g.:
105105
```html
106-
<widget-partial path="widgets" />
106+
<_widget path="widgets" />
107107
```
108108
is equivalent to:
109109
```erb
@@ -115,9 +115,9 @@ is equivalent to:
115115

116116
Partials can yield a value, such as a builder object that can be used by child partials. For example:
117117
```html
118-
<widget-builder-partial yields="widget">
119-
<widget-element-partial widget%="widget" />
120-
</widget-builder-partial>
118+
<_widget_builder yields="widget">
119+
<_widget_element widget%="widget" />
120+
</_widget_builder>
121121
```
122122
is equivalent to:
123123
```erb
@@ -130,9 +130,9 @@ is equivalent to:
130130

131131
Instead of using `yields` attribute, a parent partial can indirectly pass a variable to its children using the `provide` and `inject` helpers. The example above can be modified as follows:
132132
```html
133-
<widget-builder-partial>
134-
<widget-element-partial />
135-
</widget-builder-partial>
133+
<_widget_builder>
134+
<_widget_element />
135+
</_widget_builder>
136136
```
137137

138138
`_widget_builder.html.theo`:
@@ -156,7 +156,7 @@ Instead of using `yields` attribute, a parent partial can indirectly pass a vari
156156
You can freely mix ERB and Theo syntax, e.g.:
157157
```erb
158158
<% if total_amount > 100 %>
159-
<free-shipping-partial amount%="total_amount" />
159+
<_free_shipping amount%="total_amount" />
160160
<% end %>
161161
```
162162

@@ -165,19 +165,19 @@ You can freely mix ERB and Theo syntax, e.g.:
165165

166166
You can build a `<form>` element in ERB using [ActionView form helpers](https://guides.rubyonrails.org/form_helpers.html). Theo provides corresponding partials. For example:
167167
```html
168-
<form-with-partial model%="widget" data-turbo-confirm="Are you sure?">
168+
<_form_with model%="widget" data-turbo-confirm="Are you sure?">
169169
<div>
170-
<label-partial name="name" />
171-
<text-field-partial name="name" />
170+
<_label name="name" />
171+
<_text_field name="name" />
172172
</div>
173173

174174
<div>
175-
<label-partial name="size" />
176-
<select-partial name="size" options%="['Big', 'Small']" />
175+
<_label name="size" />
176+
<_select name="size" options%="['Big', 'Small']" />
177177
</div>
178178

179-
<submit-partial value="Create" />
180-
</form-with-partial>
179+
<_submit value="Create" />
180+
</_form_with>
181181
```
182182
is equivalent to:
183183
```erb

lib/theo-rails/theo.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Rails
66
DYNAMIC_ATTRIBUTE = /(?:(?<name>#{ATTRIBUTE_NAME.source})\s*%=\s*#{ATTRIBUTE_VALUE.source})/
77
ATTRIBUTES = /(?<attrs>(?:\s+#{ATTRIBUTE.source})*)/
88
LITERAL_ATTRIBUTES = %i[path as yields].freeze
9-
PARTIAL_TAG = /(?<partial>[\w-]+-partial)/
9+
PARTIAL_TAG = /(?<partial>_[\w-]+)/
1010
PARTIAL = /(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*>(?<content>.*?)<\/\k<partial>>)|(?:<#{PARTIAL_TAG.source}#{ATTRIBUTES.source}\s*\/>)/im
1111
DYNAMIC_EXPRESSION = /^<%=([^%]*)%>$/
1212

@@ -18,7 +18,7 @@ def process(source)
1818
# Partials
1919
source.gsub(PARTIAL) do |_|
2020
match = Regexp.last_match
21-
partial = (match[:partial]).delete_suffix('-partial').underscore
21+
partial = (match[:partial]).delete_prefix('_')
2222
attributes = match[:attrs] || ''
2323
content = match[:content]&.strip
2424

0 commit comments

Comments
 (0)