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

Sharing identical files between templates #67

Closed
pykong opened this issue Sep 16, 2019 · 6 comments
Closed

Sharing identical files between templates #67

pykong opened this issue Sep 16, 2019 · 6 comments
Milestone

Comments

@pykong
Copy link
Collaborator

pykong commented Sep 16, 2019

I share many identical files between different template folders.
I'd really like to keep my templates DRYer and include those files by reference only.

@jpscaletti
Do you get an idea of how we could achieve this?

Some ideas:

  • We allow for a multi-level inheritance whereby a template folder inherits templates from a parent template.
  • We allow specifying folder whose contents or a list of file paths to be copied over to the destination folder.

Relates to: #58

@ghost
Copy link

ghost commented Sep 16, 2019

A soft link? Copy & Paste? The inheritance system for templates sounds like over-engineering it to me.
Update: See my comment below.

@ghost
Copy link

ghost commented Sep 16, 2019

I was writing that Jinja's inheritance mechanism doesn't work with files outside the templates folder, but I just remembered the one of Jinja's loader allows you to specify several folders as sources...

So yes, in theory, we could use Jinja's ChoiceLoader or maybe a PrefixLoader by default and have a list of optional "extra templates folders" in the copier.yml file to add to the current working folder (added by default). In that way, you could call a group of templates to use however you want.

How it's sound to you?

@pykong
Copy link
Collaborator Author

pykong commented Sep 17, 2019

Thanks for your answer. You appear to a have much more knowledge about Jinja2's engine than me.
Let me try to translate your suggestions into code. Should be ready by the weekend.

My suggestion is that once we merge (or reject) the PR resulting from that you may release copier 3.

@pykong
Copy link
Collaborator Author

pykong commented Sep 17, 2019

I wonder if we couldn't just:

  • provide a list of additional source folder paths
  • iterate over this list against copy_local

We would need to define an order of precedence within that list and versus the original src_path setting.

I am still researching whether using Jinja2's ChoiceLoader would offer any benefits.
As far as my current understanding goes ChoiceLoader would only provide additional lookup paths for templates but not instructable to include additional folders.

The documentation of the ChoiceLoader class reads:

This loader works like the PrefixLoader just that no prefix is specified. If a template could not be found by one loader the next one is tried.

@pykong
Copy link
Collaborator Author

pykong commented Sep 20, 2019

I believe we were talking about different things.

You: How to teach Jinja2 to locate additional files...
Me: How can we keep our templates DRY...

We can already specify additional lookup paths via the extra_paths setting.
Yet, this is only "half-DRY" as we include file content from another location but would still require to do so in an *.tmpl file. Hence we may need to include a lot of the same of those files in multiple templates. Not exactly DRY.

Nevermind I have an elegant idea on how to address this.
I am prototyping this idea right now. If it works as robustly as I am imaging copier will be endowed with a new and very powerful feature right in time before its version 3. ;-)

@pykong pykong mentioned this issue Sep 23, 2019
9 tasks
@pykong pykong changed the title Enhancement - Sharing identical files between templates Sharing identical files between templates Nov 20, 2019
@pykong pykong added this to the 3.0.0 milestone Nov 20, 2019
@pykong
Copy link
Collaborator Author

pykong commented Feb 26, 2020

This is issue is no longer relevant.

@pykong pykong closed this as completed Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant