platelet
platelet is an HTML-first templating language.
This repo contains a Rust library for rendering platelet templates.
Why platelet?
Unlike moustache, handlebars, Jinja, Liquid and other templating languages, platelet's syntax is part of HTML (similar to Vue.js).
This has a few upsides:
- Higher level but less powerful than direct string manipulation
- The language is natural to read and write when working with HTML, and control flow follows HTML structure
- You can use your own HTML formatters and tooling
- HTML sanitization is more natural and straightforward
Example
You can explore live examples in the platelet playground
Template
{{ i }} × {{ n }} = {{ i * n }}
Context (input)
Output
1 × 7 = 7
2 × 7 = 14
3 × 7 = 21
Advanced example
Template templates/index.html
<!doctype html>
{{ title }}
Template templates/blogpost.html
{{blogpost.title}}
{{blogpost.date}}
Context (input)
Reference
| Syntax | Example | Details |
|---|---|---|
pl- directives |
pl-if, pl-for ... |
→ |
^ attributes |
^class, ^name ... |
→ |
{{ ... }} nodes |
{{ user.email }} |
→ |
| Expressions | 1 + users[i].score |
→ |
pl- Directives
HTML Attributes starting with a pl- are special. They are inspired by Vue's directives.
| attribute |
|---|
pl-if |
pl-else-if |
pl-else |
pl-for |
pl-html |
pl-src |
pl-slot |
pl-is |
Conditionals: pl-if, pl-else-if, pl-else
pl-if will only render this element if the expression is truthy
pl-else-if, used following a pl-if, will only render this element if the expression is truthy
pl-else, used following a pl-if or pl-else-if, will render this element otherwise
Add to cart
Add to cart (1 item left!)
Out of stock
If applied to a <template>, the template will be and the children rendered.
pl-for
Render element multiple times.
Allows 4 types of expression:
{{item.text}}
...
...
...
If applied to a <template>, the template will be removed and the children rendered.
pl-html
Set the innerHTML (without sanitization) to the given expression.
To set the outerHTML, apply this to a <template>.
pl-src
Given a path as a string, renders the template at the path and replaces the element.
Some text...
The attributes set on the element (regular attributes or rendered ^ attributes) are used as the context for rendering the template.
pl-slot
On a <slot>, pl-slot (with an optional name) marks the element as a slot, to be replaced.
On a <template>, pl-slot marks the template content as "what fills that slot".
index.html
...
...
layout.html
Output
...
...
pl-is
Replace the rendered element's tag with this element, given an expression that returns a string
{item}
^ Attributes
In an HTML attribute, prefixing the attribute with ^ allows you to set the value to a platelet expression.
If the expression is false or null, the attribute will not render.
This will render:
Text Nodes
In an HTML text node, {{variable}} inserts a (sanitized) string.
Welcome back {{user.name}}!
If the variable is not defined then an error is returned.
| Data type | Rendered as |
|---|---|
| Number | A number |
| String | A string |
| Boolean | true or false |
| Null | blank |
| Array | error |
| Object | error |
Expressions
All valid JSON values are valid platelet expressions. On top of this, single-quoted strings 'like this' are allowed for convenience when working with HTML.
Operators
On anything: ==, !=, &&, ||, !, x ? y : z
On numbers: + (addition)
On strings and arrays: + (concatenation)
On objects: + (shallow merge, right hand side overriding)
On numbers: -, *, /, % (mod)
On numbers: >, <, >=, <=
On objects arrays and strings, indexing operator a[b]
On objects, dot access: {"name": "angus"}.name
On arrays, objects and strings: len(z)
Expressions can be bracketed (9 + 3) / 2 == 6
Truthiness
false, [], "", {}, null are falsy.
All other values are truthy.