Views
.
A view is a function from the model to a description of the screen. This page builds one out of smaller functions, shows a list, and shows part of it only sometimes.
Note:
In the loop, the view is the step from the model to the screen. It is step 1 of the tutorial, and question 4 of Thinking in Silk UI.
A menu
Live: this is the program below, running
Breakfast
Vegetarian only
Shakshuka
vegetarian
Eggs in tomato and pepper
$11.50
Smoked trout toast
Rye, dill, lemon
$13.00
Mushroom omelette
vegetarian
Three eggs, chives
$10.50
Chicken congee
Ginger, scallion, egg
$12.00
examples/src/core/menu.silk
Copy
//! A cafe menu: a view built from small functions, a list, and one condition.
import
core.look {
ACCENT
,
INK
,
LINE
,
MUTED
, button, rgb }
import
loom.css
import
loom.css { px,
Align
,
BorderStyle
}
import
loom.lib {
Document
,
Node
,
NoCommand
,
NoOutput
,
Program
,
Update
}
import
silk.core.ownership {
Copy
}
import
silk.mem.allocator {
Allocator
,
OutOfMemoryError
}
/// One thing on the menu.
pub
struct
Dish
{
pub
name: string<'
static
>,
pub
about: string<'
static
>,
/// The price, in cents.
pub
price: i64,
pub
vegetarian: bool, }
impl
Copy
for
Dish
{}
const
DISHES
: [
Dish
;
4
] = [
Dish
{ name:
"Shakshuka"
, about:
"Eggs in tomato and pepper"
, price:
1150
, vegetarian:
true
},
Dish
{ name:
"Smoked trout toast"
, about:
"Rye, dill, lemon"
, price:
1300
, vegetarian:
false
},
Dish
{ name:
"Mushroom omelette"
, about:
"Three eggs, chives"
, price:
1050
, vegetarian:
true
},
Dish
{ name:
"Chicken congee"
, about:
"Ginger, scallion, egg"
, price:
1200
, vegetarian:
false
}, ]
pub
struct
Cafe
{
pub
vegetarianOnly: bool, }
pub
union
Message
{
ClickedFilter
, }
type
Next
=
Update
<
Cafe
,
NoCommand
,
NoOutput
>
impl
Program
<
Message
,
NoCommand
>
for
Cafe
{
effect
fn
init
() ->
Next
!
OutOfMemoryError
? &
mut
Allocator
{
return
Next
.
of
(
Cafe
{ vegetarianOnly:
false
}) }
effect
fn
update
(
mut
model:
Cafe
, message:
Message
) ->
Next
!
OutOfMemoryError
? &
mut
Allocator
{
match
move
message {
Message
.
ClickedFilter
=> { model.vegetarianOnly = !model.vegetarianOnly } }
return
Next
.
of
(
move
model) }
fn
view
(model: &
Cafe
, 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.
alignItems
(
Align
.
Center
), css.
columnGap
(
px
(
12.0
))])], &[ h.
box
( &[h.
style
(&[css.
fontSize
(
px
(
20.0
)), css.
fontWeight
(
700.0
), css.
flexGrow
(
1.0
)])], &[h.
text
(
"Breakfast"
)], ),
button
( h,
if
model.vegetarianOnly {
"Show everything"
}
else
{
"Vegetarian only"
},
Message
.
ClickedFilter
, ), ], ), h.
column
( &[h.
testId
(
"dishes"
)], &[ h.
each
( &
DISHES
, |dish|
if
model.vegetarianOnly && !dish.vegetarian { h.
empty
() }
else
{
row
(h, dish) }, ), ], ), ], ) } }
/// One line of the menu.
fn
row
(h: &
Document
<
Message
>, dish: &
Dish
) ->
Node
{
return
h.
row
( &[ h.
style
( &[ css.
alignItems
(
Align
.
Center
), css.
columnGap
(
px
(
10.0
)), css.
paddingTop
(
px
(
10.0
)), css.
paddingBottom
(
px
(
10.0
)), css.
borderTopWidth
(
px
(
1.0
)), css.
borderStyle
(
BorderStyle
.
Solid
), css.
borderColor
(
rgb
(
LINE
)), ], ), ], &[ h.
column
( &[h.
style
(&[css.
flexGrow
(
1.0
), css.
rowGap
(
px
(
2.0
))])], &[ h.
row
( &[h.
style
(&[css.
alignItems
(
Align
.
Center
), css.
columnGap
(
px
(
8.0
))])], &[ h.
span
(&[h.
style
(&[css.
fontWeight
(
600.0
)])], &[h.
text
(dish.name)]),
if
dish.vegetarian {
badge
(h,
"vegetarian"
) }
else
{ h.
empty
() }, ], ), h.
span
(&[h.
style
(&[css.
color
(
rgb
(
MUTED
)), css.
fontSize
(
px
(
13.0
))])], &[h.
text
(dish.about)]), ], ),
price
(h, dish.price), ], ) }
/// A price in cents, as dollars: `1150` shows as `$11.50`.
fn
price
(h: &
Document
<
Message
>, cents: i64) ->
Node
{
let
rest = cents %
100
return
h.
row
( &[h.
style
(&[css.
fontVariantNumeric
(
"tabular-nums"
)])], &[ h.
text
(
"$"
), h.
number
(cents /
100
), h.
text
(
"."
),
if
rest <
10
{ h.
text
(
"0"
) }
else
{ h.
empty
() }, h.
number
(rest), ], ) }
fn
badge
(h: &
Document
<
Message
>, label: string) ->
Node
{
return
h.
span
( &[ h.
style
( &[ css.
fontSize
(
px
(
11.0
)), css.
paddingLeft
(
px
(
6.0
)), css.
paddingRight
(
px
(
6.0
)), css.
borderRadius
(
px
(
999.0
)), css.
borderWidth
(
px
(
1.0
)), css.
borderStyle
(
BorderStyle
.
Solid
), css.
borderColor
(
rgb
(
ACCENT
)), css.
color
(
rgb
(
ACCENT
)), ], ), ], &[h.
text
(label)], ) }
Elements
Every element is a call on
h
that takes a list of attributes and a list of children.
examples/src/core/menu.silk
Copy
fn
badge
(h: &
Document
<
Message
>, label: string) ->
Node
{
return
h.
span
( &[ h.
style
( &[ css.
fontSize
(
px
(
11.0
)), css.
paddingLeft
(
px
(
6.0
)), css.
paddingRight
(
px
(
6.0
)), css.
borderRadius
(
px
(
999.0
)), css.
borderWidth
(
px
(
1.0
)), css.
borderStyle
(
BorderStyle
.
Solid
), css.
borderColor
(
rgb
(
ACCENT
)), css.
color
(
rgb
(
ACCENT
)), ], ), ], &[h.
text
(label)], ) }
Call
What it is
h.box
A box. Its children stack, like a
div
h.row
A box that lays its children out in a row
h.column
A box that lays its children out in a column
h.span
A run of text inside a sentence, to style part of it
h.button
A box the keyboard can reach. Enter and Space click it
h.link
A link to a path of the program. See Routing
h.input
A text field. See Events and input
h.grid
A box laid out as a CSS grid
h.image
,
h.icon
A picture, by URL
h.row
and
h.column
are flex containers, so the layout properties you know from CSS apply:
css.alignItems
,
css.justifyContent
,
css.columnGap
,
css.flexGrow
.
Text
Text is a node too.
examples/src/core/menu.silk
Copy
/// A price in cents, as dollars: `1150` shows as `$11.50`.
fn
price
(h: &
Document
<
Message
>, cents: i64) ->
Node
{
let
rest = cents %
100
return
h.
row
( &[h.
style
(&[css.
fontVariantNumeric
(
"tabular-nums"
)])], &[ h.
text
(
"$"
), h.
number
(cents /
100
), h.
text
(
"."
),
if
rest <
10
{ h.
text
(
"0"
) }
else
{ h.
empty
() }, h.
number
(rest), ], ) }
•
h.text("Breakfast")
shows a string.
•
h.number(12)
shows a whole number.
•
h.decimal(3.14159, 2)
shows
3.14
.
The view never builds strings to show numbers. That keeps
view
free of allocation, and it means a changing number touches one text node and nothing else.
A list
examples/src/core/menu.silk
Copy
fn
view
(model: &
Cafe
, 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.
alignItems
(
Align
.
Center
), css.
columnGap
(
px
(
12.0
))])], &[ h.
box
( &[h.
style
(&[css.
fontSize
(
px
(
20.0
)), css.
fontWeight
(
700.0
), css.
flexGrow
(
1.0
)])], &[h.
text
(
"Breakfast"
)], ),
button
( h,
if
model.vegetarianOnly {
"Show everything"
}
else
{
"Vegetarian only"
},
Message
.
ClickedFilter
, ), ], ), h.
column
( &[h.
testId
(
"dishes"
)], &[ h.
each
( &
DISHES
, |dish|
if
model.vegetarianOnly && !dish.vegetarian { h.
empty
() }
else
{
row
(h, dish) }, ), ], ), ], ) }
h.each
calls a function for every item of a slice and puts the nodes it returns where the
h.each
is. The function is an ordinary lambda, so it can see
model
and
h
.
For a list whose items come and go, give each item a key, so that focus and scroll position stay with the right row: see Events and input.
Showing something sometimes
There is no template language, so a condition is an
if
.
Silk
Copy
if
dish.vegetarian {
badge
(h,
"vegetarian"
) }
else
{ h.
empty
() }
h.empty()
is nothing at all. It leaves no node behind. The filter in the menu works the same way: a dish that should not show becomes
h.empty()
.
When a branch needs several nodes and you do not want a box around them,
h.fragment(&[a, b])
puts them directly in the parent.
Splitting a view
row
,
price
and
badge
above are the whole technique. A view function takes
h
, takes what it needs to show, and returns a
Node
. It has no state of its own, so there is nothing to wire up and nothing to forget.
When a part of the screen needs its own model and messages, that is a different tool: Composition.
Naming nodes
Three attributes give a node a name that is not its look.
Attribute
For whom
h.label("Close")
People using a screen reader. The accessible name
h.accessRole(Role.List)
The same people. What kind of thing the node is
h.testId("dishes")
Tests and the inspector
The menu gives its list
h.testId("dishes")
, and a test finds it by that name. Testing shows how.
Recap
•
A view is a function from the model to nodes. It runs after every update.
•
Split a view into functions. That is the unit of reuse.
•
Show a list with
h.each
, and give rows that can move a key.
•
To show something only sometimes, use an
if
. To hide it, leave it out.
Next: Styling.