{{ switch }} continues across nested loops when the inner count is odd

Abierto Apto para principiantes
#15,820 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
laravel, php
Área
backend

Línea de trabajo

El error está en la clase de la etiqueta Rotate, que guarda su posición en un array estático $counts indexado únicamente por un hash md5 de los parámetros de la etiqueta. Empieza reproduciendo el problema con la plantilla de bucles anidados del issue y revisa cómo se construye y se reinicia ese hash. Se considera terminado cuando cada bucle interno vuelve a empezar en el primer valor, con una prueba que cubra bucles anidados con un número impar de iteraciones internas.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

tags
Bug description

{{ switch }} tag does not restart at the beginning of each inner loop when it is rendered inside a nested loop.
The counter is shared for the whole request.

How to reproduce

With two values,
an odd inner loop makes the next parent item continue the previous sequence.
An even inner loop hides the problem, because the sequence lands back on the first value.

Steps to reproduce

{{ loop times="2" }}
    {{ loop times="3" }}
        {{ switch between="1|0" }}
    {{ /loop }}
{{ /loop }}
Expected

Each inner loop starts again at the first value.
1 0 1
1 0 1

Actual

1 0 1
0 1 0

The second group is the continuation of the first (… 1 then 0 1 0), not a new 1 0 1.

Logs

Environment
Environment
Laravel Version: 12.70.2
PHP Version: 8.4.16
Composer Version: 2.10.2
Environment: local
Debug Mode: ENABLED
Maintenance Mode: OFF
Timezone: UTC
Locale: fr_CA

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: file
Database: sqlite
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Storage
public/storage: NOT LINKED

Statamic
Addons: 2
License Key: Not set
Sites: 2 (Français, English)
Stache Watcher: Disabled
Static Caching: Disabled
Version: 6.40.0 PRO

Statamic Addons
jacksleight/statamic-bard-texstyle: 4.2.3
mg/litespeed: dev-dev
Installation

Fresh statamic/statamic site via CLI

Additional details
Cause

Statamic\Tags\Rotate stores the position on a static array, keyed only by a hash of the tag parameters:

protected static $counts = [];
$hash = md5(implode(',', $this->params->all()));
if (! isset(static::$counts[$hash])) {
    static::$counts[$hash] = 0;
}
$switch = $vars[static::$counts[$hash] % count($vars)];
static::$counts[$hash]++;

resetStaticState() clears that array between requests. Nothing clears it between iterations of the parent loop, and the hash does not include the parent iteration. Every render of the same {{ switch }} shares one counter.

A parameter such as for="gallery" does not scope the counter to a loop. It is only part of the hash, so it separates this tag from another {{ switch }} with different parameters.

Lenguaje dominante
PHP
Estrellas
4.9k
Forks
647
Merge medio
23 h 42 min
PR fusionados (30 d)
148

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de statamic/cms

Todos los issues de statamic/cms

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.