Templates
workspace.templates maps keys to templates. A template is one of:
- a layer or locale template, merged into the layers and locales which reference it;
- a query template: SQL in
template, or a module whose default export renders SQL; requests name it with thetemplateparam; - a view template: HTML for the view API.
The core query templates, eg. wkt, location_get and field_stats, are always available; CUSTOM_TEMPLATES adds more.[1]
Sources
src loads a template when it is first used: https: from a URL, file: from the XYZ_CWD directory, cloudfront: and signer prefixes from their providers. ${NAME} in a src is replaced by the SRC_NAME environment variable. module: true compiles a JavaScript module from the src. An unreachable src adds an error to the composed object.
Composition
layer.template names a template, or is an object with a key, which the layer is merged into; the layer's own keys win. templates lists more templates merged into the layer in order. Locales compose the same way.[2]include_props and exclude_props limit which keys a template contributes. A template with a role is only merged for users with that role; see roles.
Query templates
Requests never send SQL. A query template's SQL uses ${name} for checked identifiers, eg. ${table}, ${filter}, ${viewport}, and %{name} for values, which are sent as query parameters.[3] dbs selects the connection; admin requires an admin user, and layer a layer param; statement_timeout limits the query in milliseconds.
Pitfalls
${...}is written into the SQL. Use%{...}for any value a user can choose.- A template's role hides it from users without the role, silently.
- The workspace schemas do not resolve templates. A layer whose
templateis missing or incomplete passes them, and is dropped or fails when XYZ composes the locale. Template contents are not checked; test the composed workspace with the admin-only/api/workspace/test.
Examples
A layer template reused by a locale, and a query template shown in a dataview.
{
"dbs": "DEVDB",
"templates": {
"countries_layer": {
"format": "geojson",
"table": "naturalearth.countries",
"geom": "geom_4326",
"srid": "4326",
"qID": "id"
},
"population_by_continent": {
"template": "SELECT continent, sum(pop_est) AS population FROM ${table} GROUP BY continent"
}
},
"locale": {
"layers": {
"countries": {
"template": "countries_layer",
"display": true,
"dataviews": {
"continents": {
"dataview": "Json",
"query": "population_by_continent",
"queryparams": { "table": true }
}
}
}
}
}
}