Symlinked templates


I am exploring a possible feature for an app I am working on. Ideally we would like to be able to symlink templates so we can use them in multiple paths easily, partials are not really a good option for our use case. I have written a custom template resolver built on top of ActionView::PathResolver to accomplish this. I was speaking with eileencodes about this feature and trying to explore the idea of Rails having native support for this, she said I should start a discussion here.

What do people think? I would love to have this natively supported and would be more than happy to work on a PR.


Why aren’t partials a good option?

We are using Rails to serve static templates from a Git repo. We have template writers who are writing Ditta style templates that are single sourced. What this means is that they write a template that may be used in various versions of documentation which will live at different urls, ie /v2/template or /v3/template. The source templates live in a /content folder, to simplify their site structuring we are considering having the site live in a separate folder holding its own directory structure with content templates symlinked so they can be in multiple locations inside the site’s structure. I have written a controller and associated prepend view path rule and route to know how to resolve the correct template from that structure.

While partials could work, from a template writing perspective it adds a fair amount of overhead, also symlinks are well understood and a first class citizen of most file systems.

I also think partials will be problematic because we don’t want the templates themselves to be in the look up path for Rails.

Sounds like some pretty specific requirements for what you need which is a good candidate for an external gem.

I’m thinking that you may use a app helper for that.

Let’s say that you are using erb, you may have something like that:









You can implement a simple helper like that:

<%= shared ‘foo’ %>

And use something like that:

def shared(template, *options)

You may have business rules here to discover the path.

render “shared/#{template}”, *options


You aren’t probably using ERB but you have similar ways to accomplish the same helper feature in almost all of them.

That is not enough?

I’m not against symlink functionality (I’m not even in the core xD), I’m just trying to understand your requirement here.

As I understand it, in order to use a partial like being suggested here, the file has to be in the look up path. This would mean that even under this approach templates would resolve in ways that I do not intend, since render uses the view path to find its template candidates.

I have built something similar to this, but I have to handle a lot of what Rails handles out of the box. :frowning:

Yeah it is rather specific, although it is fairly trivial to implement. There is a file bin read in the template renderer, basically all that needs to happen is that you check if it is a symlink before that and resolve the symlink before reading the file. My reason for bringing it to the core group is that symlinks are a pretty standard feature of the file system so I sort of expected them to just be followed.