Skip to content

Add Tera::get_referenced_templates - #1059

Open
patrick-immometrica wants to merge 1 commit into
Keats:masterfrom
patrick-immometrica:feat/template-dependencies
Open

patrick-immometrica wants to merge 1 commit into
Keats:masterfrom
patrick-immometrica:feat/template-dependencies

Conversation

@patrick-immometrica

Copy link
Copy Markdown
Contributor

Closes #1044

Adds Tera::get_referenced_templates(name). It returns the names of the templates a template references through extends and include, plus the ones those templates reference in turn. It sits next to get_template_variables and works the same way.

I went with the full set of templates instead of only the direct references that jinja2.meta.find_referenced_templates returns. For dependency tracking you want to know that page.html depends on nav.html when base.html is the template that includes it. Tera already resolves the parent chain, so this is a short walk over parents and include_calls.

Differences from the Jinja function:

  • No None entries. Tera only accepts string literals in extends and include, so every name is known.
  • No imports, because Tera 2 has none.
  • Names come back after fallback prefixes are resolved, so you get the name the template was added under.
  • The template itself is not part of the result.

Like get_template_variables, it reports every reference it finds, including ones inside an if or inside a block that a child overrides. It also reports the parent of an included template that uses extends. I'd rather over-report than have a site generator skip a rebuild it needed.

I added a test in tests/introspection.rs that covers an extends chain with includes at each level, nested includes, a fallback prefix, an included template that extends another one, a template with no references, and an unknown template name.

Returns the templates a template references through extends and include,
including the ones referenced by those templates, for dependency tracking.

Closes Keats#1044

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

Plans for an API like Jinja2's find_referenced_templates?

1 participant