sqlpage.link(file, parameters, fragment)
Introduced in SQLPage 0.25.0.
Returns the URL of a SQLPage file with the given parameters.
Example
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.
In index.sql
, you can use the link
function to generate the URL of the product page for each product.
select 'list' as component;
select
name as title,
sqlpage.link('product', json_object('product_name', name)) as link
from products;
In product.sql
, you can then use $product_name
to get the name of the product from the URL parameter:
select 'hero' as component, $product_name as title, product_info as description
from products
where name = $product_name;
You could also have manually constructed the URL with
CONCAT('product?product_name=', name)
, but usingsqlpage.link
is better because it ensures that the URL is properly encoded.sqlpage.link
will work even if the product name contains special characters like&
, whileCONCAT(...)
would break the URL.
Parameters
file
(TEXT): The name of the SQLPage file to link to.parameters
(JSON): The parameters to pass to the linked file.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 ifproduct.sql
containsselect 'text' as component, 'product_description' as id;
, you can link to the product description section withsqlpage.link('product.sql', json_object('product_name', name), 'product_description')
.