What is {{.}} in mustache?

J.Miller picture J.Miller · Aug 15, 2014 · Viewed 11.7k times · Source

I'm starting to look through some mustache templates and I've come across something that I don't understand/haven't been able to find an explanation for.

{{#something}}word-here={{.}}{{/something}}

Can someone help me understand what the {{.}} is doing?

Answer

bobthecow picture bobthecow · Aug 15, 2014

In Mustache, {{.}} is a special tag referring to the value at the top of the context stack. If you're looping through an array, it is the current element. If you're rendering a section with an object as context, it refers to that object.

https://github.com/mustache/spec/blob/master/specs/interpolation.yml#L7-L9

So if your data looks like this:

{
  numbers: [1, 2, 3, 4, 5],
  string: 'Wheee!'
}

… and you've got a template like this:

{{# numbers }}
 * {{ . }}
{{/ numbers }}

… it will render as this:

 * 1
 * 2
 * 3
 * 4
 * 5

If your template looks like this:

{{# string }}{{ . }}{{/ string }}

… it will render as this:

Wheee!

For more on the context stack, see the Mustache.php wiki:

https://github.com/bobthecow/mustache.php/wiki/Variable-Resolution

Edit: I just realized this tag is in the Mustache wiki, under "implicit iterator":

https://github.com/bobthecow/mustache.php/wiki/Mustache-Tags#implicit-iterator