armature icon indicating copy to clipboard operation
armature copied to clipboard

Capture values with blocks

Open jgaskins opened this issue 1 year ago • 0 comments

One of the limitations of templates currently is that you can't pass blocks to them in templates while also capturing the result as a value:

<%== MyComponent.new do %>
  <!-- render more stuff here -->
<% end %>

This sort of pattern can be super useful when you need the component to wrap other content — that is, it may have content both before and after the block.

For example, forms need to output an opening <form> tag and an <input> with the authenticity token, then output the form's content, then output the closing </form> tag. Since the code above doesn't work, Armature::Form has to receive the response and write to it imperatively rather than letting the template engine call to_s(io). That is completely unintuitive.

One pattern that the Ruby erubi gem uses is <%|==:

<%|== MyComponent.new do %>
<%| end %>

So maybe we can do that here, as well.

jgaskins avatar Jul 02 '24 17:07 jgaskins