forked from ViewComponent/view_component
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize documentation (ViewComponent#973)
This PR reorganizes our documentation a bit, fleshing out the landing page and the contributing guidelines. It's probably best reviewed via the Pages Preview.
- Loading branch information
1 parent
54aa0f5
commit 0a3fe23
Showing
5 changed files
with
174 additions
and
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--- | ||
layout: default | ||
title: Getting started | ||
parent: Building ViewComponents | ||
nav_order: 1 | ||
--- | ||
|
||
# Getting started | ||
|
||
## Conventions | ||
|
||
- Components are subclasses of `ViewComponent::Base` and live in `app/components`. It's common practice to create and inherit from an `ApplicationComponent` that is a subclass of `ViewComponent::Base`. | ||
- Component names end in -`Component`. | ||
- Component module names are plural, as for controllers and jobs: `Users::AvatarComponent` | ||
- Name components for what they render, not what they accept. (`AvatarComponent` instead of `UserComponent`) | ||
|
||
## Installation | ||
|
||
In `Gemfile`, add: | ||
|
||
```ruby | ||
gem "view_component", require: "view_component/engine" | ||
``` | ||
|
||
## Quick start | ||
|
||
Use the component generator to create a new ViewComponent. | ||
|
||
The generator accepts a component name and a list of arguments: | ||
|
||
```console | ||
bin/rails generate component Example title | ||
|
||
invoke test_unit | ||
create test/components/example_component_test.rb | ||
create app/components/example_component.rb | ||
create app/components/example_component.html.erb | ||
``` | ||
|
||
Available options to customize the generator are documented on the [Generators](/guide/generators.html) page. | ||
|
||
## Implementation | ||
|
||
A ViewComponent is a Ruby file and corresponding template file with the same base name: | ||
|
||
```ruby | ||
# app/components/example_component.rb | ||
class ExampleComponent < ViewComponent::Base | ||
def initialize(title:) | ||
@title = title | ||
end | ||
end | ||
``` | ||
|
||
```erb | ||
<%# app/components/example_component.html.erb %> | ||
<span title="<%= @title %>"><%= content %></span> | ||
``` | ||
|
||
Content passed to a ViewComponent as a block is captured and assigned to the `content` accessor. | ||
|
||
Rendered in a view as: | ||
|
||
```erb | ||
<%# app/views/home/index.html.erb %> | ||
<%= render(ExampleComponent.new(title: "my title")) do %> | ||
Hello, World! | ||
<% end %> | ||
``` | ||
|
||
Returning: | ||
|
||
```html | ||
<span title="my title">Hello, World!</span> | ||
``` | ||
|
||
## `#with_content` | ||
|
||
String content can also be passed to a ViewComponent by calling `#with_content`: | ||
|
||
```erb | ||
<%# app/views/home/index.html.erb %> | ||
<%= render(ExampleComponent.new(title: "my title").with_content("Hello, World!")) %> | ||
``` | ||
|
||
## Rendering from controllers | ||
|
||
It's also possible to render ViewComponents in controllers: | ||
|
||
```ruby | ||
# app/controllers/home_controller.rb | ||
def show | ||
render(ExampleComponent.new(title: "My Title")) { "Hello, World!" } | ||
end | ||
``` | ||
|
||
_In versions of Rails < 6.1, rendering a ViewComponent from a controller does not include the layout._ |
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 was deleted.
Oops, something went wrong.