sbunker
sbunker

Reputation: 3

Twig - applying a leading "0" in front of gallery items

First time posting so forgive me in advance.

I'm building out an image gallery. I'm using Twig. Currently my Slick carousel gallery counter is displaying "1 / 4" items in my gallery. That's all kosher. Where I'm stumped is I'm trying to append a "0" so that the slide display reads "01/04".

Here is the HTML/Twig:

<div class="nav-controls">
    {% if gallery %}
        <div class="gallery-count"><span id="current-slide">1</span> / {{ gallery.images|length }}</div>
    {% endif %}
    <section role="navigation" class="gallery-arrows{% if gallery %} active{% endif %}">
        <div class="buttons">
            <div class="prev"></div>
            <div class="next"></div>
        </div>
    </section>
</div>

Is there not a way to do this with Twig?

Thank you!

Upvotes: 0

Views: 287

Answers (1)

malarzm
malarzm

Reputation: 2966

If you fancy printf syntax you can use Twig's format:

<div class="gallery-count">
  <span id="current-slide">01</span> / {{ "%02d"|format(gallery.images|length) }}
</div>

Upvotes: 6

Related Questions