github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

defunkt / mustache

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 483
    • 26
  • Source
  • Commits
  • Network (26)
  • Issues (6)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Switch Branches (3)
    • gh-pages
    • master ✓
    • pragmas
  • Switch Tags (17)
    • v0.6.0
    • v0.5.1
    • v0.5.0
    • v0.4.2
    • v0.4.1
    • v0.4.0
    • v0.3.2
    • v0.3.1
    • v0.3.0
    • v0.2.2
    • v0.2.1
    • v0.2.0
    • v0.1.4
    • v0.1.3
    • v0.1.2
    • v0.1.1
    • v0.1.0
  • Branch List
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Logic-less Ruby templates. — Read more

  Cancel

http://defunkt.github.com/mustache/

  Cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

cleaner Rakefile 
defunkt (author)
Mon Mar 08 00:47:59 -0800 2010
commit  8ce4b498317eaf509f52b96421be0deb7c760025
tree    0b3ce49aad90449a3ca7b969196dc7046b85f45f
parent  ede589fb682b5f689d231e925ee3028ae1585aa4
mustache /
name age
history
message
file .gitignore Mon Oct 05 15:31:51 -0700 2009 docs too [defunkt]
file .kick Sat Oct 10 19:15:44 -0700 2009 kicker support via `rake kicker` http://github... [defunkt]
file CONTRIBUTORS Wed Jan 13 18:45:24 -0800 2010 update contributors [defunkt]
file HISTORY.md Mon Mar 08 00:40:25 -0800 2010 v0.6.0 [defunkt]
file LICENSE Sun Oct 04 10:20:46 -0700 2009 MIT (of course) [defunkt]
file README.md Sat Mar 06 03:11:54 -0800 2010 steal Escaping section from mustache.js [defunkt]
file Rakefile Mon Mar 08 00:49:28 -0800 2010 cleaner Rakefile [defunkt]
directory benchmarks/ Tue Dec 15 14:55:53 -0800 2009 Make the comparison between ERB/Haml/Mustache m... [nex3]
directory bin/ Sat Mar 06 00:00:12 -0800 2010 bin/mustache points you to mustache(1) or the web [defunkt]
directory contrib/ Sat Mar 06 02:11:25 -0800 2010 Emacs: Automatically load tpl-mode for .mustach... [defunkt]
directory examples/ Fri Mar 05 20:38:20 -0800 2010 Add failing nested objects test [defunkt]
directory lib/ Mon Mar 08 00:40:25 -0800 2010 v0.6.0 [defunkt]
directory man/ Sat Mar 06 05:15:35 -0800 2010 break [defunkt]
file mustache.gemspec Mon Mar 08 00:49:28 -0800 2010 cleaner Rakefile [defunkt]
directory test/ Fri Mar 05 21:52:24 -0800 2010 No more TypeErrors [defunkt]
README.md

Mustache

Inspired by ctemplate and et, Mustache is a framework-agnostic way to render logic-free views.

As ctemplates says, "It emphasizes separating logic from presentation: it is impossible to embed application logic in this template language."

For a list of implementations (other than Ruby) and tips, see http://defunkt.github.com/mustache/.

Overview

Think of Mustache as a replacement for your views. Instead of views consisting of ERB or HAML with random helpers and arbitrary logic, your views are broken into two parts: a Ruby class and an HTML template.

We call the Ruby class the "view" and the HTML template the "template."

All your logic, decisions, and code is contained in your view. All your markup is contained in your template. The template does nothing but reference methods in your view.

This strict separation makes it easier to write clean templates, easier to test your views, and more fun to work on your app's front end.

Why?

I like writing Ruby. I like writing HTML. I like writing JavaScript.

I don't like writing ERB, Haml, Liquid, Django Templates, putting Ruby in my HTML, or putting JavaScript in my HTML.

Usage

Quick example:

>> require 'mustache'
=> true
>> Mustache.render("Hello {{planet}}", :planet => "World!")
=> "Hello World!"

We've got an examples folder but here's the canonical one:

class Simple < Mustache
  def name
    "Chris"
  end

  def value
    10_000
  end

  def taxed_value
    value - (value * 0.4)
  end

  def in_ca
    true
  end
end

We simply create a normal Ruby class and define methods. Some methods reference others, some return values, some return only booleans.

Now let's write the template:

Hello {{name}}
You have just won ${{value}}!
{{#in_ca}}
Well, ${{taxed_value}}, after taxes.
{{/in_ca}}

This template references our view methods. To bring it all together, here's the code to render actual HTML;

Simple.render

Which returns the following:

Hello Chris
You have just won $10000!
Well, $6000.0, after taxes.

Simple.

Tag Types

For a language-agnostic overview of Mustache's template syntax, see the mustache(5) manpage or http://defunkt.github.com/mustache/mustache.5.html.

Escaping

Mustache does escape all values when using the standard double Mustache syntax. Characters which will be escaped: & \ " < >. To disable escaping, simply use tripple mustaches like {{{unescaped_variable}}}.

Example: Using {{variable}} inside a template for 5 > 2 will result in 5 &gt; 2, where as the usage of {{{variable}}} will result in 5 > 2.

Dict-Style Views

ctemplate and friends want you to hand a dictionary to the template processor. Mustache supports a similar concept. Feel free to mix the class-based and this more procedural style at your leisure.

Given this template (winner.mustache):

Hello {{name}}
You have just won ${{value}}!

We can fill in the values at will:

view = Winner.new
view[:name] = 'George'
view[:value] = 100
view.render

Which returns:

Hello George
You have just won $100!

We can re-use the same object, too:

view[:name] = 'Tony'
view.render
Hello Tony
You have just won $100!

Templates

A word on templates. By default, a view will try to find its template on disk by searching for an HTML file in the current directory that follows the classic Ruby naming convention.

TemplatePartial => ./template_partial.mustache

You can set the search path using Mustache.template_path. It can be set on a class by class basis:

class Simple < Mustache
  self.template_path = File.dirname(__FILE__)
  ... etc ...
end

Now Simple will look for simple.mustache in the directory it resides in, no matter the cwd.

If you want to just change what template is used you can set Mustache.template_file directly:

Simple.template_file = './blah.mustache'

Mustache also allows you to define the extension it'll use.

Simple.template_extension = 'xml'

Given all other defaults, the above line will cause Mustache to look for './blah.xml'

Feel free to set the template directly:

Simple.template = 'Hi {{person}}!'

Or set a different template for a single instance:

Simple.new.template = 'Hi {{person}}!'

Whatever works.

Views

Mustache supports a bit of magic when it comes to views. If you're authoring a plugin or extension for a web framework (Sinatra, Rails, etc), check out the view_namespace and view_path settings on the Mustache class. They will surely provide needed assistance.

Helpers

What about global helpers? Maybe you have a nifty gravatar function you want to use in all your views? No problem.

This is just Ruby, after all.

module ViewHelpers
  def gravatar(email, size = 30)
    gravatar_id = Digest::MD5.hexdigest(email.to_s.strip.downcase)
    gravatar_for_id(gravatar_id, size)
  end

  def gravatar_for_id(gid, size = 30)
    "#{gravatar_host}/avatar/#{gid}?s=#{size}"
  end

  def gravatar_host
    @ssl ? 'https://secure.gravatar.com' : 'http://www.gravatar.com'
  end
end

Then just include it:

class Simple < Mustache
  include ViewHelpers

  def name
    "Chris"
  end

  def value
    10_000
  end

  def taxed_value
    value - (value * 0.4)
  end

  def in_ca
    true
  end
end

Great, but what about that @ssl ivar in gravatar_host? There are many ways we can go about setting it.

Here's on example which illustrates a key feature of Mustache: you are free to use the initialize method just as you would in any normal class.

class Simple < Mustache
  include ViewHelpers

  def initialize(ssl = false)
    @ssl = ssl
  end

  ... etc ...
end

Now:

Simple.new(request.ssl?).render

Convoluted but you get the idea.

Sinatra

Mustache ships with Sinatra integration. Please see lib/mustache/sinatra.rb or http://defunkt.github.com/mustache/classes/Mustache/Sinatra.html for complete documentation.

An example Sinatra application is also provided: http://github.com/defunkt/mustache-sinatra-example

Rack::Bug

Mustache also ships with a Rack::Bug panel. In your config.ru add the following code:

require 'rack/bug/panels/mustache_panel'
use Rack::Bug::MustachePanel

Using Rails? Add this to your initializer or environment file:

require 'rack/bug/panels/mustache_panel'
config.middleware.use "Rack::Bug::MustachePanel"

Rack::Bug

Vim

Thanks to Juvenn Woo for mustache.vim. It is included under the contrib/ directory.

See http://gist.github.com/323622 for installation instructions.

Emacs

mustache-mode.el is included under the contrib/ directory for any Emacs users. Based on Google's tpl-mode for ctemplates, it adds support for Mustache's more lenient tag values and includes a few commands for your editing pleasure.

See http://gist.github.com/323619 for installation instructions.

TextMate

Mustache.tmbundle

See http://gist.github.com/323624 for installation instructions.

Command Line

See mustache(1) man page or http://defunkt.github.com/mustache/mustache.1.html for command line docs.

Installation

RubyGems

$ gem install mustache

Rip

$ rip install git://github.com/defunkt/mustache.git

Acknowledgements

Thanks to Tom Preston-Werner for showing me ctemplate and Leah Culver for the name "Mustache."

Meta

  • Code: git clone git://github.com/defunkt/mustache.git
  • Home: http://defunkt.github.com/mustache
  • Bugs: http://github.com/defunkt/mustache/issues
  • List: http://groups.google.com/group/mustache-rb
  • Test: http://runcoderun.com/defunkt/mustache
  • Gems: http://rubygems.org/gems/mustache
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server
ZW5kZW5yYWhheXU5QGdtYWlsLmNvbQ==