Access query and URL parameters
Problem
You need to access query or URL parameters on a Page or as part of a page Function.
Solution
Dynaboard natively supports parsing query parameters as well as the Express style (opens in a new tab) routing format for defining and accessing URL parameters.
For the following examples we’ll be using the URL: /users/1234?view=condensed
Accessing query parameters
- Use
$currentPage.query.view
to access the query parameter from inside of a page function or add interpolation double-curlies{{ $currentPage.query.view }}
to access the value when inside of a property field.
Accessing URL parameters
- Define a
Route
on the desired page. In this example we’ll use/users/:id
. - Use
$currentPage.params.id
to access theid
URL parameter from inside of a page function add interpolation double-curlies{{ $currentPage.params.id }}
to access the value when inside of a property field.
Discussion
- The URL of a page and the name of a page do not have to match. In the examples above, the name of the page could have been
userDetail
with a route of/users/:id
. - It’s possible to access the page’s URL and query string parameters by referencing the page name directly e.g.
users.params.id
rather than using$currentPage.params.id
. However, it’s not recommended for two reasons: 1) only the$currentPage
can have properly defined parameters and 2) the page variable name will change when the name of the page changes, leading to brittle application code.
Known Limitations
- Dynaboard uses a much newer version of the
path-to-regexp
library than the current4.X.X
version of Express, so not all route examples from the Express documentation work in Dynaboard 1-to-1. For more information on compatibility please refer to the path-to-regexp documentation (opens in a new tab).