Skip to content

triplate, to:=safely template RDF languages with ease

Triplate lets you write templates for RDF query and data languages — SPARQL, Turtle, TriG, N-Triples — with a typed --- frontmatter header, loops and conditionals. Values are validated and escaped per their declared RDF type, so rendered output is injection-safe by construction, and the syntax is invalid in the host language, so an unprocessed template fails fast.

---
params {
service: iri
classes: iri[] min 1
limit: int optional
}
example demo "DBpedia people & orgs" {
service: <http://dbpedia.org/sparql>
classes: [ foaf:Person, foaf:Organization ]
limit: 10
}
---
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?s WHERE {
SERVICE ${service} {
{% for c in classes join "UNION" %}
{ ?s a ${c} }
{% endfor %}
}
}
{% if limit %}LIMIT ${limit}{% endif %}

Rendered with the demo example set:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?s WHERE {
SERVICE <http://dbpedia.org/sparql> {
{ ?s a foaf:Person } UNION { ?s a foaf:Organization }
}
}
LIMIT 10

Highlights

Zero dependencies

No runtime dependencies in any implementation — the TypeScript, Python and Java cores are pure-language. (Python’s optional term type uses rdflib only if you install the extra.)

Injection-safe by construction

Each value is validated and escaped per its declared RDF type. The only unescaped path is the explicit raw type.

One typed header

A single ------ frontmatter declares every input — type, cardinality (min/max), optionality — and is stripped from the output. The body stays clean ${ } references.

Host-agnostic & fail-fast

The ${ }, $"…", $<…>, {% … %} syntax and the leading --- are invalid in SPARQL/Turtle/TriG/N-Triples, so an unprocessed template can never run by mistake.

Loops & conditionals

{% for %} with join, the ${...x} spread operator, and {% if %} with type-directed conditions that make optional params usable.

Runnable examples

example blocks let an IDE preview and execute a query without hand-built input — fixtures for development, never production defaults.

Three reference implementations

TypeScript, Python and Java — verified byte-identical by a shared conformance suite.