Embed ERB files inside another ERB files for faster performance (5-20% BOOST!!!)
MIT License
Improve application performance by 5-20% (depending on your logic, views and layouts) without major code changes.
Rails have slow rendering, expecially when you have view and many partials included inside.
This gem simply embeds views into another views. Works similar to partials, but instead of rendering main view, partials - it's creates a bigger view with content from partials and renders all together.
So if you have a pretty stable app, rarely changed ERB views(_header, _footer, _modal, _ga, _sharethis, _etc...) you can embed them in main view/layout and render together.
Such small tweak could give 5-20% boost for performance.
Currently works only with ERB templates.
Instead of:
<%= render '/shared/header' %>
just write:
<%= embed_view "../shared/_header.html.erb" %>
Please note that this is not a typical helper. This is just a meta tag which will be replaced with content from file.
There are no familiar "locals", "collection", etc things from "partial".
During development just follow the rule that content from embedded file is inside this file. So all variables available before embed_view
will be available in inserted file.
Path to file must be relative to file where it's embeds.
Note: Only this is correct syntax <%= embed_view "../shared/_header.html.erb" %>
. Specify relative path to file with real filename.
Add this line to your application's Gemfile:
gem 'embed_view'
And then execute:
$ bundle
Or install it yourself as:
$ gem install embed_view
https://boiling-falls-90100.herokuapp.com/ - simple app without this gem, typical to many apps (code https://github.com/BogdanBusko/erb_test)
https://erbtest2.herokuapp.com/ - the same app but with this gem (code https://github.com/BogdanBusko/erb_test/tree/add-embed-view)
Another sample of rendering from dummy app: https://github.com/igorkasyanchuk/embed_view/tree/master/test/dummy/app/views
Processing by UsersController#show as HTML
Parameters: {"id"=>"6"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 6], ["LIMIT", 1]]
↳ app/controllers/users_controller.rb:51
embed_view: ../shared/_header.html.erb
embed_view: ../shared/_footer.html.erb
embed_view: _ga.html.erb
Rendering users/show.html.erb within layouts/application
Rendered users/show.html.erb within layouts/application (0.9ms)
(0.1ms) SELECT COUNT(*) FROM "users"
↳ app/views/layouts/application.html.erb:20
User Load (0.2ms) SELECT "users".* FROM "users" ORDER BY random() LIMIT ? [["LIMIT", 5]]
↳ app/views/layouts/application.html.erb:428
Completed 200 OK in 38ms (Views: 34.9ms | ActiveRecord: 0.5ms)
Before
<tbody>
<% @users.each do |user| %>
<%= render '/users/user', user: user %>
<% end %>
</tbody>
After (see that no variables are passed, because "user" variable is already defined)
<tbody>
<% @users.each do |user| %>
<%= embed_view "_user.html.erb" %>
<% end %>
</tbody>
wrk -t2 -c2 -d10s http://localhost:3000
You are welcome to contribute.
Contributors:
The gem is available as open source under the terms of the MIT License.