AMP

amp-mustache

版本注意事項

版本 說明
0.2 支援 SVG 元素並縮減套件大小 (12.2KB vs. 20.5KB,已 gzip 壓縮)。

遷移至更現代的 HTML 清理程式庫 (Caja 變更為 DOMPurify)。由於標記和屬性允許清單的差異,這可能會導致輕微的重大變更。我們建議您先測試網頁,再推送至正式環境,以確保產生的標記變更不會影響功能。
0.1 初始實作。

語法

Mustache 是一種無邏輯範本語法。詳情請參閱 Mustache 規格。部分核心 Mustache 標記如下:

  • {{variable}}:變數標記。輸出變數的 HTML 跳脫字元值。
  • {{#section}} {{/section}}:區段標記。可以測試變數是否存在,並在變數是陣列時疊代變數。
  • {{^section}} {{/section}}:反向標記。可以測試變數是否不存在。
  • {{{unescaped}}}:未跳脫字元的 HTML。輸出標記有所限制 (請參閱下方的「限制」)。

用法

amp-mustache 範本必須根據 AMP 範本規格定義及使用。

首先,必須宣告/載入 amp-mustache,如下所示:

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

接著,Mustache 範本可以定義在 scripttemplate 標記中,如下所示:

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

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

請盡可能使用 template 標記,因為 AMP 驗證功能會提供實用的開發體驗提示。針對表格環境中範本的特殊情況和問題,請使用 script 範本。請參閱下方「表格」一節。

範本的探索方式、轉譯時間,以及資料的提供方式,都由目標 AMP 元素決定,該元素會使用此範本轉譯其內容 (例如在 amp-listamp-form 等)。

限制

驗證

與所有 AMP 範本一樣,amp-mustache 範本必須是格式正確的 DOM 片段。這表示您無法使用 amp-mustache 執行下列操作:

  • 計算標記名稱。例如,不允許使用 <{{tagName}}>
  • 計算屬性名稱。例如,不允許使用 <div {{attrName}}=something>

「三花括號」的輸出會經過清理,僅允許下列標記:

aamp-imgarticleasidebblockquotebrcaptioncodecolcolgroupdddeldetailsdivdldtemfigcaptionfigurefooterh1h2h3headerhriinslimainmarknavolppreqssectionsmallspanstrongsubsummarysuptabletbodytdtfootththeadtimetruul

清理

Mustache 輸出已清理,以確保安全性並維持 AMP 有效性。這可能會導致某些元素和屬性遭到靜音移除。

常見錯誤

巢狀範本

根據 AMP 驗證,<template> 元素不得為其他 <template> 元素的子項。當巢狀使用兩個使用範本的元件時,可能會發生這種情況,例如 amp-listamp-form

為了解決這個問題,<template> 元素也可以透過元件上的 template 屬性,依 id 參照。

<amp-list id="myList" src="https://foo.com/list.json">
  <template type="amp-mustache">
    <div>{{title}}</div>
  </template>
</amp-list>

也可以表示為

<!-- Externalize templates to avoid nesting. -->
<template type="amp-mustache" id="myTemplate">
  <div>{{title}}</div>
</template>

<amp-list id="myList" src="https://foo.com/list.json" template="myTemplate">
</amp-list>

表格

由於 AMP 範本字串必須在 <template> 元素中指定,因此可能會因瀏覽器剖析而導致非預期的行為。例如,<table> 元素可能會導致文字的寄養父代。在下列範例中:

<template type="amp-mustache">
  <table>
    <tr>
{{#foo}}      <td></td>
{{/foo}}    </tr>
  </table>
</template>

瀏覽器會寄養父代文字節點 {{#foo}}{{/foo}}

{{#foo}} {{/foo}}<table>
  <tr>
    <td></td>
  </tr>
</table>

解決方法包括將 Mustache 區段包裝在 HTML 註解中 (例如 <!-- {{#bar}} -->)、使用非表格元素 (例如 <div>),或使用 <script type="text/plain"> 標記來定義範本。

<script type="text/plain" template="amp-mustache">
  <table>
    <tr>
{{#foo}}<td></td>{{/foo}}
    </tr>
  </table>
</script>

跳脫引號

使用 amp-mustache 計算屬性值時,跳脫引號可能會造成問題。例如:

<template type="amp-mustache">
  <!-- A double-quote (") in foo will cause malformed HTML. -->
  <amp-img alt="{{foo}}" src="example.jpg" width="100" height="100"></amp-img>

  <!-- A single-quote (') or double-quote (") in bar will cause an AMP runtime parse error. -->
  <button on="tap:AMP.setState({foo: '{{bar}}'})">Click me</button>
</template>

{{foo}}{{bar}} 變數中使用 HTML 字元代碼將無法運作,因為 Mustache 會 HTML 跳脫 & 字元 (例如 &quot; -> &amp;quot;)。一種解決方法是使用類似字元,例如 ′ (&prime;) 和 ″ (&Prime;)。

HTML 實體

HTML 實體不會保留在 <template> 元素中。

如果您想要伺服器端轉譯包含使用者產生文字的 <template>,這可能會造成問題,因為包含 {{}}{{{}}} 的使用者產生文字會被視為 Mustache 區段。例如,將 {{ 替換為 HTML 實體 &lcub;&lcub; 無法運作,因為瀏覽器剖析 <template> 時不會保留這些實體。

解決方法包括將 {{ 等字串替換為不同的字元,或直接從使用者產生的內容中移除這些字串。

驗證

請參閱 AMP 驗證器規格中的 amp-mustache 規則

需要更多協助嗎?

您已將這份文件讀過十幾遍,但還是沒有涵蓋所有問題嗎?也許其他人也有同樣的感受:請在 Stack Overflow 上與他們聯繫。

前往 Stack Overflow
發現錯誤或缺少功能?

AMP 專案大力鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的長期參與者,但我們也歡迎您針對特別感興趣的問題提供一次性的貢獻。

前往 GitHub