Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$index not available in Blade Collection / Taxonomy tag #11377

Closed
stuartcusackie opened this issue Jan 22, 2025 · 3 comments
Closed

$index not available in Blade Collection / Taxonomy tag #11377

stuartcusackie opened this issue Jan 22, 2025 · 3 comments
Labels

Comments

@stuartcusackie
Copy link

stuartcusackie commented Jan 22, 2025

Bug description

The documentation for both the collection tag and the taxonomy tag show a list of variables, including $index.

I cannot figure out how to access these variables in Blade. I've dump all_defined_vars but I don't see it there. Seems like a bug.

How to reproduce

Try to access $index in either the collection tag or the taxonomy tag, e.g.:

<statamic:collection from="blog">
    <li><a href="{{ $url }}">{{ $index }} {{ $title }}</a></li>
</statamic:collection>
<s:taxonomy from="categories">
  <li><a href="{{ $url }}">{{ $index }} {{ $title }}</a></li>
</s:taxonomy>

$index undefined!

WORKAROUND:

<s:taxonomy from="categories" as="categories">
  @foreach($categories as $index => $category)
    <li><a href="{{ $category->url }}">{{ $index }} {{ $category->title }}</a></li>
  @endforeach
</s:taxonomy>

Logs

Environment

Version: 5.45.2 PRO

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

@stuartcusackie stuartcusackie changed the title $index not available in Blade Entry / Taxonomy tag $index not available in Blade Collection / Taxonomy tag Jan 22, 2025
@duncanmcclean
Copy link
Member

duncanmcclean commented Jan 22, 2025

I'm not sure if it'll work with our Blade Components, but can you try $loop->index? In traditional foreach loops, Blade exposes a $loop variable.

(Also, while it may not seem necessary, providing the full output of support:details can be very helpful. Please provide it on future issues.)

@stuartcusackie
Copy link
Author

Yes, $loop->index is indeed available. Thanks.

It would be great if either $index could made available or the docs could be updated. The team has done a great job for Blade users but I still spend quite a lot of time trying to find workarounds like these. All of the old Statamic::tag('collection') examples have been removed so Blade users are kind of forced to use the new tags with insufficient documentation.

Noted on support:details. I'll add them all in future. 👍

@duncanmcclean
Copy link
Member

I've opened an issue on the docs: statamic/docs#1597

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants