aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-03-13 12:18:53 +0800
committerRunxi Yu <me@runxiyu.org>2025-03-13 12:18:53 +0800
commit478f73b93c37e5e32ce33eb1de963eeb23a5cc40 (patch)
tree2b6496ded87c220ef0e5d5061806ee1d66683a27
parentBegin to use hare-htmpl (diff)
downloadforge-478f73b93c37e5e32ce33eb1de963eeb23a5cc40.tar.gz
forge-478f73b93c37e5e32ce33eb1de963eeb23a5cc40.tar.zst
forge-478f73b93c37e5e32ce33eb1de963eeb23a5cc40.zip
Add basic template rendering
Diffstat (limited to '')
-rw-r--r--.gitignore1
-rw-r--r--Makefile7
-rw-r--r--main.ha4
-rw-r--r--templates/_head_common.htmpl3
-rw-r--r--templates/index.htmpl14
5 files changed, 24 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index e90b0c9..f61aa76 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
/forge
+/templates.ha
diff --git a/Makefile b/Makefile
index acb67a1..c32770f 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 aa0364f..fc38f25 100644
--- a/main.ha
+++ b/main.ha
@@ -67,7 +67,5 @@ export fn main() void = {
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, "<!DOCTYPE html><html><body><h1>")?;
- htmpl::write_escape_html(conn, "Hey there <3")?;
- htmpl::write(conn, "</h1></body></html>")?;
+ tp_index(conn)?;
};
diff --git a/templates/_head_common.htmpl b/templates/_head_common.htmpl
new file mode 100644
index 0000000..6fcfea1
--- /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>{{ title }}</title>
+{{ end }}
diff --git a/templates/index.htmpl b/templates/index.htmpl
new file mode 100644
index 0000000..3562c86
--- /dev/null
+++ b/templates/index.htmpl
@@ -0,0 +1,14 @@
+{{ define tp_index(handle: io::handle) (void | io::error | nomem) }}
+{!
+ let title: str = "Test";
+!}
+<!DOCTYPE html>
+<html lang="en">
+<head>
+{{ render _tp_head_common(handle, title) }}
+</head>
+<body>
+<h1>{{ title }}</h1>
+</body>
+</html>
+{{ end }}