Permalink
Browse files

Added `mustache` script for rendering templates on the command line.

  • Loading branch information...
1 parent 6f04090 commit 4891479045e7a2abfe2f2254fa85de282277e087 @defunkt defunkt committed Nov 23, 2009
Showing with 81 additions and 3 deletions.
  1. +2 −1 HISTORY.md
  2. +45 −2 README.md
  3. +34 −0 bin/mustache
View
@@ -1,5 +1,6 @@
-## 0.4.3 (2009-??-??)
+## 0.5.0 (2009-??-??)
+* Added `mustache` script for rendering templates on the command line.
* ctemplate compat: Partials are indicated by >, not <
* Bugfix: Context miss should return nil, not empty string. Fixes 1.9.x
View
@@ -376,7 +376,7 @@ An example Sinatra application is also provided:
[Rack::Bug][4]
----------
+--------------
Mustache also ships with a `Rack::Bug` panel. In your `config.ru` add
the following code:
@@ -410,10 +410,53 @@ more lenient tag values and includes a few commands for your editing pleasure.
TextMate
--------
-Check out Tekkub's
+Check out Tekkub's
[Mustache.tmbundle](http://github.com/tekkub/Mustache.tmbundle).
+Command Line
+------------
+
+Mustache includes a `mustache` script for rendering templates on the
+command line. This can be useful when designing HTML that will
+eventually be included in a website: instead of having to format the
+HTML as Mustache later, you can do it now!
+
+The script expects a Mustache template on STDIN with YAML
+frontmatter. An example looks like this:
+
+ $ cat complete.mustache
+ ---
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
+ ---
+ {{#names}}
+ Hi {{name}}!
+ {{/names}}
+
+ $ cat complete.mustache | mustache
+ Hi chris!
+ Hi mark!
+ Hi scott!
+
+It's probably more useful to keep the YAML and HTML in separate files,
+though. Luckily `cat` works great for this, too:
+
+ $ cat data.yml
+ ---
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
+ ---
+
+ $ cat template.mustache
+ {{#names}}
+ Hi {{name}}!
+ {{/names}}
+
+ $ cat data.yml template.mustache | ruby -I lib bin/mustache
+ Hi chris!
+ Hi mark!
+ Hi scott!
+
+
Installation
------------
View
@@ -0,0 +1,34 @@
+#!/usr/bin/env ruby
+
+require 'mustache'
+require 'yaml'
+
+if STDIN.stat.size > 0
+ doc = STDIN.read
+ if doc =~ /^(\s*---(.*)---\s*)/m
+ data = YAML.load($2.strip)
+ puts Mustache.render(doc.sub($1, ''), data)
+ else
+ puts doc
+ end
+else
+ puts <<-usage
+Usage: cat data.yml template.mustache | mustache
+
+Expects a single Mustache template on STDIN complete with YAML
+frontmatter.
+
+Runs template.mustache through Mustache, using the data in data.yml to
+replace sections and variables. Useful when developing templates
+before hooking them into your website or whatnot.
+
+The data.yml file should start with --- on a single line and end with
+--- on a single line, e.g.
+
+ ---
+ names: [ {name: chris}, {name: mark}, {name: scott} ]
+ ---
+
+The converted document will be printed on STDOUT.
+usage
+end

1 comment on commit 4891479

bry4n commented on 4891479 Nov 23, 2009

the command line of 'mustache' is a very nice feature :)

Please sign in to comment.