Skip to content

Commit 86f6736

Browse files
committed
Move HTTP and server-control references to Markdown
1 parent fdb69da commit 86f6736

28 files changed

Lines changed: 419 additions & 568 deletions
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
icon: cookie
3+
category: http-responses
4+
summary: Set a cookie in the visitor's browser.
5+
top:
6+
- name: domain
7+
type: TEXT
8+
description: The domain for which the cookie will be sent. If not specified, the cookie will be sent for
9+
all domains.
10+
- name: expires
11+
type: TIMESTAMP
12+
description: The date at which the cookie expires (either a timestamp or a date object). If not specified,
13+
the cookie will expire when the browser is closed.
14+
- name: http_only
15+
type: BOOLEAN
16+
description: Whether the cookie should only be accessible via HTTP and not via client-side scripts. If
17+
explicitly set to false, the cookie will be accessible via both HTTP and client-side scripts.
18+
- name: max_age
19+
type: INTEGER
20+
description: The maximum age of the cookie in seconds. number of seconds until the cookie expires. If both
21+
Expires and Max-Age are set, Max-Age has precedence.
22+
- name: name
23+
type: TEXT
24+
required: true
25+
description: The name of the cookie to set.
26+
- name: path
27+
type: TEXT
28+
description: The path for which the cookie will be sent. If not specified, the cookie will be sent for
29+
all paths.
30+
- name: remove
31+
type: BOOLEAN
32+
description: Set to TRUE to remove the cookie from the client browser. When specified, other parameters
33+
are ignored.
34+
- name: same_site
35+
type: TEXT
36+
description: Whether the cookie should only be sent for requests originating from the same site. See owasp.org/www-community/SameSite.
37+
`strict` is the recommended and default value, but you may want to set it to `lax` if you want your users
38+
to keep their session when they click on a link to your site from an external site.
39+
- name: secure
40+
type: BOOLEAN
41+
description: Whether the cookie should only be sent over a secure (HTTPS) connection. Defaults to TRUE.
42+
- name: value
43+
type: TEXT
44+
description: The value of the cookie to set.
45+
---
46+
47+
Sets a cookie in the client browser, used for session management and storing user-related information.
48+
49+
This component creates a single cookie. Since cookies need to be set before the response body is sent to the client,
50+
this component should be **placed at the top of the page**, before any other components that generate output.
51+
52+
After being set, a cookie can be accessed anywhere in your SQL code using the `sqlpage.cookie('cookie_name')` pseudo-function.
53+
54+
Note that if your site is accessed over HTTP (and not HTTPS), you have to set `false as secure` to force browsers to accept your cookies.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Set the cookie before producing any page content:
2+
3+
```sql
4+
SELECT 'cookie' as component,
5+
'username' as name,
6+
'John Doe' as value,
7+
FALSE AS secure; -- You can remove this if the site is served over HTTPS.
8+
```
9+
10+
On a later request, read the value the browser sent with [`sqlpage.cookie`](functions.sql?function=cookie). Setting a response cookie does not change the current request.
11+
12+
```sql
13+
SELECT 'text' as component,
14+
'Your name is ' || COALESCE(sqlpage.cookie('username'), 'not known to us') as contents;
15+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
icon: bug
3+
category: server-logic
4+
summary: Inspect the values passed to a component as JSON.
5+
---
6+
7+
Visualize any set of values as JSON.
8+
Can be used to display all the parameters passed to the component.
9+
Useful for debugging: just replace the name of the component you want to debug with 'debug', and see all the top-level and row-level parameters that are passed to it, and their types.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
rows:
3+
- component: debug
4+
my_top_level_property: true
5+
- x: y
6+
z: 42
7+
- a: b
8+
c: null
9+
---
10+
11+
At any time, if you are confused about what data you are passing to a component, just replace the component name with 'debug' to see all the parameters that are passed to it.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Show the result of a SQLPage function:
2+
3+
```sql
4+
select 'debug' as component, sqlpage.environment_variable('HOME');
5+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
icon: repeat
3+
category: server-logic
4+
summary: Render SQLPage components described by JSON data.
5+
top:
6+
- name: properties
7+
type: JSON
8+
description: A json object or array that contains the names and properties of other components.
9+
---
10+
11+
Renders other components, given their properties as JSON.
12+
If you are looking for a way to run FOR loops, to share similar code between pages of your site,
13+
or to render multiple components for every line returned by your SQL query, then this is the component to use
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
rows:
3+
- component: dynamic
4+
properties: '[{"component":"text"}, {"contents":"Hello, I am a dynamic component !", "italics":true}]'
5+
---
6+
7+
The dynamic component has a single top-level property named `properties`, but it can render any number of other components.
8+
Let's start with something simple to illustrate the logic. We'll render a `text` component with two row-level properties: `contents` and `italics`.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Static component data stored in `.json` files
2+
3+
You can also store the data for a component in a `.json` file, and load it using the `dynamic` component.
4+
5+
This is particularly useful to create a single [shell](?component=shell#component) defining the site's overall appearance and menus,
6+
and displaying it on all pages without duplicating its code.
7+
8+
The following will load the data for a `shell` component from a file named `shell.json`,
9+
using the [`sqlpage.read_file_as_text`](/functions.sql?function=read_file_as_text) function.
10+
11+
```sql
12+
SELECT 'dynamic' AS component, sqlpage.read_file_as_text('shell.json') AS properties;
13+
```
14+
15+
and `shell.json` would be placed at the website's root and contain the following:
16+
17+
```json
18+
{
19+
"component": "shell",
20+
"title": "SQLPage documentation",
21+
"link": "/",
22+
"menu_item": [
23+
{"link": "index.sql", "title": "Home"},
24+
{"title": "Community", "submenu": [
25+
{"link": "blog.sql", "title": "Blog"},
26+
{"link": "https//github.com/sqlpage/SQLPage/issues", "title": "Issues"},
27+
{"link": "https//github.com/sqlpage/SQLPage/discussions", "title": "Discussions"},
28+
{"link": "https//github.com/sqlpage/SQLPage", "title": "Github"}
29+
]}
30+
]
31+
}
32+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Including another SQL file
2+
3+
To avoid repeating the same code on multiple pages, you can include another SQL file using the `dynamic` component
4+
together with the [`sqlpage.run_sql`](/functions.sql?function=run_sql) function.
5+
6+
For instance, the following will include the file `shell.sql` at the top of the page,
7+
and pass it a `$title` variable to display the page title.
8+
9+
```sql
10+
SELECT 'dynamic' AS component,
11+
sqlpage.run_sql('shell.sql', json_object('title', 'SQLPage documentation')) AS properties;
12+
```
13+
14+
And `shell.sql` could contain the following:
15+
16+
```sql
17+
SELECT 'shell' AS component,
18+
COALESCE($title, 'Default title') AS title,
19+
'/my_icon.png' AS icon,
20+
'products' AS menu_item,
21+
'about' AS menu_item;
22+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Dynamic shell
2+
3+
On databases without a native JSON type (such as the default SQLite database),
4+
you can use the `dynamic` component to generate
5+
json data to pass to components that expect it.
6+
7+
This example generates a menu similar to the [shell example](?component=shell#component), but without using a native JSON type.
8+
9+
```sql
10+
SELECT 'dynamic' AS component, '
11+
{
12+
"component": "shell",
13+
"title": "SQLPage documentation",
14+
"link": "/",
15+
"menu_item": [
16+
{"link": "index.sql", "title": "Home"},
17+
{"title": "Community", "submenu": [
18+
{"link": "blog.sql", "title": "Blog"},
19+
{"link": "https//github.com/sqlpage/SQLPage/issues", "title": "Issues"},
20+
{"link": "https//github.com/sqlpage/SQLPage/discussions", "title": "Discussions"},
21+
{"link": "https//github.com/sqlpage/SQLPage", "title": "Github"}
22+
]}
23+
]
24+
}
25+
' AS properties
26+
```
27+
28+
[View the result of this query, as well as an example of how to generate a dynamic menu
29+
based on the database contents](./examples/dynamic_shell.sql).

0 commit comments

Comments
 (0)