【问题标题】:Error while printing simple value打印简单值时出错
【发布时间】:2018-02-26 17:24:54
【问题描述】:

我收到了这个错误:

类型:Twig_Error_Syntax 消息:参数必须用 逗号。值“h_open”(“标点符号”)的意外标记“名称” 预期值为 ",")。

我无法显示字段ticker.24h_open

       {% if ticker %}
                <div class="row">
                    <div class="col-3">
                        {{ ticker.amountAssetName }}
                    </div>
                    <div class="col-3">
                        {{ ticker.amountAssetID }}
                    </div>
                    <div class="col-3">
                        {{ ticker.amountAssetDecimals }}
                    </div>
                    <div class="col-3">
                        {{ dump( ticker.24h_open) }}
                    </div>
                </div>          
        {% endif %}

24 是 twig 中的保留关键字吗?

【问题讨论】:

  • 方法/变量不能以数字开头。更改您的住宿名称或使用{{ ticker['24h_open'] }}
  • 使用ticker['24_open'] 给我null。票是一个对象,而不是一个数组。我可以成功显示和访问ticker.amountAssetDecimals 和其他属性。这个API好像和twig不兼容。
  • 如果它是一个对象,你需要按照 Dormilich 的建议使用 attribute
  • 我通过使用 json_encode(curloutput, true) 并获取一个数组而不是一个对象来解决它,然后我在 PHP 中进行了计算并向数组添加了一个受支持的变量:$ticker['minimumFee'] = round( 0.001 / $ticker['24h_close'] * pow( 10,$ticker['amountAssetDecimals']),0); 查看 &lt;td&gt; {{ ticker.minimumFee }} &lt;/td&gt;

标签: php twig


【解决方案1】:

24 是 twig 中的保留关键字吗?

否,但变量名无效。 Twig 使用attribute() 函数通过数组键访问。

相关示例:

{{ attribute(ticker, '24h_close') }}

【讨论】:

  • 那么为什么我可以显示ticker.amountAssetDecimals 而不是ticket.24h_open? (我正在使用 twig 1.x)我尝试使用 {{ dump( attribute(ticker, 24h_open)) }} 但我收到此错误:消息:参数必须用逗号分隔。值“h_open”的意外标记“名称”(“标点符号”应为值“,”)。
  • 是{{ dump( attribute(ticker, '24h_open')) }},如果你不引用,你又犯了同样的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-03
  • 2011-03-29
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多