Skip to content

Jinja IntegrationΒ #165

Open
Open
@ZeroIntensity

Description

@ZeroIntensity

Proposal:

Jinja2 is quite popular when it comes to Python templating. It lets you integrate Python into your HTML with syntax that looks kind of like:

<p>Hello, {{ world }}</P>

Personally, I don't think this syntax is very pretty - but that's not really up for me to decide. Jinja support was added in #126. However, theoretically, with some black magic at runtime, Python code itself could be used to take Jinja variables. This could integrate nicely with #12, whenever that gets done.

I'm thinking it could look something like:

from view import get, jinja_vars
from view.components import *

scope = jinja_vars()

@get("/")
async def index():
    return p(f"Hello, {scope.world}!")

This could then be used from Jinja templates, somehow:

env.render_template("app/index.py")  # Very speculative, I'm not sure if Jinja would even allow this

This feature would allow for some nice, easy integration with tools that require Jinja, such as MkDocs. If doing it through render_template isn't possible, maybe Jinja has some way to call Python files, and view.py could mess around with the stack to get the scope?

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiThis has to do with the Python API (view)complexThis should only be looked at by someone who knows what they're doingdelayedDelayed due to another issuefeatureNew feature

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions