ruby - Parsing process for views in rails -


what parsing process views in rails? partially interested in parsing order raw html vs ruby code in erb tags within views.

i think order view code parsed , sent requester:

  • a controller calls view
  • the view code gets parsed top bottom
    • when rails encounters erb tag during parsing process: rails resolves , appends result parsed html (this includes erb tags referencing helpers)
  • once entire view parsed overall result sent requester

this appears not case. appears view code scan erb snippets , parses first (including references helpers). after taken care of: rails parses top bottom view code , sends result requester.

take view example:

# _form.html.erb <p> hello world </p> <p> foobar </p> <% if something_is_true %>   <%= some_helper_method_that_returns_html %> <% end %> 

is correct order in how rails figures out views , sends result requester?

  • rails scans view called on controller: view partials view references, see if there erb snippets
    • for erb snippets resolved/transformed html , appended html view
  • rails parses top bottom view (which @ point view aggregate of plus referenced partials, , html erb
  • after view parsed: rails sends result requester

follow question: there order erb tags resolved? example: perhaps erb tag references helper resolved first before erb tag iterates through collection? or: rails resolve erb tags top bottom?

you right think view code read top bottom , processed in way. there problem approach. erb template engine/library, , template engine fast has work intelligently.

the process of rendering action goes this:

  • request passed controller
  • before , around filters ran
  • specified action runs , rails know file render
  • erb code picked erb file ignoring html rails not know it
  • erb code processed in orderly fashion. ex:

    <%= render 'my_header' %> html <%= render 'my_footer' %> 

    in scenario file _my_header.html.erb processed , injected first , _my_footer.html.erb processed , injected. erb tags resolved top bottom.

  • this whole file injected layout file in place of <%= yield %>
  • after filters ran , around filters closed
  • the data sent client

i hope clears questions erb library.


Comments