木材图像 - 使用自定义尺寸

Timber Image - using custom sizes

在我在 https://github.com/timber/timber/

上提出问题之前先在这里发布

我想显示自定义尺寸的 ACF 图像,例如:

<img src="{{ image.sizes.medium }}" width="{{ image.sizes.medium-width }}" height="{{ image.sizes.medium-height }}" alt="{{ image.alt }}" title="{{ image.title }}"> 

这是 image 数组

Array
(
    [id] => 473
    ...
    [width] => 768
    [height] => 400
    [sizes] => Array
    (
        [thumbnail] => http://assets.url.com/uploads/image-150x150.jpg
        [thumbnail-width] => 150
        [thumbnail-height] => 150
        [medium] => http://assets.url.com/uploads/image-300x300.jpg
        [medium-width] => 300
        [medium-height] => 300
        [medium_large] => http://assets.url.com/uploads/image-768x400.jpg
        [medium_large-width] => 768
        [medium_large-height] => 400
    ...
    )
)

当然,破折号会破坏东西,所以我不能。有没有更正确的方法来做到这一点,或者我应该 post 这是 github 中的一个问题?或者这可能是 ACF 问题?感谢您提供任何帮助,这可能会为我指明正确的方向。

当你在 Twig 中有一个数组时,使用点符号只是访问其内容的一种方法。您还可以访问与在 PHP:

中相同的内容 through the so-called subscript notation
<img src="{{ image.sizes.medium }}"
    width="{{ image.sizes['medium-width'] }}"
    height="{{ image.sizes['medium-height'] }}"
    alt="{{ image.alt }}" title="{{ image.title }}">

这仅适用于数组。如果 对象 的 属性 名称中有破折号,则必须使用 attribute()。这是 Twig 文档中的示例:

{{ attribute(foo, 'data-foo') }}