Support Mustache and Derivatives #275

Closed
sorin-ionescu opened this Issue Feb 17, 2012 · 12 comments

Projects

None yet

4 participants

I like Mustache and its improved cousin Handlebars. New template engines are easy to add to Tilt. I've done it before since its author yanked Mustache out for philosophical reasons. But, how easy is it to integrate Mustache-based template languages with Middleman since templates are separate from views? How do you handle scope/locals and access to YAML front matter?

Owner
tdreyno commented Feb 17, 2012

Anything Tilt supports, we support. So any gem or code which adds an engine to Tilt will automatically work and be passed both instance variables (from using the page block) and frontmatter (which is a helper method).

That said, there are templating languages which are antithetical to dynamic data (like Mustache). We've added specific code to work with Liquid, which has similar issues.

I see no reason a middleman extension couldn't provide both the Tilt bridge for Handlebars and deal with any peculiarities with passing data into it's sandbox.

Instead of hard coding Liquid support, perhaps more generic code can be added that will work with any unorthodox template engine?

Owner
tdreyno commented Mar 4, 2012

We have a generic means of passing data to Tilt. Here is how we use it for Liquid:

      provides_metadata %r{\.liquid$} do |path|
        { :locals => { :data => data.to_h } }
      end

You can hook in and "provide metadata" for any template (or all). This hash is passed directly to Tilt, containing locals and options.

@tdreyno tdreyno closed this Mar 4, 2012
Contributor

Here's a basic example of Mustache in Middleman, using the tilt-mustache gem:

https://gist.github.com/2026206

Owner
tdreyno commented Mar 13, 2012

Brilliant. If you open a pull request, I'll put that in middleman-more

I've had some trouble with tilt-mustache in the past. I tend role my own Tilt integration. Let's not depend on yet another gem. Does that gist have support for {{{yield}}}?

Contributor

That's a good point - it's probably overkill to require the gem.

I tried yield yesterday and it worked. If you're asking about {{{yield}}} vs {{yield}} I didn't test that.

Owner
tdreyno commented Mar 13, 2012

I wasn't intending to include the gem, but it's a nice little compatibility layer for those who install it themselves. Basically just making data/frontmatter work.

jdberry commented Apr 13, 2012

So what's the current state of middleman support for mustache?

Owner
tdreyno commented Apr 13, 2012

The current state is: The creator doesn't think Mustache should work in the way people want it to work (with Tilt), so we don't support it... but we do support Tilt.

Therefore, any gem which gets Mustache working with Tilt should add support to Middleman automatically. The gist above extends that basic support to let Mustache access MM data/frontmatter.

I don't need this feature, but if someone packages it up as pull request to middleman-more then I will accept it.

Contributor

I never did get around to submitting a patch... The code from the gist will allow for a minimal mustache usage though, despite my laziness!

Contributor

Specifically, the gist gives a naive approach in which Middleman data variables are fed as-is into Mustache.

Whereas really, Mustache templates are intended to be fed Mustache view objects.

So, my approach is a hack, a crummy solution - but if you can handle a few crumbs in your Mustache, then it may be "good enough" :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment