json - 尝试实现 amp-mustache 时出错

标签 json jekyll mustache amp-html amp-mustache

我正在尝试复制 this example没有成功。我想使用 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>

我的 /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"
   }
 ]
}

但我无法让它工作,json 中的值没有在模板中被替换,我收到此错误:
Missing URL for attribute 'href' in tag 'a'

不知道为什么值{{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>
更新 : 我写了一个more detailed explanation
雨果
<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>

关于json - 尝试实现 amp-mustache 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38672182/

相关文章:

java - 在 JAX-RS Web 服务过滤器中获取 JSON 消息

python - 在 python 中保存 json 响应

css - Jekyll 的图像设置在不同页面上的工作方式不同

PHP Mustache 2.1 部分加载不基于文件名

php - 我无法使用 json 在本地主机服务器中发布数据和图像

android - 用浏览器代码替换一些特殊字符

github - 如何在不本地安装 Jekyll 的情况下将新帖子添加到 github 页面

jekyll - 如何防止我的github页面制作帖子标题 'title-cased'

javascript - mustache javascript : how to handle with boolean values

javascript - mustache 可以迭代顶级数组吗?