Using a template to display data, we can make use of some template functions to improve the presentation.
So far we've used {{ . }}
to insert textual representations of the parameters,
and so had to wrap them in <pre> tags to ensure they displayed properly.
Now we can use the template-based render
function to render data as an HTML table:
import("http")
route := {method:str path:str, relgen:str}{
["", "/", "index_handler"]
}
index_handler := {
method:str, path:str,
response:tuple{header:{k:str, seq:int, v:str}, status:int, body:str},
} begin
response := http.response{*,
body:=execute_template("b1", "hello1", tuple{mydata:=S})
}
yield
end
main := {} begin
template_register(batch_name:="b1", template:=dee(
template_name:="hello1",
template_content:=
"<!doctype html><h1>Data Server</h1> {{ render .mydata }}"
))
res := http.serve(route:=route)
print(res)
end
Notice the added render
inside the template bracket-pairs. In a browser, hitting http://localhost:8080 would now give:
Data Server
SNO | SNAME | STATUS | CITY |
---|---|---|---|
S1 | Smith | 20 | London |
S2 | Jones | 10 | Paris |
S3 | Blake | 30 | Paris |
S4 | Clark | 20 | London |
S5 | Adams | 30 | Athens |
This HTML table is a great improvement, since it will display correctly on a variety of devices and can be styled using CSS, etc.
However, we're still rendering a relvar, which has no left-to-right nor top-to-bottom order, so the render order is arbitrary and could change each time the page is rendered.
To fix that, we should instead render Ra tables. A Ra table is an array of tuples, and as such does have a top-to-bottom ordering and, when rendered, each tuple has a specific left-to-right ordering.
order
One way to get a Ra table is to use the Ra order
function, which takes a relation expression and a list of attributes to order by.
It isn't a relational operation, however: the output table is no longer a relation and so relational operations such as project and filter are no longer applicable.
It should be considered a final presentation function to convert from relation-land to fixed-display-land (a bit like presenting a date in a string format suited to a given locale).
order(S, {SNO})
SNO | SNAME | STATUS | CITY |
---|---|---|---|
S1 | Smith | 20 | London |
S2 | Jones | 10 | Paris |
S3 | Blake | 30 | Paris |
S4 | Clark | 20 | London |
S5 | Adams | 30 | Athens |
A reverse ordering can be done by prefixing an attribute with -
, e.g.
order(S, {-CITY, -SNO})
SNO | SNAME | STATUS | CITY |
---|---|---|---|
S3 | Blake | 30 | Paris |
S2 | Jones | 10 | Paris |
S4 | Clark | 20 | London |
S1 | Smith | 20 | London |
S5 | Adams | 30 | Athens |
And an extra parameter can be given to limit the number of rows (after ordering), e.g.
order(S, {SNO}, 3)
SNO | SNAME | STATUS | CITY |
---|---|---|---|
S1 | Smith | 20 | London |
S2 | Jones | 10 | Paris |
S3 | Blake | 30 | Paris |
Again, these are HTML tables, and now we control the top-to-bottom ordering. The left-to-right ordering is also now fixed, at least, but notice that we didn't say what it was. To do that, we'll need to either:
- Use the Ra
paginate
function to specify not only the top-to-bottom order but also a left-to-right attribute order
OR
- Iterate over each tuple's attributes in a given order in the template
Next we'll see how the paginate
function can help.