intermediate web-app

Putting It All Together

Let's build a bookmarks manager — add URLs with a title and see them listed. This tutorial ties together everything from this track: server setup, database reads and writes, forms, components, and listing records. Small enough to understand completely, real enough to be useful.

Part of: Frame framework

The server file declares plugins, imports helpers, and lists all routes:

import:
	"helpers.cln"

plugins:
	frame.server
	frame.ui
	frame.data

endpoints server:
	GET "/" :
		return http.respond(200, "text/html", render_home())

	GET "/add" :
		return http.respond(200, "text/html", render_add_form())

	POST "/add" :
		string title = json.get(req.form(), "title")
		string url = json.get(req.form(), "url")
		if title == "" or url == ""
			return http.respond(400, "text/html", render_add_form())
		string params = "[\"" + title + "\", \"" + url + "\"]"
		db.query("INSERT INTO bookmarks (title, url) VALUES (?, ?)", params)
		return http.respond(200, "text/html", render_home())
GET  /     → bookmarks list page
GET  /add  → add bookmark form
POST /add  → save and show updated list

The server file is thin — routes only. Three routes cover the full cycle: list, show form, save. Validate before inserting and return the form with 400 on error. Delegate rendering to helpers.cln to keep the server file readable.

The render_home() function in helpers.cln fetches and displays all bookmarks:

functions:
	string bookmark_item(string title, string url)
		html:
			<li class="bookmark-item">
				<a href="{url}" target="_blank">{title}</a>
			</li>

	string render_home()
		string cnt_res = db.query("SELECT CAST(COUNT(*) AS CHAR) as cnt FROM bookmarks", "[]")
		integer count = json.get(cnt_res, "data.rows.0.cnt").toInteger()
		string bm_res = db.query("SELECT title, url FROM bookmarks ORDER BY id DESC", "[]")
		string items = ""
		iterate i in 0 to count - 1
			string t = json.get(bm_res, "data.rows." + i.toString() + ".title")
			string u = json.get(bm_res, "data.rows." + i.toString() + ".url")
			items = items + bookmark_item(t, u)
		html:
			<!DOCTYPE html><html><body>
			<h1>My Bookmarks</h1>
			<a href="/add">+ Add Bookmark</a>
			<ul>{!items}</ul>
			</body></html>
Renders a page with all saved bookmarks as clickable links

render_home() applies the listing pattern from the previous tutorial: count, fetch, iterate, render. The bookmark_item component is called once per row. Separation of concerns: server.cln owns routing, helpers.cln owns rendering.

  • Keep server.cln thin — routes only, delegate rendering to helpers.cln
  • Three routes cover a full feature: list, show form, save
  • Validate form input before inserting — return the form with 400 on error
  • The same patterns repeat in every app: query, count, iterate, render
📚 → Book 1, Chapter 10
Copied!