Skip to content

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

@engram-design

Description

@engram-design

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions