Skip to content

Instantly share code, notes, and snippets.

@kaspth
Created December 29, 2022 15:03
Show Gist options
  • Save kaspth/00a0a0e11e9b9d73f4d4eceafca20652 to your computer and use it in GitHub Desktop.
Save kaspth/00a0a0e11e9b9d73f4d4eceafca20652 to your computer and use it in GitHub Desktop.
Very condensed write up of Action View's rendering
# app/views/cards/_card.html.erb
# <%= greeting %>
#
# ^ gets compiled onto ActionView::Base as a Ruby method, when called like this:
#
# render "cards/card", greeting: "Hello"
#
# Here's roughly what the compiled source looks like:
class ActionView::Base
def render_card(local_assigns, output_buffer) # Not the actual method name, and the arguments change if using strict locals
# For some reason, the output_buffer argument isn't used, I believe it's from a refactoring at one point
greeting = local_assigns[:greeting] # Any passed locals are extracted from local_assigns as a local variable
@output_buffer.safe_append=(greeting.to_s) # <%= greeting %>
end
end
# Note: you can also check out ActionView::Template#compile and also search for compiled_method_container, which here, is ActionView::Base iirc.
# So the `render` call from above is actually about locating the correct compiled method name.
# And then calling `_run` on that, so:
<%= _run :render_card, greeting: "Hello" %>
# Ref: https://github.com/rails/rails/blob/663e8e1ccd049dc005924f7033ae407d2d25ba8a/actionview/lib/action_view/base.rb#L243
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment