Introducing props_template: A Jbuilder alternative

Johny Ho

I like Jbuilder. It’s been part of every Rails project every time I run rails new and the first thing I reach for when I want to create JSON responses. It’s as synonymous with JSON as ERB is with HTML.

But I wanted a version of Jbuilder that had layouts, didn’t merge hashes, had faster caches, directly used OJ’s StringWriter instead of building a hash, and allowed me to dig into a structure using a key path. Sometimes it makes sense to contribute to an open source project and submit pull requests for the features you want; sometimes we diverge so much that it makes sense to start anew.

Introducing props_template. A JSON builder with a Jbuilder-like DSL that has support for all of the previously mentioned and more!

It’s fast.

Benchmark of props_template, Jbuilder, rabl, ams, fast_json, turbostreamer

And doesn’t compromise on the DSL by staying as close as possible to Jbuilder.

# index.json.props

json.menu do
  json.current_user do
    json.email current_user.email
    json.avatar current_user.avatar
    json.inbox current_user.messages.count
  end
end

json.posts do
  json.array! paged_posts do |post|
    json.id post.id
    json.description post.description
    json.comments_count post.comments.count
    json.edit_path edit_post_path(post)
  end
end

json.footer partial: 'shared/footer' do
end

props_template was built to generate JSON for superglue, a framework that makes React and Redux as productive as Hotwire, Turbo and Stimulus. Give that a try too!