Skip to content

Latest commit

 

History

History
102 lines (80 loc) · 4.5 KB

amp-mustache.md

File metadata and controls

102 lines (80 loc) · 4.5 KB

amp-mustache

Description Allows rendering of Mustache.js.
Availability Stable
Examples None

Syntax

Mustache is a logic-less template syntax. See Mustache.js docs for more details. Some of the core Mustache tags are:

  • {{variable}} - variable tag. It outputs the the HTML-escaped value of a variable;
  • {{#section}}{{/section}} - section tag. It can test existance of a variable and iterate over it if it's an array;
  • {{^section}}{{/section}} - inverted tag. It can test non-existance of a variable.

Usage

The amp-mustache template has to be defined and used according to the AMP Template Spec.

First, the amp-mustache has to be declared/loaded like this:

<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.1.js"></script>

Then, the Mustache templates can be defined in the template tags like this:

<template type="amp-mustache">
  Hello {{world}}!
</template>

How templates are discovered, when they are rendered, how data is provided - all decided by the target AMP element that uses this template to render its content.

Restrictions

Like all AMP templates, amp-mustache templates are required to be well-formed DOM fragments. This means that among other things, you can't use amp-mustache to:

  • Calculate tag name. E.g. <{{tagName}}> is not allowed.
  • Calculate attribute name. E.g. <div {{attrName}}=something> is not allowed.
  • Output arbitrary HTML using {{{unescaped}}}. The output of "triple-mustache" is sanitized to only allow formatting tags such as <b>, <i>, and so on.

Notice also that because the body of the template has to be specified within the template element, it is impossible to specify {{&var}} expressions - they will always be escaped as {{&amp;var}}. The triple-mustache {{{var}}} has to be used for these cases.

Validation errors

The following lists validation errors specific to amp-mustache (see also template in the AMP validator specification):

Validation Error Description
Mustache template syntax in attribute name 'example1' in tag 'example2'. Mustache templates must be within the amp-mustache tag. Error thrown when templating syntax found in an attribute in another tag.
The attribute 'example1' in tag 'example2' is set to 'example3', which contains unescaped Mustache template syntax. Mustache templates must be within the amp-mustache tag. Error thrown when unescaped templating syntax found in an attribute in another tag.
The attribute 'example1' in tag 'example2' is set to 'example3', which contains a Mustache template partial. Mustache templates must be within the amp-mustache tag. Error thrown when template partial found in an attribute in another tag.