You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4c61ca5
Browse filesBrowse the repository at this point in the historyBrowse files
summary: Build a page URL with optional query parameters and a fragment.
6
+
parameters:
7
+
- name: file
8
+
type: TEXT
9
+
required: true
10
+
description: The path of the SQLPage file to link to, relative to the current file.
11
+
- name: parameters
12
+
type: JSON
13
+
description: A JSON object with the parameters to pass to the linked file.
14
+
- name: fragment
15
+
type: TEXT
16
+
description: An optional fragment (hash) to append to the URL to link to a specific section of the target
17
+
page.
18
+
---
19
+
20
+
Returns the URL of a SQLPage file with the given parameters.
21
+
22
+
### Example
23
+
24
+
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.
25
+
26
+
In `index.sql`, you can use the `link` function to generate the URL of the product page for each product.
27
+
28
+
```sql
29
+
select'list'as component;
30
+
select
31
+
name as title,
32
+
sqlpage.link('product', json_object('product_name', name)) as link
33
+
from products;
34
+
```
35
+
36
+
In `product.sql`, you can then use `$product_name` to get the name of the product from the URL parameter:
37
+
38
+
```sql
39
+
select'hero'as component, $product_name as title, product_info as description
40
+
from products
41
+
where name = $product_name;
42
+
```
43
+
44
+
> You could also have manually constructed the URL with `CONCAT('product?product_name=', name)`,
45
+
> but using `sqlpage.link` is better because it ensures that the URL is properly encoded.
46
+
> `sqlpage.link` will work even if the product name contains special characters like `&`, while `CONCAT(...)` would break the URL.
47
+
48
+
To link directly to a section, pass its `id` as the third argument, without the `#`:
49
+
50
+
```sql
51
+
selectsqlpage.link('product.sql', json_object('product_name', name), 'product_description') as link
summary: Build a link to this page with a query parameter changed or removed.
6
+
parameters:
7
+
- name: name
8
+
type: TEXT
9
+
required: true
10
+
description: The name of the variable to set.
11
+
- name: value
12
+
type: TEXT
13
+
description: The value to set the variable to.
14
+
---
15
+
16
+
Returns a URL that is the same as the current page's URL, but with a variable set to a new value.
17
+
18
+
This function is useful when you want to create a link that changes a parameter on the current page, while preserving other parameters.
19
+
20
+
It is equivalent to `sqlpage.link(sqlpage.path(), json_patch(sqlpage.variables('get'), json_object(name, value)))`.
21
+
22
+
### Example
23
+
24
+
Let's say you have a list of products, and you want to filter them by category. You can use `sqlpage.set_variable` to create links that change the category filter, without losing other potential filters (like a search query or a sort order).
0 commit comments