ElB*_*ulP 4 json jekyll mustache amp-html amp-mustache
我试图复制这个例子没有成功.我想使用mustache模板添加列表,如下所示:
<ul>
<amp-list width=auto
height=100
layout=fixed-height
src="/assets/popular.json">
<template type="amp-mustache"
id="amp-template-id">
<li>
<a href={{url}}>{{title}}</a>
</li>
</template>
</amp-list>
</ul>
Run Code Online (Sandbox Code Playgroud)
我的/assets/popular.json档案是:
{
"items": [
{
"title": "amp-carousel",
"url": "https://ampbyexample.com/components/amp-carousel"
},
{
"title": "amp-img",
"url": "https://ampbyexample.com/components/amp-img"
},
{
"title": "amp-ad",
"url": "https://ampbyexample.com/components/amp-ad"
},
{
"title": "amp-accordion",
"url": "https://ampbyexample.com/components/amp-accordion"
}
]
}
Run Code Online (Sandbox Code Playgroud)
但我不能让它工作,json模板中的值没有被替换,我得到这个错误:
Missing URL for attribute 'href' in tag 'a'
Run Code Online (Sandbox Code Playgroud)
我不知道为什么价值{{url}}没有被正确地替换为内容json.
我已经添加了必要scripts的头部.
最近我一直在从杰基尔移居到雨果并且遇到了同样的问题.以下是两者的解决方案.
现在解决了,问题是我正在使用jekyll,所以标签{{tag}}被解释为liquid tag.我解决了它写这样的代码:
<ul>
<amp-list width=auto
height=100
layout=fixed-height
src="/assets/popular.json">
<template type="amp-mustache"
id="amp-template-id">
<li>
<a href="{% raw %}{{url}}{% endraw %}">{% raw %}{{title}}{% endraw %}</a>
</li>
</template>
</amp-list>
</ul>
Run Code Online (Sandbox Code Playgroud)
更新:我已经写了更详细的解释
<ul>
<amp-list width=auto
height=100
layout=fixed-height
src="/assets/popular.json">
<template type="amp-mustache"
id="amp-template-id">
<li>
<a class="card related" id={{"{{id}}"}} {{ printf "href=%q" ""{url}}" | safeHTMLAttr }}>
{{"{{title}}"}}
</a>
</li>
</template>
</amp-list>
</ul>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
991 次 |
| 最近记录: |