dry-rb / dry-web

Lightweight web application stack with pluggable routing front-ends
http://dry-rb.org
MIT License
212 stars 19 forks source link

Include the “page” scope object in the parts provided to templates #20

Closed timriley closed 8 years ago

timriley commented 8 years ago

This is the last of my 3 little view tweaks today :) And perhaps the most controversial!

I hinted in #18 about how a "content_for"-stye method on the page object could be used to share content up to the layout from within a view's template. We have this in place using this patch on one of our rodakase apps, and it looks a bit like this. Inside a template file:

== page.content_for :head do
  meta name="citation_title" content=article.title
  - contributors.each do |contributor|
    meta name="citation_author" content=contributor.display_name

And inside the layout:

== page.content_for :head

As you can probably guess, this depends on both #18 and this PR here, which exposes page to all templates as well as layout (otherwise there's no common object to store that "content_for" data).

As well as this, having page accessible everywhere allows templates to access common, site-wide functionality, like, for example, having access to an page.assets object of some kind, which would allows templates to get the URLs of assets, whether loaded on-the-fly in development, or precompiled in production.

I know we've had some chats about exploring alternative ways to doing this, and I'd be happy to talk further and to spearhead some other explorations, but in the meantime, I think this definitely increases the utility of the Rodakase views (and I'd be happy if we worked with this page object knowing it will likely go away and be replaced by something different in the future).

timriley commented 8 years ago

Looking over the Rodakase issues, I saw #3 – looks like you already started some thinking there.

timriley commented 8 years ago

We've worked out a nicer way to handle this in our dry-web apps by providing a Page#view_locals method (e.g. https://github.com/icelab/berg/blob/dae63b88662cced52f39a3c1d66af26c2851c0e8/lib/berg/page.rb#L9-L14) for passing a default set of locals to every view, so this change is no longer needed.