Skip to content

Latest commit

 

History

History
67 lines (50 loc) · 1.35 KB

count.md

File metadata and controls

67 lines (50 loc) · 1.35 KB

Count Twig 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.

xml

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="app.twig.web_count" class="Massive\Component\Web\CountTwigExtension">
            <tag name="twig.extension" />
        </service>
    </services>
</container>

yml

services:
    app.twig.web_count:
        class: Massive\Component\Web\CountTwigExtension
        tags:
            - { name: 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 %}">