Skip to content

Commit

Permalink
docs: Document phases
Browse files Browse the repository at this point in the history
Issue-7: #7
  • Loading branch information
pawamoy committed Jan 29, 2025
1 parent ea4567b commit b508b74
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,17 +193,24 @@ class ContextUpdater(ContextHook):
└── 📄 {{module_name}}.py.jinja
```

## How does it work?

Beware the ugly hack!
Upon loading the special *loader* extension,
the function responsible for importing
a Python object using its dotted-path (a string)
is patched in the `jinja.environment` module,
where it's used to load extensions.
The patched version adds support
for loading extensions using relative file paths.
The file system loader of the Jinja environment
and its `searchpaths` attribute are used to
find the local clone of the template and determine
the absolute path of the extensions to load.
You can do many more things with a context hook,
like downloading data from external resources
to include it in the context, etc.

> [!TIP]
> Context hooks run in every Copier rendering phase.**
> During project generation or project updates, Copier passes
> through several rendering phases: when prompting (questions / answers),
> when rendering files, when running tasks, and when running migrations.
>
> By default, a context hook runs during all these phases,
> possibly multiple times, for example once per prompted question,
> or once per rendered file. The task of running the hook only once,
> or during a specific phase only, is left to you.
>
> To run only once, you can use caching within your class
> (for example by storing computed values in class variables).
>
> To run during a specific phase only, you can check the value
> of `context["_copier_phase"]` (Copier 9.5+). Many other key-value pairs
> can be found in the context that you might find useful.

0 comments on commit b508b74

Please sign in to comment.