Support integrating with Action View layouts - #623
Conversation
b185a9e to
8ba70b4
Compare
Related to rails#250 Related to rails#172 Add support for Rails' built-in layout templates defined in `app/views/layouts/`. Layouts can be used to extract properties or structures that are shared across responses. For example, a `PostsController` that inherits from `ApplicationController` will render its templates declared in `app/views/posts/` *into* the layout declared in `app/views/layouts/application.json.jbuilder`: # app/views/layouts/application.json.jbuilder json.merge! yield json.servedAt Time.current # app/views/posts/index.json.jbuilder json.partial! 'posts/post', collection: @posts, as: :post
8ba70b4 to
4854fa8
Compare
|
@seanpdoyle I maintain TurboStreamer it might be a better fit for you. It's a Jbuilder-ish DSL over streaming encoders (Oj/Yajl), and supports layouts and improved caching performance. A layout calls # app/views/layouts/application.json.streamer
json.object! do
json.meta do
json.object! { json.version 1 }
end
json.key! :data
json.yield!
end
# app/views/posts/index.json.streamer
json.array! @posts, :id, :title
# => { "meta": { "version": 1 }, "data": [ { "id": 1, "title": "..." } ] }The layout and the template share one builder, so the template writes straight into the same stream at the point it's yielded — nothing is encoded to a string and re-parsed, which also means the template's JSON can land at a key or inside an array rather than only merging into the root. This also enables streaming support. More in the README. Happy to answer questions if you give it a look. |
|
@malomalo That seems like a compelling project, thanks for the recommendation! I'm sure that there's plenty of innovation out there worth trying. The motivation behind this PR is less about innovating, and more about bringing
That's exciting! Are there any opportunities to upstream performance improvements back into |
@seanpdoyle I tried for a while with my fork to get jBuilder off hashes at least partially but if i recall correctly it's impossible due to syntax and needing to know how to insert the string. I forget the concrete example convinced me it wasn't possible. I just remember your coworkers did something similar recently - but was just looking to see how they resolved it but looking at the code now it was based of my work so not sure if they have the answers 🤣🙈. Just read the readme and recognized the performance graph, then I see Wankel in the Gemfile history and your analyzer is a fork of mine - Would have taken PRs but I guess copy pasta is the sincerest form of flattery in open source haha. Looks like OJ got some performance improvements for this use case that I should bring in to turbostreamer that props brought in and add props to comparisons. |
Related to #250
Related to #172
Add support for Rails' built-in layout templates defined in
app/views/layouts/. Layouts can be used to extract properties or structures that are shared across responses.For example, a
PostsControllerthat inherits fromApplicationControllerwill render its templates declared inapp/views/posts/into the layout declared inapp/views/layouts/application.json.jbuilder: