pallets / jinja

A very fast and expressive template engine.
https://jinja.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
10.41k stars 1.62k forks source link

The unique filter is case insensitive #2053

Closed lucaguarro closed 2 hours ago

lucaguarro commented 2 hours ago

The unique filter ignores the case of the strings in the list. For example if I have a list that looks like ["A", "a"] and I apply the unique filter, I will only get "A" in return.

Here is a minimal reproducible example:

from jinja2 import Environment, FileSystemLoader

# Sample data with duplicates
data = {
    'items': ["A", "a"]
}

# Create a Jinja2 environment
env = Environment(loader=FileSystemLoader('.'))

# Define the Jinja2 template
template_content = """
Original List:
{{ items }}

Unique Values:
{%- set unique_items = items | unique -%}
{% for item in unique_items %}
- {{ item }}
{% endfor %}
"""

# Load the template from the string
template = env.from_string(template_content)

# Render the template with the data
output = template.render(data)

# Print the output
print(output)

I would expect the output to be:

Original List:
['A', 'a']

Unique Values:
- A
- a

Instead, I get:

Original List:
['A', 'a']

Unique Values:
- A

Environment:

davidism commented 2 hours ago

This is covered in the documentation: https://jinja.palletsprojects.com/en/stable/templates/#jinja-filters.unique