Analysis of the viewing process in rails

What is the process of parsing representations in rails? I'm partially interested in the parsing order with raw html vs ruby ​​code in erb tags in views.

I would think that this is the order in which the view code is parsed and finally sent to the requestor:

  • the controller invokes the view
  • View code parsed from top to bottom
    • When the rails meet the erb tag during the parsing process: rails solves it and adds the result to the processed html (this includes erb tags that refer to helpers).
  • After analyzing the whole view, the overall result is sent to the requester

This does not seem to be the case. It looks like the view code scans any erb fragments and analyzes them first (including links to helpers). After that, it will take care: rails then parses all the view code from top to bottom and sends the result to the requestor.

Take this view, for example:

 # _form.html.erb <p> Hello World </p> <p> Foobar </p> <% if something_is_true %> <%= some_helper_method_that_returns_html %> <% end %> 

Is this the correct order in how the rails define the views and send the result to the requester?

  • Rails scans the view invoked by the controller: like any partial views referencing these links to see if there are erb fragments
    • For all erb they are allowed / converted to html and added to the html view
  • Rails then parses the top-down view (which at the moment is a collection of itself plus any reference parts and all the html that was previously erb
  • After the view has been fully analyzed: Rails sends the result to the request

Follow-up question : is there an order in which erb tags erb allowed? For example: is it possible that the erb tag that references helper is allowed before the erb tag that erb through the collection? Or: Rails always just resolves erb tags from top to bottom?

+6
source share
1 answer

You were right in believing that the view code would be read from top to bottom and processed in this way. But there is a problem with this approach. ERB is a template engine / library, and to start a template engine quickly, it should work reasonably.

The process of performing an action is something like this:

  • The request is passed to the controller
  • Before and after filtering filters
  • The specified action is executed, and now Rails knows which file to render
  • ERB code is fetched from an ERB file, ignoring all HTML, because Rails doesn't know what to do with it.
  • ERB code is processed in an orderly manner. For example:

     <%= render 'my_header' %> This is some HTML <%= render 'my_footer' %> 

    In this case, the _my_header.html.erb file will be processed and entered first, and then _my_footer.html.erb will be processed and entered. ERB tags are allowed from top to bottom.

  • This entire file is inserted into the layout file instead of <%= yield %>
  • After starting the filters and closing the filters
  • Data is sent to the client

Hope this clears all your questions about the ERB library.

+2
source

All Articles