Template Automation & Sync Guide

Cookiecutter • Jinja2 Extensions • Cruft • Hooks • CI/CD Integration

1. Setup & Installation

Crucial: Installing cruft automatically installs cookiecutter.

pip install cruft jinja2-strcase jinja2-time

Test locally: cruft create . --no-input

2. Internal & Nested Variables

{
  "project_name": "My Power App",
  "project_slug": "{{ cookiecutter.project_name | to_snake }}",
  "_extensions": ["jinja2_strcase.StrcaseExtension", "jinja2_time.TimeExtension"],
  "_copy_without_render": ["*.png"]
}

3. Power Extensions

Filter Result (Input: “API Key”)
to_snake api_key
to_pascal ApiKey
to_kebab api-key
to_constant API_KEY

4. Lifecycle with Cruft

  • cruft check: Check for template updates.
  • cruft diff: See changes from base template.
  • cruft update: Sync template changes into project.

5. Python Hooks

Pre-Gen (Validation) & Post-Gen (Cleanup):

# hooks/post_gen_project.py
import os, subprocess
subprocess.call(['git', 'init'])

6. Debugging

Metadata: _template, _repo_dir, _output_dir.

Full dump: {{ cookiecutter | dictsort }}

7. GitLab CI Sync

check_sync:
  script:
    - pip install cruft
    - cruft check

8. Expert Tips

Use {% raw %}{{{% endraw %} to skip rendering and "_comment" for JSON docs.


lu122131

Luca Bortolotti Lentsch Engineer And Photographer From Lombardia Currently Around the World #lu122131ph