npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ahmadnassri/template-literals-engine

v2.0.4

Published

Feed it a JSON Spec, it will spit out a lightweight HTTP client!

Downloads

816

Readme

JS Template Literals Engine

a very basic, and straight to the point Template Engine using JS Template Literals

license release semantic

Why

Template Literals (also known as "Template strings") are a familiar and simple to use method to run embedded expressions and string interpolation, which are the primary functionality of a Template Engine.

This library wraps template literals with traditional techniques (layouts, partials, helpers, etc ...) to facilitates the usage through a structured file format (.jstl)

What

Features:

  • Layouts: native support for layouts
  • Partials Support: include files and pass data with ease
  • Fast: Templates are cached by default
  • Extensible: extensible with custom helpers

How

Usage

template.jstl
hello ${data.name}
index.js
const Engine = require('@ahmadnassri/template-literals-engine')

const engine = new Engine(options)

engine.render('template', { name: 'ahmad' })

String Interpolation

since templates are parsed as standard JavaScript Template Literals the same functionality is expected

Variables are accessible data context

index.js
const Engine = require('@ahmadnassri/template-literals-engine')

const engine = new Engine()

const result = engine.render('template', { name: 'ahmad' })

console.log(result)
template.jstl
Hello ${data.name}
result
$ node index.js
Hello ahmad

Helper functions

Helpers are essentially functions that can be called within the template.

All helper functions are available through the fn context

index.js
const Engine = require('@ahmadnassri/template-literals-engine')

const engine = new Engine({
  helpers: { 
    capitalize : str => string[0].toUpperCase() + string.slice(1)
  }
})

const result = engine.render('template', { name: 'ahmad' })

console.log(result)
template.jstl
Hello ${fn.capitalize(data.name)}
result
$ node index.js
Hello Ahmad

Built-in Helpers

fn.render(templateName, [dataObject])

Behaves exactly like Engine.render by including other template file: templateName inline

fn.apply(templateName, [dataArray])

enumerates over dataArray and applies each item to a newly rendered instance of templateName

index.js
const Engine = require('@ahmadnassri/template-literals-engine')

const engine = new Engine()

const actors = [
  { name: 'William Hartnell', year: '1963' },
  { name: 'Patrick Troughton', year: '1966' },
  { name: 'Jon Pertwee', year: '1970' },
  { name: 'Tom Baker', year: '1974' },
  { name: 'Peter Davison', year: '1981' },
  { name: 'Colin Baker', year: '1984' },
  { name: 'Sylvester McCoy', year: '1987' },
  { name: 'Paul McGann', year: '1996' },
  { name: 'Christopher Eccleston', year: '2005' },
  { name: 'David Tennant', year: '2005' },
  { name: 'Matt Smith', year: '2010' },
  { name: 'Peter Capaldi', year: '2013' },
  { name: 'Jodie Whittaker', year: '2017' }
]

const result = engine.render('page', { actors })

console.log(result)
page.jstl
${ fn.include('header') }

<h1>List of actors who have played the Doctor</h1>

<ul>
  ${ fn.apply('doctor', data.actors) }
</ul>

${ fn.include('footer') }
doctor.jstl
<li>${data.name} - first appeared in ${data.year}</li>
result
$ node index.js
<body>

<h1>List of actors who have played the Doctor</h1>

<ul>
<li>William Hartnell - first appeared in 1963</li>
<li>Patrick Troughton - first appeared in 1966</li>
<li>Jon Pertwee - first appeared in 1970</li>
<li>Tom Baker - first appeared in 1974</li>
<li>Peter Davison - first appeared in 1981</li>
<li>Colin Baker - first appeared in 1984</li>
<li>Sylvester McCoy - first appeared in 1987</li>
<li>Paul McGann - first appeared in 1996</li>
<li>Christopher Eccleston - first appeared in 2005</li>
<li>David Tennant - first appeared in 2005</li>
<li>Matt Smith - first appeared in 2010</li>
<li>Peter Capaldi - first appeared in 2013</li>
<li>Jodie Whittaker - first appeared in 2017</li>
</ul>

</body>

Front Matter

Every template file can optionally include a front matter block, which is parsed and included into the data context

index.js
const Engine = require('@ahmadnassri/template-literals-engine')

const engine = new Engine()

const result = engine.render('template')

console.log(result)
template.jstl
---
name: ahmad
---
Hello ${data.name}
result
$ node index.js
Hello ahmad

Layouts

defining a layout property in the Front Matter block of a template will result in rendering that layout first and including the current template as content.

Layouts can infinitely cascade, the only limit is your system resources!

index.js
const Engine = require('@ahmadnassri/template-literals-engine')

const engine = new Engine()

const result = engine.render('template', { name: 'ahmad' })

console.log(result)
template.jstl
---
layout: layouts/welcome
---

<h2>${data.name}</h2>
layouts/welcome.jstl
---
layout: layouts/base
---

<h1>Welcome!</h1>

${data.content}
layouts/base.jstl
<html>
  <body>
    ${data.content}
  </body>
</html>
result
$ node index.js
<html>
  <body>
    <h1>Welcome</h1>
    <h2>ahmad</h2>
  </body>
</html>

API

Constructor new Engine([optionsObject])

returns a new instance of the template engine class

| name | type | required | default | description | |-----------------|----------|----------|-----------------|----------------------------------------------------| | root | String | ✖ | process.cwd() | path to look for template files | | extension | String | ✖ | jstl | template file extension | | helpers | Object | ✖ | {} | key => function helpers map to pass to templates | | matter | Object | ✖ | {} | Options to pass to gray-matter |

example:
const Engine = require('@ahmadnassri/template-literals-engine')

const engine = new Engine({
  root: 'templates',
  extension: 'html'
  helpers: {
    capitalize : str => string[0].toUpperCase() + string.slice(1),
    ...
  }
})

Method: render(templateName, [dataObject])

parses the content of the file at ${templateName}.jstl and passes dataObject to it, returns the processed output string

example:
const Engine = require('@ahmadnassri/template-literals-engine')

const engine = new Engine({ root: 'templates' })

engine.render('a-template') // => ./templates/a-template.jstl
engine.render('nested/template', { foo: 'bar' }) // => ./templates/nested/template.jstl

Author: Ahmad Nassri • Twitter: @AhmadNassri