All expressions in FastplateJS are wrapped by !(
expression )
except for the end-block expression: !/
In fastplate you can do these things:
- Insert value into text:
!(path.to.value)
This is the json path to your value from the passed data object. It can also be the iterator variable when inside a loop. Or it can be a function. (this function must have been defined in the data object passed to thefastplate(text, data)
call).- If it is a function you can pass arguments after the initial function name. EG:
!(load body)
. - You can also pass variables that are in scope to the function by using
$
. EG:!(renderAsName $user.name big)
. In this case the actual user name will be passed as the first argument and the literal string"big"
will be passed as the second. - The return value of the function is inserted into the text.
- If it is a function you can pass arguments after the initial function name. EG:
- If statement
!(if <value>)
. Note:==
and!=
are not supported. The value must be a boolean. (or it will be coerced into a boolean by javascript). The value can also be a function call ie!(if func arg1 arg2)
. To end the if block use!/
- If else:
!(if <value>) value is true !(else) value is false !/
. This will output either "value is true" or "value is false" depending on the value of the variable "value" - For loop
!(for el in <elements>)
. Elements must be an array, or iterator. To end the loop block use:!/
Currently syntax errors are not handled and will the fastplate(text, data)
call will throw an error.
Async: Fast plate is now async, and will return a promise. All user defined functions can be async aswell, but don't have to be.
renderPage.js
is provided to show how Fastplate can be used to make a simple, but powerful html templating library.
Make syre to update the path in line 10, to wherever your template files are stored. The load function will take a file location relative to that path and insert that files contents into the original file. Omit the .html
in the file location.
Todo:
- error handling and proper syntax errors
- Allow escaping of special characters.
- expression parser for the if statement
- if else block:
!(elif <value>)
......!/
. Replace by!(if v) v is true !(else) !(if v2) v2 is true !(else) nothing is true !/ !/