Skip to content
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

Before rendering a file, add _template_file and _this_file to the context #1453

Open
bswck opened this issue Dec 8, 2023 · 8 comments
Open

Comments

@bswck
Copy link

bswck commented Dec 8, 2023

Actual Situation

During developing my template, I've decided that it would be a good idea to place comments a'la "this file was created from template, consider changing the template" in files that come from the template:

https://github.com/bswck/downstream/blob/c28c694f011aab17a6f7185d948bddd4a5657309/.gitignore#L1-L3

But I had to use this ugly hardcoding the relpath:

https://github.com/bswck/skeleton/blob/35fb54c5ce37f28201eea2f532570192449d61f4/project/.gitignore.jinja#L1C5-L1C31

Same for other template-managed files.

Desired Situation

I can consistently use {{_template_file|skeleton_notice(snref=snref, srev=srev)|line_prefix("# ")}} in every file.
This will prevent any future situations where I rename a template file, but the skeleton notice remains unchanged with the broken path.

Proposed solution

Before rendering a file, add _template_file and _this_file to the context (naming can be different).
I suggest _this_file apart from _template_file for consistency.

I can take this task if you would like this to be implemented.

@yajo
Copy link
Member

yajo commented Dec 17, 2023

I don't see how a comment in the file telling the name of the file would be useful, but I also have nothing against the feature if someone wants to contribute it.

@yajo yajo added this to the Community contribution milestone Dec 17, 2023
@bswck
Copy link
Author

bswck commented Dec 18, 2023

I don't see how a comment in the file telling the name of the file would be useful

Telling the template filename would be useful in comments, because it can navigate template downstream projects contributors to change things in templates -- at the infrastructure level -- not in the downstream files.

When it comes to exposing filenames of the rendered files to themselves, I think it can potentially be useful to, for example, pass those filenames to filters from the template extensions that could customize behavior, such as automatically documenting in the README which files come from the copier template and which not. Just as an example.

@bswck
Copy link
Author

bswck commented Dec 18, 2023

I think it is technically possible to retrieve these metadata from Jinja2 itself as it renders files, but it isn't quite convenient programmatically, because it has no real use in website HTML templates Jinja2 is primarily designed for.

@yajo
Copy link
Member

yajo commented Dec 19, 2023

Have you tried this? https://stackoverflow.com/a/40346872/1468388

@bswck
Copy link
Author

bswck commented Dec 19, 2023

Have you tried this? stackoverflow.com/a/40346872/1468388

This is what I was referring to. But instead, I've built an extension that sets _origin in my context.
Anyway, I still think setting these variables might be useful universally.

@bswck
Copy link
Author

bswck commented Dec 19, 2023

Especially since in the raw template filename there is this /tmp/<some_copier_unique_dir>/ prefix, which could ideally be stripped.

@yajo
Copy link
Member

yajo commented Dec 19, 2023

OK makes sense.

Add _copier prefix to the variables, as we usually do. Example: _copier_src_file and _copier_dst_file

@ssteinerx
Copy link

I would also find it useful to access the template file name and the eventual output file name.

It's especially useful in scenarios like YAML Front Matter for markdown files, which are part of documentation or static site generators like Jekyll.

Regarding the code snipped in the shared Stack Overflow link, when I try it I get this:

  {{ self._TemplateReference__context.name }}

Raises:

jinja2.exceptions.SecurityError: access to attribute '_TemplateReference__context' of 'TemplateReference' object is unsafe.

I've looked at the extensions/context.py and am poking around, but it would be very helpful not to have to take a deep dive into extensions and context management to access basic context information like the current template (or the list of inherited templates!) and output file name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants