Skip to content

Latest commit

 

History

History
47 lines (33 loc) · 929 Bytes

count.md

File metadata and controls

47 lines (33 loc) · 929 Bytes

Count Extension

The count twig extension gives you a simple and efficient way to have a global counter in your twig template.

Setup

Service Registration

The twig extension need to be registered as symfony service.

services:
    Sulu\Twig\Extensions\CountExtension: ~

If autoconfigure is not active you need to tag it with twig.extension.

Usage

Counter

You can increase and get the current counter with:

{{ counter('example') }}
{{ counter('example') }}
{{ counter('test') }}
{{ counter('test') }}
{% do reset_counter('example') %}
{{ counter('example') }}

Output:

1
2
1
2
1

A more real use case would be to check for odd or even:

<div class="section{% if counter('section') is odd %} section--black{% endif %}">