-
#1249's in, which puts all generator options under the The sections are module ViewComponent
class DocsBuilderComponent < Base
class Section < Struct.new(:heading, :methods, :show_types, keyword_init: true)
def initialize(heading: nil, methods: [], show_types: true)
methods.sort_by! { |method| method[:name] }
super
end
end
end
end At the point that I wrote this, it wasn't intuitive for me to go from that But I also wasn't in much of a hurry to do so. The view code for the component is as follows: ---
layout: default
title: API
nav_order: 3
---
<!-- Warning: AUTO-GENERATED file, don't edit. Add code comments to your Ruby instead <3 -->
# API
<% @sections.each do |section| %>
## <%= section.heading %>
<% section.methods.each do |method| %>
### <%== render ViewComponent::DocsBuilderComponent::MethodDoc.new(method) %>
<% end %>
<% end %> All the complexity of the would-be Having mulled this over for a little while, I'm wondering if there'd be a way for module ViewComponent
class DocsBuilderComponent < Base
class Section < ViewComponent::Base
def initialize(heading: nil, methods: [], show_types: true)
@heading = heading
@show_types = show_types
@methods = methods.sort_by! { |method| method[:name] }
super
end
def call
erb = <<~ERB
## <%= @heading %>
<% @methods.each do |method| %>
### <%= render ViewComponent::DocsBuilderComponent::MethodDoc.new(method) %>
<% end %>
ERB
ERB.new(erb).result(view_context.send(:binding))
end
end
end
end This has a few perks:
It's also got drawbacks:
What do folks think of this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @boardfish, sorry we've taken a long time to respond. I think putting ERB into Ruby files is a great idea 😄 That's the sort of approach to templating Rux advocates for and what JSX does too. While I would personally prefer to see a transpilation step or even template language support built into Ruby, that's probably a long way off. I wonder if it makes sense to stop at just supporting inline ERB. What about HAML, Slim, etc? We could have method suffixes like My only real concern is that people might think they should write all their components this way, which IMHO is a mistake for larger components. It can get pretty overwhelming to look at a 1000+ line |
Beta Was this translation helpful? Give feedback.
Hey @boardfish, sorry we've taken a long time to respond.
I think putting ERB into Ruby files is a great idea 😄 That's the sort of approach to templating Rux advocates for and what JSX does too. While I would personally prefer to see a transpilation step or even template language support built into Ruby, that's probably a long way off.
I wonder if it makes sense to stop at just supporting inline ERB. What about HAML, Slim, etc? We could have method suffixes like
call_erb
,call_haml
, etc that could automatically invoke the correct template handler.My only real concern is that people might think they should write all their components this way, which IMHO is a mistake for larger components.…