Sprightly
💡 A very light-weight JS template engine
Install / Use
/learn @obadakhalili/SprightlyREADME
Template engines like EJS and Handlebars are good. But sometimes, you don't want all the code and the complexity that comes with them. You only want a simple variables injection with components support. Well, that is what Sprightly is for, it is a one-filer that lets you use those tiny features without having to include or think about anything else.
Guide and APIs
When you import * as APIs from "sprightly" you get access to the following APIs:
-
sprightly.The template engine function. Interface:
function sprightly(entryPoint: string, data: Data, options?: Options): stringGiven an
entryPoint, the function will return the file injected with the variables referenced indata. The function also receives anoptionsargument containing an object of options to modify the function’s behavior. Example:<!-- ./nested/file.html --> {{ name }} says {{> ../message.html }}.<!-- ./message.html --> HiNotes:
{{ var }}is the syntax for referencing a variable.{{> ./path/to/file }}is the syntax for referencing a component.
sprightly("./nested/file.html", { cool: "Osid" }) // => "Osid says Hi". -
sprightlyAsync.A promisifed version of the
sprightlyfunction described above. -
Data.The interface for the
dataparameter defind insprightly's function.interface Data { [key: string]: string | number | Array<string | number | Data> | Data }The
keyis the name of the variable referenced in the template. It can hold a string, a number, a recuresiveDataobject, or an array of all of the mentioned types. This means that the template can use subscripting syntax to reference deeply nested variables. Example:const data = { property: { nested: [{ anotherProperty: ["foo", "bar"] }], }, } sprightly("./file.html", data) // => "bar"In template:
<!-- ./file.html --> {{ property.nested.0.anotherProperty.1 }}The solution used for the subscribting logic is get-value. Its syntax is simple, but you can visit its page if you want to inspect it more thouroughly.
-
Options.The interface for the
optionsparameter defind insprightly's function.interface Options { keyFallback?: string throwOnKeyNotfound?: boolean cache?: boolean }| Option | Default | Description | | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | |
throwOnKeyNotFound|false| Determines whether to throw an error if a referenced variable isn’t found. | |keyFallback| empty string | Contains the value to be used if a referenced variable doesn’t exist. Doesn’t matter ifthrowOnKeyNotFoundis set totrue. | |cache| If available, it is set to thecachevalue sent through Express’s options that comes fromapp.set("view cache", truthValue), if not, it is set tofalse. | Determines whether to cache processed entry points or not. Typically you would want to set this totruein production andfalsein development. | -
SprightlyError.The error class thrown by the
sprightlyfunction. Occusions that the error is thrown at:- If the passed
entryPointargument isn’t a string. - If the passed
dataargument isn’t an object. - If the passed
optionsargument isn’t an object. - If the passed
entryPointdoesn’t exist. - If a referenced variable isn’t found. Triggered only if
throwOnKeyNotFoundis set totrue. - If a referenced component isn’t found.
- If the passed
Usage with ExpressJS
// to import the Express adapter
import sprightlyExpress from "sprightly/express"
/* This sets up the template engine.
* Express by default requires and calls the necessary template engine according to
* the extension of the file to render (e.g. .hbs). But because sprightly has no specific
* file extension to allow it to be used with any file, you have to set this up manually.
*/
app.engine(
"html",
sprightlyExpress({
cache: false,
keyFallback: "obada",
throwOnKeyNotfound: false,
}),
)
app.get("/", (_, res) => {
res.render("./nested/file.html", { cool: "sprightly" })
})
