-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
Allow returning native python objects in minijinja-py #691
Comments
Matching the behavior of the I would love to better understand under which circumstances this is necessary and useful. |
First, thanks for the quick reply and for maintaining this library! In systems where Jinja is used widely, users use system-provided data objects to write templates, and often, intermediate variables are repeated in many templates. Instead of repeating the definition of a complicated variable in many templates, we render those separately and feed them into where they're used. This account describes my use case, but I believe that this is the case for every system where users manage a great number of templates. I admit that I initially thought that it's going to be as easy as "just don't stringify the result!", but I understand that it's probably much more complicated. |
Could you provide a motivating example with a few templates that demonstrates this? In particular I wonder if you just care about the single value response |
Sure: Say you want to use the expected delivery date for some order that a customer has made Without intermediate variablesExample 1{%- set fulfillments = (customer_orders or []) | selectattr('id', 'eq', latest_customer_order_id) -%}
{%- set estimated_delivery_dates = fulfillments | map(attribute='estimated_delivery_at') | unique | select | list -%}
{%- if estimated_delivery_dates | length == 1 -%}
{%- set estimated_delivery_date = estimated_delivery_dates[0] | with_tzinfo | format_date(...) -%}
{%- else -%}
{%- set estimated_delivery_date = 'next week' -%}
{%- endif -%}
Hi {{ CustomerName }},
Thank you for ordering ... !
Your order is due to arrive by {{ estimated_delivery_date }}.
... Example 2{%- set fulfillments = (customer_orders or []) | selectattr('id', 'eq', latest_customer_order_id) -%}
{%- set estimated_delivery_dates = fulfillments | map(attribute='estimated_delivery_at') | unique | select | list -%}
{%- if estimated_delivery_dates | length == 1 -%}
{%- set estimated_delivery_date = estimated_delivery_dates[0] | with_tzinfo | format_date(...) -%}
{%- else -%}
{%- set estimated_delivery_date = 'next week' -%}
{%- endif -%}
Hi {{ CustomerName }},
We're sorry to hear that your order is being delayed.
According to our courier, your order should arrive by {{ estimated_delivery_date }}.
... With intermediate variable
|
I'd like to add a use case here: this feature will make it possible to use |
Description
Python's implementation of Jinja2 allows returning native objects, as opposed to always stringifying the result. This is useful if you are using Jinja outside the context of creating text files, like an intermediate step where users may use templates to define values that will then be used elsewhere.
Example in Jinja2
Suggested behavior in minijinja-py
The text was updated successfully, but these errors were encountered: