0

I am using cookiecutter 1.7.3

I want a way to have defaults in case an undefined variable in the template.

What I tried

using default filter in jinja

"github_username": "{{ cookiecutter.github_repo.owner.login|default('Your GitHub username here', true) }}"

or boolean

"github_username": "{{ cookiecutter.github_repo.owner.login or 'Your GitHub username here' }}"

None of this works. I still get undefined variable in template error.

Can advise?

Kim Stacks
  • 10,202
  • 35
  • 151
  • 282

1 Answers1

0

Since this question remains unanswered, adding something that worked for me. I used the is defined expression to check if the key of the object exists since cookiecutter threw an error if the key didn't exist. If you're sure the key will not be undefined and exists and you need to check for a value, you can do something like:

"yay" if cookiecutter.github_repo.owner == "Dr. Acula" else ":("

So in this case, it would be something like this:

"github_username": "{{ cookiecutter.github_repo.owner.login if cookiecutter.github_repo.owner.login is defined else "Your GitHub username here" }}"
Dr. Acula
  • 469
  • 6
  • 13