Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/blakewilliams/component_embedded_ruby

Strict Ruby templates with component support
https://github.com/blakewilliams/component_embedded_ruby

Last synced: 16 days ago
JSON representation

Strict Ruby templates with component support

Awesome Lists containing this project

README

        

# Component Embedded Ruby

Strict HTML templating with support for components.

### Features:

* Strict HTML parsing. eg: matching end tags are enforced
* HTML attributes are either static, or dynamic. No more `class="hello <%=
extra_classes %>`, instead this logic should be pushed up to components.
* Component rendering has a single dependency, a `render` method being present
in the rendering context.
* automatic Rails support for `crb` extension

### Usage

Define a template:

```ruby


hello world


```

Define a component

```ruby
class Capitalization
def initialize(upcase: false)
@upcase = upcase
end

def render_in(_view_context)
children = yield

if @upcase
children.upcase
else
children
end
end
end
```

Render it

```ruby
ComponentEmbeddedRuby.render(template_string)
```

See results

```html

HELLO WORLD


```

If trying to render outside of a Rails environment, ensure that the binding
passed to the renderer has a top-level `render` method that can accept component
instances and convert them to strings.

e.g. the most basic example could look like this:

```ruby
def render(renderable, &block)
# This assumes components being rendered utilize `to_s` to render their
# templates
renderable.to_s(&block)
end
```

For more examples, check out the `ComponentEmbeddedRuby::Renderable` tests.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/BlakeWilliams/component_embedded_ruby.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).