Advanced: Parameterized Pages
Parameterized pages allow you to programmatically create webpages using your data.
Evidence takes a parameter you supply through a URL and uses it to populate a template markdown file, allowing you to create one page that can display data for many objects.
In our example, we are going to create parameterized pages for products, so you can display data for any product without having to create unique files.
#
Set up a product directoryCreate a product-performance
folder in pages/
and create the two .md files shown below:
- Square brackets indicate a parameterized template file
- You can navigate to localhost:3000/product-performance to test the new URL (but you will need content in the directory for anything to be displayed)
- If a valid parameter is supplied in the URL, Evidence will run the template file and populate it based on the parameter
- If no parameter is specified in the URL, Evidence will use
index.md
if it is in the directory
#
Create the product-performance/index.md contentFirst, let's create a loop to create a link for each item that Needful Things sells. This Will allow us to navigate to each page we'll create.
Our links page now looks like this:
#
Add the code below to [product].mdWe'll explain how this code works in a minute - for now, paste this into your file to get it working.
#
How the Template Code Works#
Access the ParameterThis variable accesses the parameter included in the URL:
- For example, if the URL ends in
/product/Typewriter
, this variable will show "Typewriter" - This variable is used in the header of the template page
#
Filter Queries with the ParameterQueries are included as normal in template files, but the results are filtered from within Evidence components.
You can apply a filter to a dataset by appending this code to the dataset name. This is a standard JavaScript method for filtering data. We plan to make this simpler in the future.
This means that the code will look in the dataset d
and include only those rows where the item column is equal to the page's parameter variable.
Adding this to a normal <BarChart/>
component gives us the following:
#
Follow a Product LinkClick on any of the links to visit the parameterized page for that product, which should look like this:
There we have it - a page for each product, but only one underlying file. This could be used to automatically create reports for divisions within companies, analysis of specific products, or anything else. The possibilities are almost endless.
For Needful Things - they can now work out how much inventory they will need.