MUSTACHE(1)

πŸ’… MUSTACHE(1) β€” the Mustache Manual, bestie edition

okay so we're talking about mustache, from the Mustache Manual bestie. Pull up a chair, grab a strawberry πŸ“, and let's get into it.


✨ NAME ✨

mustache β€” the Mustache processor. That's it, that's the name, iconic behavior honestly πŸ‘‘


πŸ¦‹ SYNOPSIS πŸ¦‹

mustache <YAML> <FILE>
mustache --compile <FILE>
mustache --tokens <FILE>

Three whole modes, three whole vibes, pick your fighter πŸ’–


🌸 DESCRIPTION 🌸

Mustache is a logic-less templating system for HTML, config files, anything. Like, anything anything. No if soup, no weird conditionals, just vibes and tags. It's giving minimalism and honestly? It ate.

The mustache command processes a Mustache template preceded by YAML frontmatter from standard input and prints one or more documents to standard output. Standard in, documents out, main character energy all the way through. ✨

YAML frontmatter begins with --- on a single line, followed by YAML, ending with another --- on a single line, e.g.

---
names: [ {name: chris}, {name: mark}, {name: scott} ]
---

If you are unfamiliar with YAML β€” bestie, don't panic β€” it is a superset of JSON. Valid JSON should work fine. You can literally paste your JSON in there and it'll be chill about it. 🫢

After the frontmatter should come any valid Mustache template. See mustache(5) for an overview of Mustache templates. (Yes, there's a whole other manual page, she's famous.)

For example:

{{#names}}
  Hi {{name}}!
{{/names}}

Now let's combine them. The way that two files become one output… it's giving chemistry πŸ§ͺ

$ cat data.yml
---
names: [ {name: chris}, {name: mark}, {name: scott} ]
---

$ cat template.mustache
{{#names}}
  Hi {{name}}!
{{/names}}

$ mustache data.yml template.mustache
Hi chris!
Hi mark!
Hi scott!

Look at that. Three greetings. Not a single loop written by hand. Slay. πŸ’…

If you provide multiple YAML documents (as delimited by ---), your template will be rendered multiple times. Like a mail merge. Fr fr, that's the whole bit β€” one template, many documents, all the output. πŸ“¬

For example:

$ cat data.yml
---
name: chris
---
name: mark
---
name: scott
---

$ cat template.mustache
Hi {{name}}!

$ mustache data.yml template.mustache
Hi chris!
Hi mark!
Hi scott!

Three documents in, three documents out. No cap. πŸ”₯


πŸ’– OPTIONS πŸ’–

By default mustache will try to render a Mustache template using the YAML frontmatter you provide. That's the default behavior, the no-thoughts-just-render option. It can do a few other things, however. Little side quests, if you will. 🧚

  • -c, --compile β€” Print the compiled Ruby version of a given template. This is the code that is actually used when rendering a template into a string. Useful for debugging but only if you are familiar with Mustache's internals. Like, this is the raw sauce, the actual Ruby that runs β€” gorgeous if you know what you're looking at, absolute gibberish if you don't πŸ™ˆ
  • -t, --tokens β€” Print the tokenized form of a given Mustache template. This can be used to understand how Mustache parses a template. The tokens are handed to a generator which compiles them into a Ruby string. Syntax errors and confused tags, therefore, can probably be identified by examining the tokens produced. Basically: when your template is acting up, this is the vibe check that tells you why. πŸ‘€

πŸŽ€ INSTALLATION πŸŽ€

If you have RubyGems installed:

gem install mustache

One line. That's the whole installation. We love an easy win. ⭐


🌈 EXAMPLES 🌈

$ mustache data.yml template.mustache
$ cat data.yml | mustache - template.mustache
$ mustache -c template.mustache
$ cat <<data | ruby mustache - template.mustache
---
name: Bob
age: 30
---
data

Note the - in there β€” that's the "read from stdin" move, and the last one pipes a heredoc straight into ruby mustache. Chef's kiss. πŸ‘‘


Mustache is Copyright (C) 2009 Chris Wanstrath

Original CTemplate by Google

Respect to the OGs, honestly. 🫑


πŸ¦„ SEE ALSO πŸ¦„


and that's the whole page, bestie β€” this one came from DEFUNKT back in November 2016, filed under MUSTACHE(1). Go forth and render something cute. πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈ