https://github.com/existentialmutt/inline_view_component
Include template strings in Rails ViewComponent class definitions
https://github.com/existentialmutt/inline_view_component
Last synced: 9 months ago
JSON representation
Include template strings in Rails ViewComponent class definitions
- Host: GitHub
- URL: https://github.com/existentialmutt/inline_view_component
- Owner: existentialmutt
- License: mit
- Created: 2020-10-25T03:45:20.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-02T02:44:27.000Z (about 5 years ago)
- Last Synced: 2025-07-02T20:06:30.262Z (about 1 year ago)
- Language: Ruby
- Size: 48.8 KB
- Stars: 22
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# InlineViewComponent
This gem allows your ViewComponents to define template strings within the class definition. You should be able to use any templating language your rails app supports (ERB, HAML, Slim...). There's also custom syntax highlighting for heredoc templates for Sublime Text.
## Usage
Include the `InlineViewComponent` mixin and then specify your template string with `template(string)`. Be sure to delete your component's external template file or ViewComponent will raise an error.
#### Examples
```ruby
class ErbComponent < ViewComponent::Base
include InlineViewComponent
def message
"Hello World!"
end
template <<~ERB
<%= message %>
ERB
end
```
```ruby
class HamlComponent < ViewComponent::Base
include InlineViewComponent
def message
"Hello HAML!"
end
template <<~HAML, :haml
%p= message
HAML
end
```
You can provide any template format you have installed as an optional second argument to the template method
```ruby
template <<~SLIM, :slim
h1 Hello World!
SLIM
```
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'inline_view_component'
```
And then execute:
```bash
$ bundle
```
Or install it yourself as:
```bash
$ gem install inline_view_component
```
## Syntax Highlighting
Syntax highlighting for Sublime Text is available. Download [this file](/editor/Ruby.sublime-syntax) and add it to your Sublime User package. Then open your ruby files in the `Ruby (Custom)` syntax (or choose `View -> Syntax -> Open All with current extention as...` to use it automatically).
To get syntax highlighting to work use `ERB`, `HAML`, or `SLIM` as the delimiter for your heredoc string e.g.
```
template <<~HAML, :haml
%h1 A really great template
HAML
```
## TODO
This is an early release. Contributions are welcome!
- [ ] add syntax highlighting for more editors (VS Code, vim, ...)
## Contributing
Send a pull request.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).