ViewComponent / view_component

A framework for building reusable, testable & encapsulated view components in Ruby on Rails.
https://viewcomponent.org
MIT License
3.28k stars 429 forks source link

Support different formats #1062

Open vpereira opened 3 years ago

vpereira commented 3 years ago

Reading the code https://github.com/github/view_component/blob/0fd7cbf087248f3ce9c0becb4a00f598e361e3ea/lib/view_component/base.rb#L416 and documentation, I'm unsure we are able to use different formats like :xml or :text, right?

Problem:

I have a component that I want to render as :html and :text. For that, I created two views for my component, (i.e foo_component.html.haml and foo_component.text.haml. Somehow I expected to be able to pass the format to render, however I'm not able to, right?

Possible solution:

To render text "inline", I should have a method call to my class where I bypass any assigned template and render a string inline

Update

looks like I cannot have a template (i.e foo_component.html.haml) and implement the call method to deal with the inline rendering in the same class

Is that correct or did I miss anything? Any idea how to deal with that?

joelhawksley commented 3 years ago

@vpereira I'd be open to moving to support other formats. Could you open a PR that adds a test component with an API you think makes sense?

vpereira commented 3 years ago

@vpereira I'd be open to moving to support other formats. Could you open a PR that adds a test component with an API you think makes sense?

Hi @joelhawksley, thank you for your answer :)

So I will have to think about it. We have so many scenarios, basically we have:

a) regular components b) XML components (we have an XML API, where we use a lot of partials) c) helpers with some raw() calls embedded, which we wanted to extract to partials (with different formats), and consequently it would become a component itself.

the first case, is straight forward. For XML endpoints, would just be possible if ViewComponent would support XML format. The last case, I'm kind of solving it like https://github.com/openSUSE/open-build-service/pull/11583, where for html format, i render a template and for text format, i call a simple method (and bypass any template). BTW the "OBS" is probably one of the oldest rails code base being actively developed. We are on rails 6.1 and it exists since 2006.

For which scenario you would like to see a test component? for the XML API, or a component supporting many formats?

joelhawksley commented 3 years ago

@vpereira thanks for sharing the example PR! How cool to see ViewComponent in use in OBS ❤️

I think it would be best to prototype something that supports both raw and html like that PR would benefit from.

reeganviljoen commented 1 year ago

@vpereira I have started a discussion around how view_component handles formats because their are other issues with similar interests, and hopefully that will help shed some light on this feature

simkim commented 1 year ago

Would be helpfull to have an html and turbo_stream format