This branch implements partials. They work the same way as they do in Rails:
<%# Assume there's a file at app/views/subdir/_partial.html.erb %>
<h1>This comes before the partial.</h1>
<%= render "subdir/partial" %>
<p>This comes after the partial.</p>
The actual rendering of partials is pretty easy. You can see a super dumb implementation in 57d2b269613b4ad71fbecc3864a3cca04db4c37d. Most of the important stuff in this pull is refactoring. I'll outline the two important parts here:
View::Scope
rtomayko/tilt allows a "scope" to be passed to Template#render. The scope is just an object that defines the methods you can use in the template. So, if a template has <%= pluralize("dog") %>, it'll look for a pluralize method in the scope object you passed to Template#render.
This branch adds View::Scope, which includes the View::Scope#render method. This is what lets us do <%= render "subdir/partial" %> in templates.
View::Registry
Previously, views were loaded in Application.new, and looked up in Application#call. Now, Application.new creates a new View::Registry, which handles loading the views (in View::Registry.new) and looking them up (in View::Registry#find_template_view). It also handles looking up partials with View::Registry#find_partial_view, which View::Scope#render (described above) calls.
This branch implements partials. They work the same way as they do in Rails:
The actual rendering of partials is pretty easy. You can see a super dumb implementation in 57d2b269613b4ad71fbecc3864a3cca04db4c37d. Most of the important stuff in this pull is refactoring. I'll outline the two important parts here:
View::Scope
rtomayko/tilt allows a "scope" to be passed to
Template#render
. The scope is just an object that defines the methods you can use in the template. So, if a template has<%= pluralize("dog") %>
, it'll look for apluralize
method in the scope object you passed toTemplate#render
.This branch adds
View::Scope
, which includes theView::Scope#render
method. This is what lets us do<%= render "subdir/partial" %>
in templates.View::Registry
Previously, views were loaded in
Application.new
, and looked up inApplication#call
. Now,Application.new
creates a newView::Registry
, which handles loading the views (inView::Registry.new
) and looking them up (inView::Registry#find_template_view
). It also handles looking up partials withView::Registry#find_partial_view
, whichView::Scope#render
(described above) calls./cc @jessicard