-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to define templates that take parameters
```ruby class TestComponent < ViewComponent::Base template_arguments :list, :multiple # call_list now takes a `multiple` keyword argument def initialize(mode:) @mode = mode end def call case @mode when :list call_list multiple: false when :multilist call_list multiple: true when :summary call_summary end end end ```
- Loading branch information
Showing
9 changed files
with
121 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 3 additions & 11 deletions
14
test/sandbox/app/components/multiple_templates_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class MultipleTemplatesComponent < ViewComponent::Base | ||
def initialize(mode:) | ||
@mode = mode | ||
|
||
def initialize | ||
@items = ["Apple", "Banana", "Pear"] | ||
end | ||
|
||
def call | ||
case @mode | ||
when :list | ||
call_list | ||
when :summary | ||
call_summary | ||
end | ||
end | ||
template_arguments :list, :number | ||
template_arguments :summary, :string | ||
end |
2 changes: 1 addition & 1 deletion
2
test/sandbox/app/components/multiple_templates_component/list.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<ul> | ||
<ul data-number="<%= number %>"> | ||
<% @items.each do |item| %> | ||
<li><%= item %></li> | ||
<% end %> | ||
|
4 changes: 4 additions & 0 deletions
4
...sandbox/app/components/multiple_templates_component/multiple_templates_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<div class="container"> | ||
<%= call_summary string: "foo" %> | ||
<%= call_list number: 1 %> | ||
</div> |
2 changes: 1 addition & 1 deletion
2
test/sandbox/app/components/multiple_templates_component/summary.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<div>The items are: <%= @items.to_sentence %></div> | ||
<div>The items are: <%= @items.to_sentence %>, <%= string %></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters