simple-template-utils
Simple template utility to generate strings based on templates and input data.
Last updated 6 months ago by neapnic .
BSD-3-Clause · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install simple-template-utils 
SYNC missed versions from official npm registry.

Simple Template Utils · Tests License Neap

Simple Template Utils helps generating strings based on templates and input data. By default, the template uses parameters between the opening delimiter {{ and closing delimiter }}, but those delimiters can be customized.

Table of Contents

Install

npm i simple-template-utils

Getting started

Let's say there is a folder in the root folder called template with a file called your-template.html similar to this:

<!DOCTYPE html>
<html>
<head>
	<title>{{ project.page.title}}</title>
	<meta charset="utf-8" >
	<meta name="version" content="{{     version }}">
</head>
<body>
	<h1>{{ project.blog.title }}</h1>
	{{project.blog.content}}
</body>
</html>

The following code in an index.js

const path = require('path')
const template = require('simple-template-utils')

template.compile({
	template: path.join(__dirname, './template/your-template.html'),
	data: {
		version: '0.0.1',
		project: {
			page: {
				title: 'Hello page'
			},
			blog: {
				title: 'First blog post',
				content: '<p>Lorem ipsum</p>'
			}
		}
	}
}).then(console.log)

outputs this:

<!DOCTYPE html>
<html>
<head>
	<title>Hello page</title>
	<meta charset="utf-8" >
	<meta name="version" content="0.0.1">
</head>
<body>
	<h1>First blog post</h1>
	<p>Lorem ipsum</p>
</body>
</html>

FAQ

How to define default values?

template.compile({
	template: path.join(__dirname, './template/your-template.html'),
	data: {
		project: {
			blog: {
				title: 'Second blog post',
				content: '<p>Lorem ipsum lorem ipsum</p>'
			}
		}
	},
	defaultData: {
		version: '0.0.1',
		project: {
			page: {
				title: 'Hello page'
			},
			blog: {
				title: 'First blog post',
				content: '<p>Lorem ipsum</p>'
			}
		}
	}
}).then(console.log)

Can I use a string template instead the path to a template?

Yes, you can.

const template_01 = 
`<!DOCTYPE html>
<html>
<head>
	<title>{{ project.page.title}}</title>
	<meta charset="utf-8" >
	<meta name="version" content="{{     version }}">
</head>
<body>
	<h1>{{ project.blog.title }}</h1>
	{{project.blog.content}}
</body>
</html>`

template.compile({
	template: template_01,
	data: {
		version: '0.0.1',
		project: {
			page: {
				title: 'Hello page'
			},
			blog: {
				title: 'First blog post',
				content: '<p>Lorem ipsum</p>'
			}
		}
	}
}).then(console.log)

Does it support custom delimiters?

Yes it does. The default delimiters are {{ and }}. Let's respectively replace them with {{{ and }}}:

const template_01 = 
`<!DOCTYPE html>
<html>
<head>
	<title>{{{ project.page.title}}}</title>
	<meta charset="utf-8" >
	<meta name="version" content="{{{     version }}}">
</head>
<body>
	<h1>{{{ project.blog.title }}}</h1>
	{{{project.blog.content}}}
</body>
</html>`

template.compile({
	template: template_01,
	data: {
		version: '0.0.1',
		project: {
			page: {
				title: 'Hello page'
			},
			blog: {
				title: 'First blog post',
				content: '<p>Lorem ipsum</p>'
			}
		}
	},
	delimiters: { open:'{{{', close: '}}}' }
}).then(console.log)

This Is What We re Up To

We are Neap, an Australian Technology consultancy powering the startup ecosystem in Sydney. We simply love building Tech and also meeting new people, so don't hesitate to connect with us at https://neap.co.

Our other open-sourced projects:

GraphQL

  • graphql-s2s: Add GraphQL Schema support for type inheritance, generic typing, metadata decoration. Transpile the enriched GraphQL string schema into the standard string schema understood by graphql.js and the Apollo server client.
  • schemaglue: Naturally breaks down your monolithic graphql schema into bits and pieces and then glue them back together.
  • graphql-authorize: Authorization middleware for graphql-serverless. Add inline authorization straight into your GraphQl schema to restrict access to certain fields based on your user's rights.

React & React Native

Authentication & Authorization

  • userin: UserIn let's App engineers to implement custom login/register feature using Identity Providers (IdPs) such as Facebook, Google, Github.

General Purposes

  • core-async: JS implementation of the Clojure core.async library aimed at implementing CSP (Concurrent Sequential Process) programming style. Designed to be used with the npm package 'co'.
  • jwt-pwd: Tiny encryption helper to manage JWT tokens and encrypt and validate passwords using methods such as md5, sha1, sha256, sha512, ripemd160.

Google Cloud Platform

  • google-cloud-bucket: Nodejs package to manage Google Cloud Buckets and perform CRUD operations against them.
  • google-cloud-bigquery: Nodejs package to manage Google Cloud BigQuery datasets, and tables and perform CRUD operations against them.
  • google-cloud-tasks: Nodejs package to push tasks to Google Cloud Tasks. Include pushing batches.

License

Copyright (c) 2017-2019, Neap Pty Ltd. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of Neap Pty Ltd nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NEAP PTY LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Neap Pty Ltd logo

Current Tags

  • 0.0.4                                ...           latest (6 months ago)

4 Versions

  • 0.0.4                                ...           6 months ago
  • 0.0.3                                ...           6 months ago
  • 0.0.2                                ...           6 months ago
  • 0.0.1                                ...           6 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 5
Dependencies (5)
Dev Dependencies (5)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |