From 478f73b93c37e5e32ce33eb1de963eeb23a5cc40 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 13 Mar 2025 12:18:53 +0800 Subject: [PATCH] Add basic template rendering --- .gitignore | 1 + Makefile | 7 +++++-- main.ha | 4 +--- templates/_head_common.htmpl | 3 +++ templates/index.htmpl | 14 ++++++++++++++ diff --git a/.gitignore b/.gitignore index e90b0c91bd05d6ddde2627d6a24494acce8ac04b..f61aa7686898291fe19d8ae83bc5c7edac1840ad 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /forge +/templates.ha diff --git a/Makefile b/Makefile index acb67a10ff54ba455d74083f423bee474f3e2163..c32770f50b7538c6b5e7cae055392c26dc0ff789 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,5 @@ -forge: main.ha - hare build -o $@ $^ +forge: main.ha templates.ha + hare build -o $@ . + +templates.ha: templates/*.htmpl + htmplgen -o $@ $^ diff --git a/main.ha b/main.ha index aa0364f5b5548f0ef73a6625ae8e481ff6c51f60..fc38f2586f2484875af849f922869300c49b8024 100644 --- a/main.ha +++ b/main.ha @@ -67,7 +67,5 @@ export fn handlereq(conn: io::handle, request: *http::request) (void | io::error | nomem) = { htmpl::write(conn, "HTTP/1.1 200 OK\r\n")?; htmpl::write(conn, "Content-Type: text/html\r\n\r\n")?; - htmpl::write(conn, "

")?; - htmpl::write_escape_html(conn, "Hey there <3")?; - htmpl::write(conn, "

")?; + tp_index(conn)?; }; diff --git a/templates/_head_common.htmpl b/templates/_head_common.htmpl new file mode 100644 index 0000000000000000000000000000000000000000..6fcfea1ae8a39eb5c04cd26e8b4ded3b0236cf19 --- /dev/null +++ b/templates/_head_common.htmpl @@ -0,0 +1,3 @@ +{{ define _tp_head_common(handle: io::handle, title: str = "Untitled") (void | io::error | nomem) }} +{{ title }} +{{ end }} diff --git a/templates/index.htmpl b/templates/index.htmpl new file mode 100644 index 0000000000000000000000000000000000000000..3562c86985329b338cbf9f02c244a15bdc9fea21 --- /dev/null +++ b/templates/index.htmpl @@ -0,0 +1,14 @@ +{{ define tp_index(handle: io::handle) (void | io::error | nomem) }} +{! + let title: str = "Test"; +!} + + + +{{ render _tp_head_common(handle, title) }} + + +

{{ title }}

+ + +{{ end }} -- 2.48.1