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

[4.x]:{% namespace %} Twig tag should allow 0 #13943

Closed
engram-design opened this issue Nov 18, 2023 · 5 comments
Closed

[4.x]:{% namespace %} Twig tag should allow 0 #13943

engram-design opened this issue Nov 18, 2023 · 5 comments

Comments

@engram-design
Copy link
Contributor

engram-design commented Nov 18, 2023

What happened?

Thinking that the {% namespace %} tag should accept 0 as a value, and it currently doesn't. Probably to do with a falsey check, but I can't figure out how that works, sorry!

{% set items = ['first', 'second'] %}

{% for item in items %}
    {% namespace loop.index0 %}
        {{ tag('input', {
            name: item,
        }) }}
    {% endnamespace %}
{% endfor %}

// Produces
<input name="first">
<input name="1[second]">

// Expected
<input name="0[first]">
<input name="1[second]">

In reality, this would be surrounded by fields and the field handle, but in this reduced example, you can't have multiple {% namespace %} tags, so in a real-world use-case you'd produce:

// Expected
<input name="fields[myField][0][first]">
<input name="fields[myField][1][second]">

My use-case here is that the value for a field is an array, and I'd like to keep things simple by using an array syntax, rather than new1 or similar like Matrix does.

Craft CMS version

4.5.9

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@engram-design
Copy link
Contributor Author

Oh, managed to track down where that is, keen to hear your opinion on #13944

brandonkelly added a commit that referenced this issue Dec 11, 2023
@brandonkelly
Copy link
Member

brandonkelly commented Dec 11, 2023

Namespaces are used to prefix id HTML attributes, and id attributes aren’t allowed to begin with a number. So namespaces shouldn’t be either.

I’ve just added a check to ensure namespaces follow id attribute rules for Craft 5 (d568986).

You can work around that by prefixing your keys with a string, e.g. value:0, value:1, etc.

@brandonkelly
Copy link
Member

Nevermind, that turned out to be pretty heavy-handed, and I forgot that we’re already ensuring namespaces are properly formatted for id attributes via craft\helpers\Html::id().

Your PR is now merged for the next Craft 4 release, and I’ve adjusted Html::id() to trim out any leading digits for Craft 5. (Entirely-numeric IDs will end up getting normalized to a 10-digit random string.)

@engram-design
Copy link
Contributor Author

engram-design commented Dec 14, 2023

Cheers! I know it's kinda a "dumb" PR, but might be handy in some instances...

@brandonkelly
Copy link
Member

4.5.13 is out with that change!

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

No branches or pull requests

2 participants