You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I really don't want to create a new system for declaring variables or importing code when Jinja already has them. I just want to be able to use them in in-line templates.
This way, I could declare a copier.yaml like this:
_exclude:
- name-slugname:
type: strdefault: A nice human-readable nameslug:
type: strdefault: '{% include "name-slug.jinja" %}'
Also, I could have a template structure like this:
📄 copier.yaml
📄 name-slug.jinja
🐍 {% include "name-slug.jinja" %}.py
Apart from freeing Copier devs from reinventing some wheels, this would empower Copier templaters to not only add variables, but rather do a lot more stuff. Also it would make them more used to get reusable knowledge on the Jinja engine, rather than on a niche use case such as Copier.
Using a template loader rather than passing strings to Template or Environment.from_string() has multiple advantages. Besides being a lot easier to use it also enables template inheritance.
In other words, you can't import or include from an in-line template (or without a loader).
However, in our in-line templates, we do have a filesystem location context. So, the solution is relatively simple:
Write a custom loader that tells Jinja that context.
Let Copier know which loader to use, depending on where the rendered value comes from (is it a file? a path? or a copier.yml value?). This would mean possibly using one of PrefixLoader or ChoiceLoader.
Describe alternatives you've considered
See other user requests. All of them contain workarounds:
Is your feature request related to a problem? Please describe.
Copier users keep requesting variables support. There are good reasons to avoid supporting variables:
copier.yaml
.ContextHook
extension for that, and it's on our docs.However, it's easy to understand that people still need to keep their templates DRY.
The real problem is not within the template files. As said, Jinja is DRY enough out of the box. The real problem lies within 2 other spaces:
copier.yaml
. Here, keys are not rendered but values are.These are not files, so they get rendered in-line:
copier/copier/user_data.py
Line 389 in 0f5257a
Describe the solution you'd like
I really don't want to create a new system for declaring variables or importing code when Jinja already has them. I just want to be able to use them in in-line templates.
This way, I could declare a
copier.yaml
like this:Also, I could have a template structure like this:
Apart from freeing Copier devs from reinventing some wheels, this would empower Copier templaters to not only add variables, but rather do a lot more stuff. Also it would make them more used to get reusable knowledge on the Jinja engine, rather than on a nic
he use case such as Copier.Describe how to implement it
Quoting from Jinja docs:
In other words, you can't import or include from an in-line template (or without a loader).
However, in our in-line templates, we do have a filesystem location context. So, the solution is relatively simple:
copier.yml
value?). This would mean possibly using one ofPrefixLoader
orChoiceLoader
.Describe alternatives you've considered
See other user requests. All of them contain workarounds:
Additional context
Cookiecutter has private variables.
The text was updated successfully, but these errors were encountered: