mitsuhiko / minijinja

MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2
https://docs.rs/minijinja/
Apache License 2.0
1.67k stars 101 forks source link

Support adding methods to objects #438

Closed EricLBuehler closed 7 months ago

EricLBuehler commented 8 months ago

Hello all,

I am using minijinja to implement chat templating for LLMs. However, some templates call the .strip() method and I cannot find a way to inject this object. Is this possible to do? Thank you!

mitsuhiko commented 8 months ago

It's quite intentional that methods of Python objects are not exposed. For the same reason |items is used rather than .items() and a filter has been added to Jinja2 to match the two engines. For .strip() there is a |trim filter which has the same behavior.

{{ " foo "|trim }}

Closing this as out of scope.

mitsuhiko commented 8 months ago

See also https://github.com/pallets/jinja/issues/1561

EricLBuehler commented 8 months ago

Thank you!

mitsuhiko commented 7 months ago

On further reconsideration I do think it might be useful to add a callback to the environment so that someone could on their own fill in the blanks if they have a need.

mitsuhiko commented 7 months ago

I added a hook now in #441 which should help if you want to make this work.

EricLBuehler commented 7 months ago

Thank you, I saw that and will look into using it!