Programs and the world / Html and forms
Start here01
The language02
- 01The basics
- 02Values and numbers
- 03Variables
- 04Calls and expressions
- 05Conditions and loops
- 06Strings
- 07Arrays and hashes
- 08Classes and records
- 09Traits and enums
- 10Blocks
- 11Methods
- 12Imports and exports
- 13Foreign files
- 14Failures
Programs and the world03
- 01Files, input and output
- 02JSON, time, URLs and random numbers
- 03Html and forms
- 04Templates: Ochre
- 05Built-in methods
Kiln04
03 Programs and the world / 03
Html and forms
Html values, how they're kept safe, and forms read into records.
Html is text that's safe in a page, and Form.decode turns what a browser sends into a record you can trust. Serving and HTTP requests are the http package's.
Html
Html is text that's safe to put in a page as it is. Templates make it, and escape every value they put in, so text never becomes HTML by accident.
When you have HTML you trust, Html.raw(text) says so. It's the one way to say it, which makes every such place easy to search for. String(page) gives the text back.
def shown(page: Html): String
return String(page)
end
const page = Html.raw("<h1>Hi</h1>")
print(page) # prints <h1>Hi</h1>
print(shown(page)) # prints <h1>Hi</h1>
const wrong = shown("<h1>Hi</h1>") # error: argument 'page' of shown must be Html, got a String; text becomes Html in a template, which escapes it, or with Html.raw(text) for HTML you trustA method that takes Html takes only that: a String is text, and goes into a page through a template, which escapes it. And Html can't become JSON, since it's for pages: send it as text, with String(page).
Forms
A browser sends a form's fields as text like email=ada%40example.com&age=36: in the body for a form that posts, or in the query for one that gets. Form.decode reads that text into a record, each field as its type says, the way JSON.decode reads JSON:
record SignUp
email: String
age: Int
newsletter: Boolean # a checkbox
nickname: String? # may be left blank
end
const form = try Form.decode("email=ada%40example.com&age=36&newsletter=on", as: SignUp)
print(form) # prints SignUp(email: "[email protected]", age: 36, newsletter: true, nickname: nil)The text comes from the request a server answers (requests are the http package's): its body for a form that posts, its query for one that gets.
How each field is read
| Field type | Reads |
|---|---|
String | the text as it is, even empty |
Int, Float | a whole number, or any number |
Boolean | on (a ticked checkbox), true or 1, and off, false or 0; left out, it's false, since an unticked checkbox isn't sent |
| an enum | the name of one of its values, like pro for Plan.pro |
T? | as T, or nil when left out or empty |
[T] | every value sent under its name, in order, each as T; [] when none is, as when no checkbox of the name is ticked |
A field that's left out takes its default, if it has one. An empty field counts as left out, except for a String. Fields the record doesn't have are left alone.
A name sent more than once is for an array: ticked checkboxes of one name, or a <select multiple>. Sent twice for a field that isn't one, it's a failure: 'size' was sent more than once; a field that takes several values is an array, like [String]. A list's filters, where several sizes and colours can be ticked, and the page may be left out:
enum Size
s
m
l
end
record Filters
sizes: [Size] key "size"
colours: [String] key "colour"
page: Int = 1
end
const ticked = try Form.decode("size=s&size=l&colour=Navy", as: Filters)
print(ticked) # prints Filters(sizes: [Size.s, Size.l], colours: ["Navy"], page: 1)
print(ticked.sizes[1]) # prints Size.l
const none = try Form.decode("", as: Filters)
print(none.sizes.length) # prints 0
Form.decode("page=1&page=2", as: Filters) rescue error
print(error.message) # prints 'page' was sent more than once; a field that takes several values is an array, like [String]
endkey "firstName" reads a field under another name, as it does for JSON. Only Strings, numbers, Booleans and enum values, and arrays of them, can come from a form, so a record with fields of any other type is an error before running.
When a field doesn't fit
A field that doesn't fit fails with kind invalid, and the message names the field: 'age' must be a whole number, got 'old'. A field that's left out with no default fails the same way: 'age' is missing.