【问题标题】:Loop through twig array in Drupal在 Drupal 中循环遍历树枝数组
【发布时间】:2016-10-25 19:48:59
【问题描述】:

我在循环遍历 Drupal 8 中的 html.twig 文件中的简单数据数组时遇到问题。当我将以下块添加到页面时,Drupal 遇到“意外错误”。现在,这对页面来说是静态的也没关系。

注意:我使用 Drupal 和 twig 还不到一周,而我的 PHP 已经生锈了 10 年。

{%

set top_customers = {
   { name: "Altera G", logo: "logo-alterg.png", }
   { name: "Hollywood Was Museum", logo: "logo-hollywoodwaxmuseum.png", }
   { name: "iroaHealth", logo: "logo-iorahealth.png", }
   { name: "Lionel", logo: "logo-lionel.png", }
   { name: "Mashable", logo: "logo-mashable.png", }
   { name: "People Fluent", logo: "logo-peoplefluent.png", }
   { name: "Shop Kick", logo: "logo-shopkick.png", }
   { name: "Wistia", logo: "logo-wistia.png", }
 }

 %}   

 {{ dump(top_customers) }}

<section class="row cta-section row-padding-130">
    <div class="row-inner site-width">
        <div class="brick-10 center-brick">
            <h3>These brands are changing the way they think about AP by using MineralTree</h3>
        </div>  
        <div class="brick-12 center-brick">
            <ul class="logo-list margin-top-78 clearfix">
                {% for customer in top_customers %}
                <li>
                    <img class="customer-logo img-fluid" src="{{ directory }}/images/customer-logos/{{ customer.logo }}" alt="{{ customer.name }}" />
                </li>   
                {% endfor %}
            </ul>
        </div>
        <div class="brick-6 center-brick">  
            <div class="margin-top-78"> 
                <a href="/about-us/our-customers.html" class="solid-cta-btn button-large">view all our customers</a>
            </div>  
        </div>
    </div>
</section>

感谢您的帮助!

【问题讨论】:

  • 你还有这个错误吗?当我们不知道在哪里寻找时,很难开始。
  • 最近的日志消息显示:“Twig_Error_Syntax:哈希键必须是带引号的字符串、数字、名称或括号中的表达式(值“{”的意外标记“标点符号”在“主题/自定义/mineraltree/templates/region--banner5.html.twig”在第 4 行。在 Twig_ExpressionParser->parseHashExpression() (/Applications/MAMP/htdocs/mineraltree/vendor/twig/twig/lib/Twig 的第 281 行/ExpressionParser.php)。”

标签: php drupal twig


【解决方案1】:

尝试将top_customers 定义为数组,如下所示:

{%

set top_customers = [
   { name: "Altera G", logo: "logo-alterg.png" },
   { name: "Hollywood Was Museum", logo: "logo-hollywoodwaxmuseum.png", },
   { name: "iroaHealth", logo: "logo-iorahealth.png", },
   { name: "Lionel", logo: "logo-lionel.png", },
   { name: "Mashable", logo: "logo-mashable.png", },
   { name: "People Fluent", logo: "logo-peoplefluent.png", },
   { name: "Shop Kick", logo: "logo-shopkick.png", },
   { name: "Wistia", logo: "logo-wistia.png", },
 ]

 %} 

Here 一个工作示例

【讨论】:

  • 谢谢@Mateo!我正在从 Middleman 转换为 Drupal,并且到处都有数据循环......这对我来说是一个巨大的帮助。
猜你喜欢
  • 1970-01-01
  • 2016-09-02
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2019-11-20
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多