Skip to content

Instantly share code, notes, and snippets.

@razhangwei
Created October 21, 2024 18:36
Show Gist options
  • Save razhangwei/e00a54fe86326c809b50780ef0ac728f to your computer and use it in GitHub Desktop.
Save razhangwei/e00a54fe86326c809b50780ef0ac728f to your computer and use it in GitHub Desktop.
prompt template

Here's a concise cheatsheet for Mustache templating:

Basic Syntax

  • Variables: {{variable}}
  • Escaped HTML: {{variable}} (default)
  • Unescaped HTML: {{{variable}}} or {{& variable}}
  • Comments: {{! comment }}

Sections

  • Standard section: {{#section}}...{{/section}}
  • Inverted section: {{^section}}...{{/section}}

Partials

  • Include partial: {{> partial_name}}

Conditionals

{{#condition}}
  Shown if condition is truthy
{{/condition}}

{{^condition}}
  Shown if condition is falsy
{{/condition}}

Loops

{{#array}}
  {{.}} <!-- Current item -->
  {{name}} <!-- Property of current item -->
{{/array}}

Functions/Lambdas

{{#uppercase}}
  text to be uppercased
{{/uppercase}}

Changing Delimiters

{{=<% %>=}} changes delimiters to <% %>

Dot Notation

Access nested properties: {{person.name}}

Special Variables

  • {{.}}: Current context
  • {{this}}: Current object in a list

Parent Context Access

{{../parentProperty}}

Whitespace Control

  • {{~variable}}: Remove whitespace before
  • {{variable~}}: Remove whitespace after

Best Practices

  1. Keep logic in the view model, not in templates
  2. Use partials for reusable components
  3. Leverage sections for conditional rendering and loops
  4. Use inverted sections for fallback content
  5. Escape HTML by default, use triple mustaches only when necessary

Remember, Mustache is "logic-less," so complex operations should be handled in your data preparation before rendering the template.

Mustache

  • Chevron: a python implementation of the Mustche template language.

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