Different shaving styles. Mustache, ERB and the future of templating by Kyle Neath

There are days that I feel like I’ve spent the better part of my life inside templating languages. I’ve rocked hundreds of different styles of templates over the past 8 years. Smarty, vBulletin templates, generations of Wordpress, J2EE, XSLT, CFML, ERB, HAML, Mustache and every form of in-house bastardized template language that exists. Different mustache names.

So when I say that {{ mustache }} is my favorite templating language I’ve ever worked with, I mean it with a great deal of sincerity and experience. It’s syntactically elegant, focused on output (HTML), encourages documentation, and discourages unmaintainable magic. I want to use it everywhere.

I mustache you, why mustache?

Mustache is more than a syntax. It’s a different approach to traditional templating — mustache templates have no logic. The template files themselves are HTML and mustaches:

You cannot modify variables. You cannot apply filters. You can only output variables or a collection of variables. Everything else happens inside of a view. A view can be written in any language of your choosing: C, Objective-C, Ruby, Python, Javascript, etc. I’ll use Ruby since that’s what we use:

It’s just a good old Ruby class. Oh the wonder you can do with a class! Include modules, extend classes (such as that Layout class), and define any method you so desire. With any documentation your heart desires (something that’s missing from every templating strategy I’ve ever used).

I thought I loved Mustache a year ago, but over time I’ve learned just how revolutionary separating templates from views is toward maintainability and collaboration. Anyone who knows HTML can edit Mustache templates. And all the magic that happens on the whole V side of MVC can be fully documented and separated into re-usable Ruby classes and modules.

Mustache and beard

You want me to switch templating languages on my legacy app?

For all this talk, the application I spend most of my time working on is still ERB. In fact, the rails app that powers GitHub has over 500 erb templates. We have dozens of people throwing hundreds of commits a day at the codebase in over 150 branches. Switching to Mustache would be a disaster requiring everyone to stop development, switch patterns, and introduce an unknown number of bugs to our customers. A shitty trade.

I don’t want to stop new feature development, but I do want better templates. And I know that Mustache is the direction I’d like to go. Luckily for me, I work with the smartest people in the world. A little while ago Simon introduced a new templating strategy that I really like.

Mustache-style ERB templates

We’ve started using the mustache style but completely within ERB — we haven’t modified the template rendering chain at all. Inside of a new helper, we’ll create a view class:

module NavigationHelper class RepositoryNavigationView include ActionView:: Helpers:: NumberHelper attr_reader:current_user,:current_repository def initialize ( user, repo ) @current_user = user @current_repository = repo end # What symbols should we trigger highlighting for various tabs? # # Returns an array of symbols. def highlights_for_code [:repo_source,:repo_downloads,:repo_commits,:repo_tags,:repo_branches ] end def highlights_for_pulls [:repo_pulls ] end def highlights_for_issues [:repo_issues ] end # Should we show the wiki tab? We try and show it when it's useful to # someone using these rules: # # - Never show it if the wiki is disabled under the admin section. # - Show it if you have admin access to the repository # - Show it if there is content # # Returns true to show the tab. def show_wiki? return false unless current_repository. has_wiki? return true if logged_in? && current_repository. pushable_by? ( current_user ) current_repository. wiki. page_count > 0 end end end

Nicely documented, isolated from application-wide helpers and easy to find. Inside of the

Shaping your mustache

, create a new instance of this view object:

If you’re used to regular ERB templates it’s immediately obvious where this data comes from — it’s right at the top of the file! Ack the project for


and you’ve found your view class. No magic.

One huge advantage of this tactic is that you can still use all the same Rails/ERB shortcuts for quick prototyping. If someone doesn’t want to learn the new template strategy right away, they can use the same methods they’ve been using for years.

How to grow and style a moustache

Graceful upgrade path

Switching templating languages is something that needs to be done gracefully when you’re working with others. Ripping out everyone’s foundation is a recipe for unhappy developers. Rails is all about patterns, and sticking to those patterns is really important.

This strategy allows us to slowly convert the codebase to a better documented, view/template separation that anyone who’s worked with ERB can understand. And if we choose to switch to true-and-blue Mustache some day, our code will be 80% there already.

Overall rating page: 4.3 / 5 left 151 people.

Posted by at 07:10PM

Tags: different shaving styles, beard styles for men, kinds of mustaches, face mustache, how to grow a big mustache, classic mustache, old school mustache, mustache and beard, how to grow and style a moustache, shaping your mustache


There are no comments for this post "Mustache, ERB and the future of templating by Kyle Neath". Be the first to comment...

Add Comment