Routing
.
A program with more than one page keeps the page it is on in its model, like everything else. The address is just another thing that can change, and a change is just another message.
Note:
Routing uses nothing new: the address is a subscription, the route is a field of the model, and the view is an if
on it. Two pages
examples/src/core/pages.silk
Copy
//! Two pages and a page that is not there: the address is state, like anything else.
import
core.look { ACCENT
, INK
, MUTED
, button, rgb }
import
loom.css
import
loom.css { px }
import
loom.lib { Document
, Node
, NoCommand
, NoOutput
, Program
, Subscriptions
, Update
}
import
silk.mem.allocator { Allocator
, OutOfMemoryError
}
/// Where the program is.
pub
enum
Route
{
Home
,
About
,
NotFound
,
}
/// The route of a path. Parsing is a plain function, so it is easy to test.
pub
fn
routeOf
(path: string) -> Route
{
if
path == "/"
{
return
Route
.Home
}
if
path == "/about"
{
return
Route
.About
}
return
Route
.NotFound
}
pub
struct
Pages
{
pub
route: Route
,
}
pub
union
Message
{
ChangedLocation
{ pub
route: Route
},
ClickedBackHome
,
}
type
Next
= Update
<Pages
, NoCommand
, NoOutput
>
impl
Program
<Message
, NoCommand
> for
Pages
{
effect
fn
init
() -> Next
! OutOfMemoryError
? &mut
Allocator
{
return
Next
.of
(Pages
{ route: Route
.Home
})
}
effect
fn
update
(mut
model: Pages
, message: Message
) -> Next
! OutOfMemoryError
? &mut
Allocator
{
match
move
message {
Message
.ChangedLocation
{ route } => {
model.route = route
}
Message
.ClickedBackHome
=> {
// An update may move the location itself. The subscription below hears of it next.
return
run
Next
.of
(move
model).navigating
("/"
)
}
}
return
Next
.of
(move
model)
}
fn
view
(model: &Pages
, h: &Document
<Message
>) -> Node
{
return
h.column
(
&[h.style
(&[css.padding
(px
(16.0
)), css.rowGap
(px
(12.0
)), css.color
(rgb
(INK
))])],
&[
h.row
(
&[h.style
(&[css.columnGap
(px
(16.0
))])],
&[tab
(h, "/"
, "Home"
, model.route == Route
.Home
), tab
(h, "/about"
, "About"
, model.route == Route
.About
)],
),
match
model.route {
Route
.Home
=> h.text
("This is the home page."
)
Route
.About
=> h.text
("This page is about the program."
)
Route
.NotFound
=> h.column
(
&[h.style
(&[css.rowGap
(px
(8.0
)), css.color
(rgb
(MUTED
))])],
&[h.text
("There is no page here."
), button
(h, "Back home"
, Message
.ClickedBackHome
)],
)
},
],
)
}
fn
subscriptions
(model: &Pages
, subs: &mut
Subscriptions
<Message
>) -> () {
subs.location
(1
, |path| Message
.ChangedLocation
{ route: routeOf
(path) })
}
}
/// A link to a page, marked when it is the page shown.
fn
tab
(h: &Document
<Message
>, path: string, label: string, current: bool) -> Node
{
return
h.link
(
path,
&[
h.selected
(current),
h.style
(
&[
css.color
(
if
current {
rgb
(ACCENT
)
} else
{
rgb
(INK
)
},
),
css.fontWeight
(
if
current {
700.0
} else
{
400.0
},
),
],
),
],
&[h.text
(label)],
)
}This one has no live demo on the page, because its links would take you away from here. You are using the real thing instead: this site is a Silk UI program, and every link in its sidebar is an
h.link
. Watch the address bar as you click around. No page loads.Where am I
examples/src/core/pages.silk
Copy
fn
subscriptions
(model: &Pages
, subs: &mut
Subscriptions
<Message
>) -> () {
subs.location
(1
, |path| Message
.ChangedLocation
{ route: routeOf
(path) })
}subs.location
sends a message with the path whenever the location changes. It also sends one at the very start, before the first render, with the path the program opened at. So a program that opens at /about
shows the about page first, with no flash of the home page.examples/src/core/pages.silk
Copy
/// The route of a path. Parsing is a plain function, so it is easy to test.
pub
fn
routeOf
(path: string) -> Route
{
if
path == "/"
{
return
Route
.Home
}
if
path == "/about"
{
return
Route
.About
}
return
Route
.NotFound
}Turning a path into a route is an ordinary function. Make the route an enum or a union, so that
view
can match
on it and the compiler tells you when you forgot a page.Links
examples/src/core/pages.silk
Copy
/// A link to a page, marked when it is the page shown.
fn
tab
(h: &Document
<Message
>, path: string, label: string, current: bool) -> Node
{
return
h.link
(
path,
&[
h.selected
(current),
h.style
(
&[
css.color
(
if
current {
rgb
(ACCENT
)
} else
{
rgb
(INK
)
},
),
css.fontWeight
(
if
current {
700.0
} else
{
400.0
},
),
],
),
],
&[h.text
(label)],
)
}h.link(path, attributes, children)
is a link to a path of your program. A click moves the location and nothing else. The subscription hears about it and update
puts the new route in the model.In a page, a link is a real
a
element with a real href
: it can be opened in a new tab, copied, and followed by a crawler. The browser's back and forward buttons move the program too.Moving from
update
examples/src/core/pages.silk
Copy
effect
fn
update
(mut
model: Pages
, message: Message
) -> Next
! OutOfMemoryError
? &mut
Allocator
{
match
move
message {
Message
.ChangedLocation
{ route } => {
model.route = route
}
Message
.ClickedBackHome
=> {
// An update may move the location itself. The subscription below hears of it next.
return
run
Next
.of
(move
model).navigating
("/"
)
}
}
return
Next
.of
(move
model)
}Sometimes the program decides to go somewhere: after a form is saved, or when a button is not a link.
navigating(path)
on what update
returns moves the location, exactly as a click on a link to that path would. The location subscription hears of it next.Testing it
A test moves the location itself and checks the model.
tests/src/core.silk
Copy
effect
fn
pagesBody
() -> ()
! AssertionError
<'static
> | TreeError
| OutOfMemoryError
? &mut
Allocator
{
run
Testing
.expect
(routeOf
("/about"
) == Route
.About
, "a path has a route"
)
run
Testing
.expect
(routeOf
("/nowhere"
) == Route
.NotFound
, "or it has none"
)
let
mut
page = Html
.make
()
let
mut
app = run
program.make<Pages
, PagesMessage
, NoCommand
>()
run
app.start
(&mut
page, 0.0
)
run
drain
(&mut
app, &mut
page)
run
navigate
(&mut
app, &mut
page, "/nowhere"
)
run
Testing
.expect
(app.model.value.route == Route
.NotFound
, "the program hears a move"
)
app.send
(PagesMessage
.ClickedBackHome
)
run
drain
(&mut
app, &mut
page)
run
Testing
.expect
(app.location
() == "/"
, "an update can move the location"
)
run
Testing
.expect
(app.model.value.route == Route
.Home
, "and hears of its own move"
)
}Recap
•
The current route is state, so it is in the model.
•
subs.location
turns every change of the address into a message.•
h.link
moves the address without loading a page, and update
can move it too.•
A function from a path to a route is easy to test alone.
Next: Composition.