Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is {{.}} in mustache?

Tags:

mustache

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?

like image 403
J.Miller Avatar asked Aug 15 '14 01:08

J.Miller


People also ask

What is Mustache code?

Mustache Template Support. Mustache is a logic-less templating system. It permits you to use pre-written text files with placeholders that will be replaced at run-time with values particular to a given request.

What is Mustache template engine?

Mustache is a simple web template system. It is available for many programming languages including JavaScript and Java. Mustache is described as a logic-less template engine because it does not have any explicit control flow statements, such as if and else conditionals or for loops.

What is Mustache JS used for?

Mustache is an open source logic-less template engine developed for languages such as JavaScript, Ruby, Python, PHP, Java and many more. It's a very lightweight, readable syntax with a comprehensive specification. Mustache can be used for HTML, config files, and source code.

What is Mustache framework?

Mustache is a web template system with implementations available for ActionScript, C++, Clojure, CoffeeScript, ColdFusion, Common Lisp, Crystal, D, Dart, Delphi, Elixir, Erlang, Fantom, Go, Haskell, Io, Java, JavaScript, Julia, Lua, .


1 Answers

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

like image 153
bobthecow Avatar answered Oct 24 '22 11:10

bobthecow