1.0.0 • Published 3 years ago

native-templater v1.0.0

Weekly downloads
2
License
MIT
Repository
-
Last release
3 years ago

Native Templater (Strict)

A simple library for easy but customizable templating. (Strict version)

For a more lenient version of NT: (COMING OUT SOON)

Getting started:

First install Native Templater with the following command:

npm install native-templater

Code Example:

TEM file:

<h1>{ heading1 } { heading2 } { heading3 }</h1>

JavaScript file:

const template = require(‘native-templater’)

template.render(“./index.tem”, {heading1: “This”, heading3: “Awesome!”, heading2: “Is”}, function(err, res) {
	if (err != null) {
	console.log(err)
	return;
	}
	console.log(res);
}}

How to Render with Native Templater

Native template has two rendering functions, render and renderAsync. The default, render function is callback based, while renderAsync will return the rendered code and can easily be assigned to a variable. Here is an adaptation of the previous example, using renderAsync.

const template = require(‘native-templater’)

var render = template.renderAsync(“./index.tem”, {heading1: “This”, heading3: “Awesome!”, heading2: “Is”})

if (render.err != null) {
	console.log(render.err)
} else {
	console.log(render.res)
}

renderAsync is object-based, to avoid having a confusing array or returning an error in the form of the rendered code, which has a type of array, meaning you’d have to check if the response was type array. Yuck.

Templating Syntax

Native Templater can take any language type and template it, and is easy to integrate in with Express. All templates look like this

{ VARIABLE }

The first curly-bracket tells Native Templater that it is the start of a template. Anything inside the template will be replaced (including the curly brackets & any spaces inside of the curly brackets).

Providing Data to Native Templater

When you want to render your template, you need to give Native Templater an object full of your variables.

The format of the rendering looks like this:

template.render(FILEDIR, OBJECT, CALLBACK)

The object for the example above will look like this:

{heading1: “This”, heading2: “Is”, heading3: “Awesome!”}

You don’t need to place your tags in any particular order.