-
Notifications
You must be signed in to change notification settings - Fork 426
ANCHORED DOT pragma
The {{% ANCHORED-DOT }}
pragma enables anchoring tags to the top of the context stack. Just like {{ . }}
resolves to the top of the context, {{ .name }}
resolves to a property or method called name
on the value at the top of the context stack.
If your rendering context looks like this:
[
'title' => 'Mustache links',
'links' => [
['href' => 'https://mustache.github.io'],
['href' => 'https://github.com/mustache/spec', 'title' => 'The Mustache spec'],
['href' => 'https://github.com/bobthecow/mustache.php', 'title' => 'Mustache.php'],
]
]
… and you wanted to render the list of links, you'd use a template like this:
<h1>{{ title }}</h1>
<ul>
{{# links }}
<li>
<a href="{{ href }}">{{ title }}{{^ title }}{{ href }}{{/ title }}</a>
</li>
{{/ links }}
</ul>
But this wouldn't do what you'd expect. Rendering {{ href }}
as a fallback for a missing title would never work, because Mustache would look up the context stack until it found a suitable title
property to use. So our example would render as:
<h1>Mustache links</h1>
<ul>
<li>
<a href="https://mustache.github.io">Mustache links</a>
</li>
<li>
<a href="https://github.com/mustache/spec">The Mustache spec</a>
</li>
<li>
<a href="https://github.com/bobthecow/mustache.php">Mustache.php</a>
</li>
</ul>
Without the {{% ANCHORED-DOT }}
pragma, each link in the data we pass to our template has to have an empty title
value to prevent a context stack lookup.
This template, however, renders exactly as you'd expect:
{{% ANCHORED-DOT }}
<h1>{{ title }}</h1>
<ul>
{{# links }}
<li>
<a href="{{ href }}">{{ .title }}{{^ .title }}{{ href }}{{/ .title }}</a>
</li>
{{/ links }}
</ul>
And the output:
<h1>Mustache links</h1>
<ul>
<li>
<a href="https://mustache.github.io">https://mustache.github.io</a>
</li>
<li>
<a href="https://github.com/mustache/spec">The Mustache spec</a>
</li>
<li>
<a href="https://github.com/bobthecow/mustache.php">Mustache.php</a>
</li>
</ul>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.