Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/official-site/content/components/cookie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
icon: cookie
category: http-responses
summary: Set a cookie in the visitor's browser.
top:
- name: domain
type: TEXT
description: The domain for which the cookie will be sent. If not specified, the cookie will be sent for
all domains.
- name: expires
type: TIMESTAMP
description: The date at which the cookie expires (either a timestamp or a date object). If not specified,
the cookie will expire when the browser is closed.
- name: http_only
type: BOOLEAN
description: Whether the cookie should only be accessible via HTTP and not via client-side scripts. If
explicitly set to false, the cookie will be accessible via both HTTP and client-side scripts.
- name: max_age
type: INTEGER
description: The maximum age of the cookie in seconds. number of seconds until the cookie expires. If both
Expires and Max-Age are set, Max-Age has precedence.
- name: name
type: TEXT
required: true
description: The name of the cookie to set.
- name: path
type: TEXT
description: The path for which the cookie will be sent. If not specified, the cookie will be sent for
all paths.
- name: remove
type: BOOLEAN
description: Set to TRUE to remove the cookie from the client browser. When specified, other parameters
are ignored.
- name: same_site
type: TEXT
description: Whether the cookie should only be sent for requests originating from the same site. See owasp.org/www-community/SameSite.
`strict` is the recommended and default value, but you may want to set it to `lax` if you want your users
to keep their session when they click on a link to your site from an external site.
- name: secure
type: BOOLEAN
description: Whether the cookie should only be sent over a secure (HTTPS) connection. Defaults to TRUE.
- name: value
type: TEXT
description: The value of the cookie to set.
---

Sets a cookie in the client browser, used for session management and storing user-related information.

This component creates a single cookie. Since cookies need to be set before the response body is sent to the client,
this component should be **placed at the top of the page**, before any other components that generate output.

After being set, a cookie can be accessed anywhere in your SQL code using the `sqlpage.cookie('cookie_name')` pseudo-function.

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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Set the cookie before producing any page content:

```sql
SELECT 'cookie' as component,
'username' as name,
'John Doe' as value,
FALSE AS secure; -- You can remove this if the site is served over HTTPS.
```

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.

```sql
SELECT 'text' as component,
'Your name is ' || COALESCE(sqlpage.cookie('username'), 'not known to us') as contents;
```
9 changes: 9 additions & 0 deletions examples/official-site/content/components/debug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
icon: bug
category: server-logic
summary: Inspect the values passed to a component as JSON.
---

Visualize any set of values as JSON.
Can be used to display all the parameters passed to the component.
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
rows:
- component: debug
my_top_level_property: true
- x: y
z: 42
- a: b
c: null
---

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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Show the result of a SQLPage function:

```sql
select 'debug' as component, sqlpage.environment_variable('HOME');
```
13 changes: 13 additions & 0 deletions examples/official-site/content/components/dynamic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
icon: repeat
category: server-logic
summary: Render SQLPage components described by JSON data.
top:
- name: properties
type: JSON
description: A json object or array that contains the names and properties of other components.
---

Renders other components, given their properties as JSON.
If you are looking for a way to run FOR loops, to share similar code between pages of your site,
or to render multiple components for every line returned by your SQL query, then this is the component to use
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
rows:
- component: dynamic
properties: '[{"component":"text"}, {"contents":"Hello, I am a dynamic component !", "italics":true}]'
---

The dynamic component has a single top-level property named `properties`, but it can render any number of other components.
Let's start with something simple to illustrate the logic. We'll render a `text` component with two row-level properties: `contents` and `italics`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Static component data stored in `.json` files

You can also store the data for a component in a `.json` file, and load it using the `dynamic` component.

This is particularly useful to create a single [shell](?component=shell#component) defining the site's overall appearance and menus,
and displaying it on all pages without duplicating its code.

The following will load the data for a `shell` component from a file named `shell.json`,
using the [`sqlpage.read_file_as_text`](/functions.sql?function=read_file_as_text) function.

```sql
SELECT 'dynamic' AS component, sqlpage.read_file_as_text('shell.json') AS properties;
```

and `shell.json` would be placed at the website's root and contain the following:

```json
{
"component": "shell",
"title": "SQLPage documentation",
"link": "/",
"menu_item": [
{"link": "index.sql", "title": "Home"},
{"title": "Community", "submenu": [
{"link": "blog.sql", "title": "Blog"},
{"link": "https//github.com/sqlpage/SQLPage/issues", "title": "Issues"},
{"link": "https//github.com/sqlpage/SQLPage/discussions", "title": "Discussions"},
{"link": "https//github.com/sqlpage/SQLPage", "title": "Github"}
]}
]
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Including another SQL file

To avoid repeating the same code on multiple pages, you can include another SQL file using the `dynamic` component
together with the [`sqlpage.run_sql`](/functions.sql?function=run_sql) function.

For instance, the following will include the file `shell.sql` at the top of the page,
and pass it a `$title` variable to display the page title.

```sql
SELECT 'dynamic' AS component,
sqlpage.run_sql('shell.sql', json_object('title', 'SQLPage documentation')) AS properties;
```

And `shell.sql` could contain the following:

```sql
SELECT 'shell' AS component,
COALESCE($title, 'Default title') AS title,
'/my_icon.png' AS icon,
'products' AS menu_item,
'about' AS menu_item;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Dynamic shell

On databases without a native JSON type (such as the default SQLite database),
you can use the `dynamic` component to generate
json data to pass to components that expect it.

This example generates a menu similar to the [shell example](?component=shell#component), but without using a native JSON type.

```sql
SELECT 'dynamic' AS component, '
{
"component": "shell",
"title": "SQLPage documentation",
"link": "/",
"menu_item": [
{"link": "index.sql", "title": "Home"},
{"title": "Community", "submenu": [
{"link": "blog.sql", "title": "Blog"},
{"link": "https//github.com/sqlpage/SQLPage/issues", "title": "Issues"},
{"link": "https//github.com/sqlpage/SQLPage/discussions", "title": "Discussions"},
{"link": "https//github.com/sqlpage/SQLPage", "title": "Github"}
]}
]
}
' AS properties
```

[View the result of this query, as well as an example of how to generate a dynamic menu
based on the database contents](./examples/dynamic_shell.sql).
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Dynamic tables

The `dynamic` component can be used to generate [tables](?component=table#component) with dynamic columns,
using [your database's JSON functions](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide).

For instance, let's say we have a table with three columns: user_id, name, and role.
We want to create a table where each row is a user, and each column is a role.
We will return a set of json objects that look like this: `{"name": "Alice", "admin": true, "editor": false, "viewer": true}`
```sql
SELECT 'table' AS component;
SELECT 'dynamic' AS component,
json_patch(
json_object('name', name),
json_object_agg(role, is_admin)
) AS properties
FROM users
GROUP BY name;
```
44 changes: 44 additions & 0 deletions examples/official-site/content/components/http_header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
icon: world-www
category: http-responses
summary: Set response headers for caching, content types, and more.
top:
- name: Access-Control-Allow-Origin
type: TEXT
description: Specifies which origins are allowed to access the resource in a cross-origin request, used
for implementing Cross-Origin Resource Sharing (CORS).
- name: Cache-Control
type: TEXT
description: Directives for how long the page should be cached by the browser. Set this to max-age=N to
keep the page in cache for N seconds.
- name: Content-Disposition
type: TEXT
description: Provides instructions on how the response content should be displayed or handled by the client,
such as inline or as an attachment.
- name: Location
type: TEXT
description: Specifies the URL to redirect the client to, usually used in 3xx redirection responses.
- name: Set-Cookie
type: TEXT
description: Sets a cookie in the client browser, used for session management and storing user-related
information.
---

An advanced component to set arbitrary HTTP headers: can be used to set a custom caching policy to your pages, or implement custom redirections, for example.
If you are a beginner, you probably don't need this component.

When used, this component has to be the first component in the page, because once the page is sent to the browser, it is too late to change the headers.

HTTP headers are additional pieces of information sent with responses to web requests that provide instructions
or metadata about the data being sent — for example,
setting cache control directives to control caching behavior
or specifying the content type of a response.

Any valid HTTP header name can be used as a top-level parameter for this component.
The examples shown here are just that, examples; and you can create any custom header
if needed simply by declaring it.

If your header's name contains a dash or any other special character,
you will have to use your database's quoting mechanism to declare it.
In standard SQL, you can use double quotes to quote identifiers (like "X-My-Header"),
in Microsoft SQL Server, you can use square brackets (like [X-My-Header]).
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
rows:
- component: http_header
Cache-Control: public, max-age=600, stale-while-revalidate=3600, stale-if-error=86400
---

Set cache control directives for caching behavior. In this example, the response can be cached by the browser
and served from the cache for up to 600 seconds (10 minutes) after it is first requested.
During that time, even if the cached response becomes stale (outdated), the browser can still use it (stale-while-revalidate)
for up to 3600 seconds (1 hour) while it retrieves a fresh copy from the server in the background.
If there is an error while trying to retrieve a fresh copy from the server,
the browser can continue to serve the stale response for up to 86400 seconds (24 hours) (stale-if-error) instead of showing an error page.
This caching behavior helps improve the performance and responsiveness of the website by reducing the number of requests made to the server
and allowing the browser to serve content from its cache when appropriate.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
rows:
- component: http_header
Location: /another-page.sql
---

Redirect the user to another page. In this example, the user is redirected to a file named another-page.sql at the root of the website. The current page will not be displayed at all.
This is useful in particular for content creation pages that contain only INSERT statements, because you can redirect the user to the page that lists the content after it has been created.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
rows:
- component: http_header
X-My-Header: my value
---

Set a custom non-standard header for the response. In this example, the response will include a custom header named X-My-Header with the value "my value".
26 changes: 26 additions & 0 deletions examples/official-site/content/components/log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
icon: logs
since: 0.37.1
category: server-logic
summary: Write a message to the SQLPage server log.
top:
- name: level
type: TEXT
description: How important the message is. One of 'trace', 'debug', 'info' (default), 'warn', 'error'.
Not case-sensitive. Controls the level shown in the logs.
- name: message
type: TEXT
required: true
description: The text to write to the server logs. It is printed when the page runs.
---

A component that writes messages to the server logs.
When a page runs, it prints your message to the terminal/console (standard error).
Use it to track what happens and troubleshoot issues.

## Reading log messages

- Running from a terminal (Linux, macOS, or Windows PowerShell/Command Prompt): they show up in the window.
- Docker: run `docker logs <container_name>`.
- Linux service (systemd): run `journalctl -u sqlpage`.
- This component's output is written to [standard error (stderr)](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)). SQLPage request access logs are separate and are written to standard output (stdout).
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
### Record a simple message

This writes "Hello, World!" to the server logs.

```sql
SELECT 'log' as component, 'Hello, World!' as message;
```

Example output:

```text
[2025-09-13T22:30:14.722Z INFO sqlpage::log from "x.sql" statement 1] Hello, World!
```

### Set the importance (level)

Choose how important the message is.

```sql
SELECT 'log' as component, 'error' as level, 'This is an error message' as message;
```

Example output:

```text
[2025-09-13T22:30:14.722Z ERROR sqlpage::log from "x.sql" statement 2] This is an error message
```

### Log dynamic information

Include variables like a username.

```sql
set username = 'user'

select 'log' as component,
'403 - failed for ' || coalesce($username, 'None') as message,
'error' as level;
```
Loading
Loading