Skip to content

Commit 611f8b1

Browse files
committed
document sqlpage.link
1 parent 29fa14c commit 611f8b1

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
INSERT INTO
2+
sqlpage_functions (
3+
"name",
4+
"introduced_in_version",
5+
"icon",
6+
"description_md"
7+
)
8+
VALUES
9+
(
10+
'link',
11+
'0.25.0',
12+
'link',
13+
'Returns the URL of a SQLPage file with the given parameters.
14+
15+
### Example
16+
17+
Let''s say you have a database of products, and you want the main page (`index.sql`) to link to the page of each product (`product.sql`) with the product name as a parameter.
18+
19+
In `index.sql`, you can use the `link` function to generate the URL of the product page for each product:
20+
21+
```sql
22+
select ''list'' as component;
23+
select
24+
name as title,
25+
sqlpage.link(''product.sql'', json_object(''product_name'', name)) as link;
26+
```
27+
28+
Using `sqlpage.link` is better than manually constructing the URL with `CONCAT(''product.sql?product_name='', name)`, because it ensures that the URL is properly encoded.
29+
The former works when the product name contains special characters like `&`, while the latter would break the URL.
30+
31+
In `product.sql`, you can then use `$product_name` to get the name of the product from the URL parameter:
32+
33+
```sql
34+
select ''text'' as component;
35+
select CONCAT(''Product: '', $product_name) as contents;
36+
```
37+
38+
### Parameters
39+
- `file` (TEXT): The name of the SQLPage file to link to.
40+
- `parameters` (JSON): The parameters to pass to the linked file.
41+
- `fragment` (TEXT): An optional fragment (hash) to append to the URL. This is useful for linking to a specific section of a page. For instance if `product.sql` contains `select ''text'' as component, ''product_description'' as id;`, you can link to the product description section with `sqlpage.link(''product.sql'', json_object(''product_name'', name), ''product_description'')`.
42+
'
43+
);
44+
45+
INSERT INTO
46+
sqlpage_function_parameters (
47+
"function",
48+
"index",
49+
"name",
50+
"description_md",
51+
"type"
52+
)
53+
VALUES
54+
(
55+
'link',
56+
1,
57+
'file',
58+
'The path of the SQLPage file to link to, relative to the current file.',
59+
'TEXT'
60+
),
61+
(
62+
'link',
63+
2,
64+
'parameters',
65+
'A JSON object with the parameters to pass to the linked file.',
66+
'JSON'
67+
),
68+
(
69+
'link',
70+
3,
71+
'fragment',
72+
'An optional fragment (hash) to append to the URL to link to a specific section of the target page.',
73+
'TEXT'
74+
);

0 commit comments

Comments
 (0)