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

feat: Allow no template_config, enabling you to use litestar-vite with html frameworks other than Jinja2 #63

Merged
merged 2 commits into from
Dec 29, 2024

Conversation

cj
Copy link
Contributor

@cj cj commented Dec 27, 2024

Description

Allowing no template_config enables you to use litestar-vite with html frameworks other than Jinja2.

main.py

from typing import Any

from litestar import Litestar, MediaType, Request, Response, get
from litestar_vite import ViteConfig, VitePlugin
from weba import ui

from app import env
from app.components.layout import Layout

plugins: list[Any] = [
    VitePlugin(
        config=ViteConfig(
            hot_reload=env.debug,
            use_server_lifespan=env.debug,
            dev_mode=env.debug,
            port=env.vite_port,
        ),
    )
]


@get("/")
async def index(request: Request[Any, Any, Any]) -> Response[str]:
    async with Layout(request) as html:
        with html.main:
            ui.h1("Hello, World!")

    return Response(
        content=str(html),
        media_type=MediaType.HTML,
        status_code=200,
    )


app = Litestar(
    [index],
    openapi_config=None,
    plugins=plugins,
    debug=env.debug,
)

layout.py

from typing import Any

from litestar import Request
from litestar_vite.loader import render_asset_tag, render_hmr_client
from weba import Component, Tag, tag, ui

from app import env


class Layout(Component):
    html = "./layout.html"

    def __init__(self, request: Request[Any, Any, Any], title: str | None = None):
        self.title = f" | {title}" if title else ""
        self.context = {"request": request}

    @tag("title")
    def title_tag(self, t: Tag):
        t.string = self.title

    @tag("body")
    def body(self):
        pass

    @tag("body > main")
    def main(self):
        pass

    @tag("body > header")
    def header(self):
        pass

    @tag("body > footer")
    def footer(self):
        pass

    async def render(self):
        self._append_hmr()
        self._append_assets()

    def _append_hmr(self):
        if not env.debug:
            return

        self.body.append(ui.raw(render_hmr_client(self.context)))
        self.body.append(
            ui.script(
                src="/__reload__/static/reload-listener.js",
                data_worker_script_path="/__reload__/static/reload-worker.js",
                data_ws_path="/__reload__",
                defer=True,
                async_=True,
            )
        )

    def _append_assets(self):
        self.body.append(ui.raw(render_asset_tag(self.context, path=["app/scripts/main.js", "app/styles/main.css"])))

cj and others added 2 commits December 23, 2024 11:50
Allowing no template_config enables you to use litestar-vite on html frameworks other than `Jinja2`.
@cj cj changed the title Allow no template_config, enabling you to use litestar-vite with html frameworks other than Jinja2 feat: Allow no template_config, enabling you to use litestar-vite with html frameworks other than Jinja2 Dec 27, 2024
Copy link
Member

@cofin cofin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cofin cofin merged commit aa94f9e into litestar-org:main Dec 29, 2024
3 of 4 checks passed
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.

2 participants