Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default jinja2 setup. #196

Closed
tomchristie opened this issue Nov 8, 2018 · 4 comments
Closed

Default jinja2 setup. #196

tomchristie opened this issue Nov 8, 2018 · 4 comments

Comments

@tomchristie
Copy link
Member

I think we probably ought to include a default Jinja2 setup out-of-the box, so...

from starlette.applications import Starlette
from starlette.responses import Response

app = Starlette(template_dir="templates")

@app.route("/")
async def homepage(request):
    template = app.get_template("index.html")
    content = template.render(request=request)
    return HTMLResponse(content=content)

We'd ensure that the support is strictly optional, and document what a more explicit setup would look like.

This would require:

  • Adding template_dir= to Starlette.__init__ and use it to setup a .template_env attribute.
  • Add Starlette.get_template()

See our example repo or our template docs for how the template setup should work.

Any feedback on this?
Anyone interested in tacking a crack at it?

@marcosschroh
Copy link
Contributor

Good one. Also I think the templates that we are using for DEBUG mode (500) and in the future 404 should be loaded by Jinja as well.

@Witekkq
Copy link
Contributor

Witekkq commented Nov 9, 2018

So currently the jinja2 package is needed to be installed even if I do not use tempate in my project at all?

@jessebraham
Copy link
Contributor

Jinja2 is an optional package and is only required for using the default template configuration

@tomchristie
Copy link
Member Author

@Witekkq - It's completely optional. If you set template_directory it'll set up a default environment for you, but otherwise you can either just omit that, or use a different templating engine or whatever.

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

No branches or pull requests

4 participants