jakeboxer / hyperloop

Make simple websites with a technology stack familiar to Rails programmers.
MIT License
72 stars 4 forks source link

Get partials working #7

Closed jakeboxer closed 10 years ago

jakeboxer commented 10 years ago

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.

/cc @jessicard