Repository navigation
Support integrating with Action View layouts - #623
seanpdoyle wants to merge 1 commit into
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 they might have and idea. *redacting/editing comment - I jumped to conclusions with a few things in the earlier comment and props-template although similar on first look approaches some things differently |
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: