No release in over 3 years
Low commit activity in last 3 years
HTML templates with embedded Ruby components
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.17
~> 5.0
~> 0.12.2
> 6.0
~> 13.0
~> 1.0
 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:

<h1>
  <Capitalization upcase={true}>hello world</Capitalization>
</h1>

Define a component

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

ComponentEmbeddedRuby.render(template_string)

See results

<h1>HELLO WORLD</h1>

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:

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.