php - Grav/Twig 循环数组

标签 php content-management-system twig grav

我的 Grav/Twig 模板有一个简单的问题。我有一个 .md 文件,其结构如下...

---
class: news
title: Latest News
story:
  title: TITLE OF STORY
  date:
    month: April
    day: 2
    year: 2016
  highlights:
    points:
      - icon: mic.png
        text: Best Speaker Award
      - icon: trophy.png
        text: Winner as voted by audience
  quote: 
    text: “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur dictum nisl quis pharetra dictum. Etiam ac egestas leo. Pellentesque tincidunt sem vel risus vehicula scelerisque. Cras rutrum, mauris viverra porttitor condimentum, orci quam condimentum libero, ac tempor nunc risus ac felis.”
    author: -Time Magazine
  cite: http://www.time.com
  image: awards.jpg
---

我想提取 icon: mic.png 和 icon: tropy.png 值,并在 news.twig.html 文件中执行类似的操作...

<div class="highlights">
    {% for highlight in page.header.story.highlights.points %}
      <div class="point">
        {% if highlight.text %}
        <span class="highlight-icon">              
          <img src="{{ page.media.images.[ {{ highlight.icon }} ].url }}">
        </span>
        {% endif %}
        {% if highlight.text %}
        <span class="highlight-text">{{ highlight.text }}</span>
        {% endif %}
      </div>
    {% endfor %}

但是我收到一个错误,“预期的名称或号码”位于带有 img 标记的行上,

<img src="{{ page.media.images.[ {{ highlight.icon }} ].url }}">

提前谢谢您!!

最佳答案

如果其他人将来遇到同样的问题,这是我的答案...我将行更改为...

<img src="{{ page.find('/images').media[highlight.icon].url }}">

关于php - Grav/Twig 循环数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36378453/

相关文章:

php - 不使用 PDO 创建表

jquery - CKEditor自定义图像浏览器

model-view-controller - Joomla!遵循 MVC 架构?

ios - CMS,或用于社区文件共享的预制解决方案

php - Symfony2 : form_widget call in twig throws exception "Catchable fatal error ... must be an instance of Symfony\Component\Form\FormView"

php - Twig 不会抛出异常

php - 在 Magento 中从 URL 调整图像大小

php - 在 MAMP 上安装 PECL

php - Mysql,如何读取同一插入行的其他字段中的自增值?

templates - Symfony 2 中的模板部分