ZeroIntensity / view.py

The Batteries-Detachable Web Framework
https://view.zintensity.dev
MIT License
205 stars 15 forks source link

Jinja Integration #165

Open ZeroIntensity opened 5 months ago

ZeroIntensity commented 5 months ago

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?