Closed jimbaker closed 1 year ago
Bikeshed: maybe the signature of template
can be (*items: str | Thunk)
? (Or if you really want keyword args, (*items: str | Thunk, **kwargs)
?)
I'd also like to bikeshed the field names in Thunk
, but later.
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 f(*args, **kwargs)
is that most general form.
Concretely, tag "foo{bar}baz"
would end up calling tag("foo", bar, "baz")
.
What use do you have for **kwargs
?
@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 "{num} things"
text.
FWIW I finally get this, things_template
is the template created from the "{num} things"
string using the template()
function. Jim is demonstrating how the template can be in a different module.
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.