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

Aperta Adatta ai principianti
#15,820 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
laravel, php
Ambito
backend

Direzione di ricerca

Il bug si trova nella classe del tag Rotate, che mantiene la propria posizione in un array statico $counts indicizzato solo tramite un hash md5 dei parametri del tag. Inizia riproducendo il problema con il template di cicli annidati presente nella issue, quindi leggi come viene costruito e azzerato quell'hash. È completato quando ogni ciclo interno ricomincia dal primo valore, con un test che copra cicli annidati con un numero dispari di iterazioni interne.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
PHP
Stelle
4.9k
Fork
647
Merge medio
23h 42m
PR unite (30g)
148

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di statamic/cms

Tutte le issue di statamic/cms

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.