Skip to content

feat(vite): auto-load Jinja2 templates from the Vite provider#135

Merged
bedus-creation merged 1 commit into
mainfrom
feature/vite-auto-load-templates
Jun 20, 2026
Merged

feat(vite): auto-load Jinja2 templates from the Vite provider#135
bedus-creation merged 1 commit into
mainfrom
feature/vite-auto-load-templates

Conversation

@bedus-creation

Copy link
Copy Markdown
Contributor

Summary

Reimplements the Vite template fix from the reverted PR #133, provider-only — no helper function, no Template class, no ambient/global request, no middleware or ContextVar.

Fresh apps previously got no template engine: ViteProvider.register_jinja_directives() only injected the vite() globals when a templates binding already existed, so a brand-new app had nothing to render with. The provider now binds a Jinja2Templates engine out of the box.

Changes

  • ViteConfig: add template: bool = True and templates_directory: str = "resources/templates".
  • ViteProvider.register(): when config.template is true and no templates binding exists, build Jinja2Templates(directory=base_path/templates_directory) and bind it. The jinja2/starlette.templating import is guarded and raises a clear install fastapi-startkit[vite] error. An existing templates binding always wins (backward-compatible). register_jinja_directives() is left unchanged.
  • Stubs: moved the published template stub to resources/templates/index.html so all published scaffolding is consistent under resources/ (resources/css, resources/js, resources/templates). The stub references {{ vite('resources/js/app.ts') }} and vite.config.ts input resources/js/app.ts — these line up.
  • Example: synced example/vite-app/config/vite.py with the new fields. The example keeps binding its own templates engine in FastAPIProvider (registered before ViteProvider), demonstrating the "existing binding wins" path; routes/web.py renders the standard way and is unchanged.

Rendering

Unchanged, standard Starlette/FastAPI:

app.make("templates").TemplateResponse(request, name, context)

No framework rendering helper is introduced.

Explicitly excluded (per task #272)

No vite/template.py, no template() helper, no Template class, no RequestContextMiddleware, no current_request ContextVar, no global/ambient request, and nothing new exported from vite/__init__.py.

Tests

tests/vite/test_template_binding.py covers: binds templates when enabled and none pre-bound; respects an existing binding; skips when template=False; vite()/vite_asset()/vite_react_refresh() present on templates.env.globals after boot; missing-jinja2 raises the clear install error.

  • uv run pytest tests/vite/ -v → 36 passed
  • uv run pytest --ignore=tests/masoniteorm/postgres --cov → 1553 passed, 7 skipped, total coverage 67.23% (above fail_under)
  • ruff format + ruff check clean

Do not merge — merging is the user's job.

Fresh apps got no template engine because ViteProvider only injected the
vite() globals when a "templates" binding already existed. The provider
now binds a Jinja2Templates engine out of the box, config-gated and
backward-compatible.

- Add ViteConfig.template (default True) and
  ViteConfig.templates_directory (default "resources/templates")
- ViteProvider.register() binds "templates" when enabled and none is
  pre-bound; an existing binding always wins. The jinja2 import is
  guarded with a clear "install fastapi-startkit[vite]" message
- Publish the template stub under resources/templates/index.html so all
  scaffolding stays consistent under resources/
- Sync the example app's config/vite.py with the new fields
- Cover binding, override, disabled, post-boot globals and the missing
  jinja2 error in tests/vite/

Rendering stays the standard Starlette way:
app.make("templates").TemplateResponse(request, name, context).
@bedus-creation bedus-creation merged commit ca3cbae into main Jun 20, 2026
3 checks passed
@bedus-creation bedus-creation deleted the feature/vite-auto-load-templates branch June 20, 2026 09:06
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.

1 participant