Skip to content

Search tags, filters, functions and guides.

JavaScript API / Creating an environment

JavaScript API

Creating an environment

Set up Kiln with createKiln and its options.

createKiln creates an environment: the object that loads, compiles and renders your templates. Create one when your app starts and reuse it for every render.

import { createKiln, memoryLoader } from "@lucidkiln/core";

const kiln = createKiln({
	loader: memoryLoader({
		"layouts/base.kiln": "...",
		"pages/home.kiln": "...",
	}),
	functions: {
		document: async (collection: string, query: { slug: string }) =>
			db.find(collection, query),
	},
	globals: { site: { name: "Acme" } },
});

const html = await kiln.render("pages/home", { route: { path: "/" } });

Options

OptionDefaultDescription
loaderRequiredWhere templates come from. See Loaders
functions{}Functions templates can call
filters{}Filters templates can use
tests{}Tests templates can use with is
types{}Checks for your own prop types, such as Post. See TypeScript types
globals{}Variables every template can read
stricttrueWhether missing variables and properties are errors. See Strict mode
autoescape"html"The escaping strategy for output, or false to turn it off
locale"en"The locale for formatNumber, plural and formatDate
timeZone"UTC"The time zone for formatDate
cacheNoneWhere the cache tag saves output, such as memoryCache(). See cache
cacheNamespace""Your app and build identity, added to every fragment cache key. Change it on deployment. See Deployments and shared stores
htmlCheckstrueWhich HTML rules check reports, and how. See HTML checks
debugfalseTurns on the dump function
limitsSee LimitsCaps on output size, steps and nesting

locale and timeZone have fixed defaults rather than the server's settings, so a page renders the same on your laptop, on Node and on Workers.

The environment

The environment has these methods:

MethodDescription
render(name, context?, options?)Renders a template to a string. See Rendering
renderBlock(name, block, context?, options?)Renders one block of a template
check(name, source?)Finds problems in a template without rendering it. See Errors and checks
analyze(name, source?)Reports what a template uses
precompile(name, source?)Compiles a template to a JavaScript module at build time. See Precompiling
invalidate(name?)Clears one compiled template, or all of them, from the cache