Skip to content

Commit

Permalink
Added mustache script for rendering templates on the command line.
Browse files Browse the repository at this point in the history
  • Loading branch information
defunkt committed Nov 23, 2009
1 parent 6f04090 commit 4891479
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
3 changes: 2 additions & 1 deletion HISTORY.md
@@ -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

Expand Down
47 changes: 45 additions & 2 deletions README.md
Expand Up @@ -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:
Expand Down Expand Up @@ -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
------------

Expand Down
34 changes: 34 additions & 0 deletions bin/mustache
@@ -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

0 comments on commit 4891479

Please sign in to comment.