What is {{.}} in mustache?

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

Leave a Comment

tech