1

I am inserting a list of sponsor images in a sidebar in grav and want to show only images starting with sponsor_.

At the moment, my code adds any image in the page folder.

base.html.twig:

<div class="sidebar-right-content">
<!-- insert R sidebar images here     -->

<div class="sponsor-image">
  <h3>Sponsors</h3>
    {% for image in page.media.images %}
      <br>{{ image.html }}
    {% endfor %}
</div>

I have tried restricting the images returned by using the expression below, but no images are returned:

{% for image in page.media.images if image matches '/^sponsor.*$/' %}

Is there a way to use a filter in this context?

4

2 回答 2

1

try: {% image in page.media.images |contains('sponsor') %}

于 2016-12-05T03:56:49.187 回答
1

After some testing of a few options, I found a somewhat inelegant way to solve this problem. The main thing to undertand is that page.media.images returns an array, so we can't filter this as a string. So I just nested a match statement. I wonder if there's a one-liner solution.

{% for image in page.media.images %}
    {% if image.html matches '/.*sponsor.*$/' %}
    <br>{{ image.html }}
    {% endif %}
{% endfor %}
于 2016-12-05T07:52:24.893 回答