Skip to content

Conversation

@mattt
Copy link
Collaborator

@mattt mattt commented Sep 16, 2025

@johnmai-dev
Copy link
Contributor

Thank you very much for contributing this PR! ♥️

I’ve run some tests and found a few issues:

  1. Missing default parameter int(default=99) / float(default=1.5)

Caught error: runtime("Unexpected keyword argument 'default' for filter.")

let template = try Template("{{ 'invalid'|int(default=99) }}")
let result = try template.render([:])
#expect(result == "99", "Should return default value when conversion fails")
import jinja2
template = jinja2.Template("""
{{ 'invalid'|int(default=99) }}
""")
print(template.render({}))
  1. Missing attribute parameter max(attribute='age') / min(attribute='age')

Caught error: runtime("Unexpected keyword argument 'attribute' for filter.")

let context = [
    "users": Value.array([
        .object(["name": .string("John"), "age": .int(30)]),
        .object(["name": .string("Jane"), "age": .int(25)]),
        .object(["name": .string("Bob"), "age": .int(35)])
    ])
]

let template = try Template("{{ (users|max(attribute='age')).name }}")
let result = try template.render(context)
#expect(result == "Bob", "Should return user with highest age")
import jinja2
template = jinja2.Template("""
{{ (users|max(attribute='age')).name }}
""")

print(template.render({
    'users': [
        {"name": "John", "age": 30},
        {"name": "Jane", "age": 25},
        {"name": "Bob", "age": 35}
    ]
}))
  1. testName parameter should be optional select / reject

Caught error: runtime("Missing required argument 'testName' for filter.")

let context = [
    "items": Value.array([
        .string("hello"),
        .string(""),
        .string("world"),
        .null,
        .string("test")
    ])
]

let template = try Template("{{ items|select|join(',') }}")
let result = try template.render(context)
#expect(result == "hello,world,test", "Should select truthy values")
import jinja2
template = jinja2.Template("""
{{ items|select|join(',') }}
""")

print(template.render({
    'items': [
        "hello",
        "",
        "world",
        None,
        "test"
    ]
}))
  1. escape - HTML entity encoding differences

Expectation failed: (result → "<">& ") == "<">& "

let template = try Template("{{ text|escape }}")
let result = try template.render(["text": .string("<\">& ")])
#expect(result == "&lt;&#34;&gt;&amp; ", "Should use numeric entities for quotes")
import jinja2
template = jinja2.Template("""
{{ text|escape }}
""")
print(template.render({
    'text': '<">& '
}))

@mattt mattt force-pushed the mattt/jinja branch 2 times, most recently from 11188db to 00172f6 Compare September 22, 2025 16:27
@mattt
Copy link
Collaborator Author

mattt commented Sep 22, 2025

@johnmai-dev That's super helpful! Thank you for going through everything with a fine-toothed comb. I just opened a PR fixing those issues here: huggingface/swift-jinja#3

@mattt mattt force-pushed the mattt/jinja branch 3 times, most recently from e5c41be to 5830fc5 Compare September 24, 2025 16:59
@mattt mattt marked this pull request as ready for review September 24, 2025 17:00
@mattt mattt requested review from FL33TW00D and pcuenca September 24, 2025 17:16
@mattt mattt marked this pull request as draft September 24, 2025 17:20
@mattt
Copy link
Collaborator Author

mattt commented Sep 24, 2025

Converted to draft until 2.0.0 release is available and we can revert / drop 5830fc5.

@mattt mattt changed the title Add Jinja module to package Upgrade Jinja package to version 2.0.0 Sep 24, 2025
@mattt mattt marked this pull request as ready for review September 25, 2025 11:11
@mattt mattt mentioned this pull request Sep 25, 2025
@mattt

This comment was marked as resolved.

@mattt mattt merged commit b62cda1 into main Sep 25, 2025
2 checks passed
@mattt mattt deleted the mattt/jinja branch September 25, 2025 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants