Lesson 25 of 56 · Layout language
Layout on the web
When targeting the web, Salam's layout DSL compiles to HTML, CSS, and (when needed) JavaScript. This lesson focuses on web-specific output and runtime behavior: building, browser/DOM interactivity, and serving pages.
Building web output
salam build page.salam --platform=web produces page.html and page.css, plus page.js when scripts/events are present. Use --inline for a single self-contained HTML file. With a folder of pages (e.g. salam build pages --platform=web) each page gets its own HTML and they can share a common style.css. With no file name, the tool typically builds main.salam if present, otherwise layout pages in the folder.
Styles are extracted into generated classes/rules in the stylesheet so HTML stays compact and reusable.
Browser events and DOM
Event attributes such as click, submit, input, change, etc. become interactive behavior in the browser. Salam handlers attached to events are compiled into the page script for web builds. In handlers you can work with the DOM (e.g. dom.ById/dom.Query) as covered in the events lessons.
See Mouse events through State, the DOM and timers for the full browser event/DOM model.
Parameterized layouts and runtime rendering
Parameterized layouts can render HTML at runtime with request-specific data, which is useful for Salam web servers. Inserted parameter text is escaped for safety. For sensitive/structured values (URLs, colors, lengths, etc.) validate/parse them appropriately before use.
page Card(name: str, score: int):
title = "Card " + name
heading:
size = 2
content = name
end
if score >= 50:
paragraph:
content = "pass"
end
else:
paragraph:
content = "fail"
end
end
end
func from(html: str, mark: str): str:
start := html.Find(mark)
ret html.Substr(start, html.Len() - start)
end
func main:
println from(Card("Ali", 70), "<body>")
end<body><h2>Ali</h2><p>pass</p></body></html>
import net.http
import net.router
page Greeting(name: str):
title = "Hello " + name
heading:
size = 1
content = "Hello, " + name
end
paragraph:
content = "This page was rendered on the server."
end
end
func home(ctx: i64):
mut name := http.Query(ctx, "name")
if name == "":
name = "guest"
end
http.Html(ctx, Greeting(name))
end
func main:
mut r := router.NewRouter()
router.Get(r, "/", home)
mut s := http.NewServer(8080)
http.Use(s, r)
http.Run(s)
endOptional and null values
A value written null, or a value of an optional type (str?, int?, web.Url?), leaves the attribute not set instead of writing an empty one. On the web there is no HTML attribute, no style declaration and no text; when the value changes between empty and present the attribute is added and removed again. The word "null" in quotes is just text.
struct Work:
pub title: str?
pub note: str?
end
page Row(w: Work):
link: url = "/x" title = w.note content = w.title end
end
page:
paragraph: content = "Works" end
endA map lookup such as works().Get(id) gives the zero value for a missing key, so it is not optional by itself: make the field str? to be able to say it is missing.
A required attribute, such as url on link and image, cannot be null. A literal null or an optional value there is error E300. Give it a value with .UnwrapOr(default) or .Unwrap(), or use .value inside if x.IsSome():. Values of a plain type are not affected.
Serving and web projects
Built files can be hosted on any static web server, or served locally/production with salam serve. Salam programs can also return rendered HTML using networking packages such as net.http and net.router.
component Badge(text: str):
span:
content = text
background = "#fde68a"
end
end
page Inbox(user: str, subjects: Vector<str>):
title = "Inbox of " + user
heading:
size = 1
content = "Hello, " + user
end
if subjects.IsEmpty():
paragraph:
content = "No new messages"
end
else:
paragraph:
content = "Unread: " + subjects.Len()
end
list:
each i, s in subjects:
item:
key = i
content = (i + 1) + ". " + s
end
end
end
end
end
func from(html: str, mark: str): str:
start := html.Find(mark)
ret html.Substr(start, html.Len() - start)
end
func main:
mut subjects := Vector {} as Vector<str>
defer subjects.Free()
subjects.Push("Meeting at 10")
subjects.Push("<b>FREE</b> prize & more")
println from(Inbox("Sara", subjects), "<body>")
println from(Badge("new"), "<span")
end<body><h1>Hello, Sara</h1><p>Unread: 2</p><ul><li data-key="0">1. Meeting at 10</li><li data-key="1">2. <b>FREE</b> prize & more</li></ul></body></html> <span class="badge-a">new</span>
Notes
Browser features (forms behavior, media APIs, scrolling, clipboard, storage, etc.) are web-environment specific.
Interactive pages rely on the page script running in the browser.
For browserless native windows, see Layout in native apps.