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?
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.
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.
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.
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, .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With