@ahmadnassri/template-literals-engine

中文文档
2.0.4发布于 2023/09/05MITCommonJS近 30 天下载 3542

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

原始使用文档
文档目录5 节
  1. Why
  2. What
  3. How
  4. Usage
  5. API

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

Example
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

Example
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

Example
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

Example
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!

Example
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

nametyperequireddefaultdescription
rootString✖process.cwd()path to look for template files
extensionString✖jstltemplate 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

版本与发布记录

@ahmadnassri/template-literals-engine 共发布 15 个版本,首次发布于 2020/10/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.4latest2023/09/053 年前
2.0.12023/01/023 年前
2.0.02022/07/294 年前
1.0.112021/08/135 年前
1.0.102021/06/125 年前
1.0.92021/05/125 年前

关于 @ahmadnassri/template-literals-engine 的常见问题

@ahmadnassri/template-literals-engine 是什么?

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

如何安装 @ahmadnassri/template-literals-engine?

在项目目录运行 npm install @ahmadnassri/template-literals-engine,或使用 pnpm add / yarn add / bun add。

@ahmadnassri/template-literals-engine 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ahmadnassri__template-literals-engine 是否存在。

@ahmadnassri/template-literals-engine 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@ahmadnassri/template-literals-engine 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ahmadnassri/template-literals-engine 可以商用吗?

@ahmadnassri/template-literals-engine 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ahmadnassri/template-literals-engine 的下载量有多少?

根据 npm 官方下载统计,@ahmadnassri/template-literals-engine 在 2026/10/05 – 2026/11/03 这 30 天内被下载 3,542 次,日均约 118 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ahmadnassri/template-literals-engine 还在维护吗?

最新版本 2.0.4 发布于 2023/09/05(3 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/10/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。