Skip to content

ohaibbq/components

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Small library for rendering rails partials in to bodies of text

Usage

Create the component templates

app/views/shared/_media.html.erb
<div class="media-image">
  <%= image_tag media_image, data: { align: media_align } %>
  <%= content %>
</div>
app/views/shared/_post_header.html.erb
<h1><%= post.title %></h1>

Register the component

components.rb

Components.register([{
  name: :media,
  locals: [
    {name: :media_image},
    {name: :media_align, default: 'right'}
  ]
},
{
  name: :post_header
}])

Render the component

display_media.rb

Instances of component start and end tags will be rendered in to their respective position. Any content inside the tag is yielded as a content local to the partial.

body = <<-BODY
{{#post_header /}}
{{#media media_image="foo.jpg"}}
  OMG
{{/media}}
BODY

puts Components::Context.new(assigns: { post: Post.find(1) }).render_html(body)

display_media.rb outputs:

<h1>This was a post title</h1>
<div class="media-image">
  <img src="foo.jpg" data-align="right">
  foo
  OMG
</div>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages