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

Private variables are not exposed for rendering #2002

Open
BasPH opened this issue Jan 15, 2024 · 2 comments
Open

Private variables are not exposed for rendering #2002

BasPH opened this issue Jan 15, 2024 · 2 comments

Comments

@BasPH
Copy link

BasPH commented Jan 15, 2024

  • Cookiecutter version: 2.5.0
  • Template project url:
  • Python version: 3.12.1
  • Operating System: MacOS

Description:

I expected that I could use private variables for variables that I want to use for generating a project, but hide it for users. For example:

{
  "team_name": "X",
  "__project_name": "my_org_team_{{ cookiecutter.team_name.lower().replace(' ', '_').replace('-', '_') }}",
}

With the above, you (as expected) only get asked for "team_name", but when rendering something with {{ cookiecutter.project_name }}, I get an error:

Error message: 'collections.OrderedDict object' has no attribute 'project_name'
Context: {
    "_cookiecutter": {
        "team_name": "X"
    },
    "cookiecutter": {
        "__project_name": "my_org_team_x",
        "team_name": "X"
    }
}

(removed a few other redundant variables)

Is my understanding of private variables in cookiecutter incorrect, or is this a bug? I would expect that I could reference the private variable whilst rendering with {{ cookiecutter.project_name }}.

What I've run:

See above

@BasPH BasPH changed the title Private variable are not exposed for rendering Private variables are not exposed for rendering Jan 15, 2024
@BasPH
Copy link
Author

BasPH commented Jan 15, 2024

Update: turns out {{ cookiecutter.__project_name }} does the trick. Is this intentional? If so, I'd be happy to add an example to the docs.

@alanverresen
Copy link
Contributor

alanverresen commented Jan 15, 2024

I guess your assumption could've worked, so clarifying how rendered private variables are actually used seems like a good idea. I also quickly skimmed the docs and there doesn't seem to be an example of rendered private variables being used in the cookiecutter itself—perhaps most imporantly not in the section specifically about private variables.

It should be enough to extend the existing example from the section on private variables with the cookiecutter's directory structure:

{{ cookiecutter.__project_slug }}
├── Makefile
├── README.md
├── requirements.txt
└── src
    ├── {{ cookiecutter.__package_name }}
    │   └── __init__.py
    ├── setup.py
    └── tests
        └── __init__.py

Additional clarifications/information where deemed necessary may also be helpful.

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

No branches or pull requests

2 participants