-
Notifications
You must be signed in to change notification settings - Fork 6
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
Using templates defined in another scope #2
Comments
Bikeshed: maybe the signature of I'd also like to bikeshed the field names in |
I rather like the bikeshed on the signature - basically with tag strings, we are putting in place a new syntax for applying arguments to a function. So |
Concretely, What use do you have for |
@jimbaker This example in your initial comment looks wrong: from i18nlib import i18n_tag as _
from mytemplates import things_template
print(_(things_template, num=47)) The last line (you say) is supposed to be equivalent to num = 47
print(_"{num} things") but the expanded example seems to be missing the |
FWIW I finally get this, |
Example is written. |
Following up on https://peps.python.org/pep-0501/#proposal, let's demonstrate how we can create an interpolation template that can be used in another scope. We will use a helper function,
template
, that itself can be used as a tag:Like all tag functions,
template
takesSequence[str | Thunk]
, but it's actually just an identity function. Let's also assume tag functions take an optional**kwargs
:where
Thunk
is defined like so:With this in place, we can have the following two be equivalent, assuming
i18n_tag
takes keyword args for variables that are not defined, in this case fornum
:and
Implementing this simply requires in looping over the sequence of
str | Thunk
, we simply doso it gets
num
available in the namespace.Lastly, in making the tags just be be callables, we get some very nice syntax out of it, in terms of identity for creating explicit templates, as well as being able to directly sub in the variables with a near equivalent form.
The text was updated successfully, but these errors were encountered: