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

tarim

v0.1.5

Published

a template engine, using Lodash's template syntax and supporting including other templates

Downloads

67

Readme

Tarim is a template engine, using Lodash's template syntax and supporting include other templates.

Usage

const tmplEngine = require('tarim');

let str;
let tmpl;

// string
str = 'hello <%= user %>!';
tmpl = tmplEngine(str);
tmpl({ 'user': 'fred' })
// 'hello fred!'

// escaped string
str = '<b><%- value %></b>';
tmpl = tmplEngine(str);
tmpl({ 'value': '<script>' })
// '<b>&lt;script&gt;</b>'

// array
str = `
  <% users.forEach(u => { %>
    <li><%- u %></li>
  <% }) %>
`;
tmpl = tmplEngine(str);
tmpl({ 'users': ['fred', 'barney'] })
// '<li>fred</li><li>barney</li>'

// ES6 template string
str = 'hello ${ user }!';
tmpl = tmplEngine(str);
tmpl({ 'user': 'pebbles' })
// 'hello pebbles!'

// escaping delimiters
str = '<%= "\\<%- value %\\>" %>';
tmpl = tmplEngine(str);
tmpl({ 'value': 'ignored' })
// => '<%- value %>'

Advanced usage

const tmplEngine = require('tarim');

let str;
let tmpl;

// print() function
str = '<% users.forEach(u => print("<li>" + u + "</li>")) %>';
tmpl = tmplEngine(str);
tmpl({ 'users': ['fred', 'barney'] })
// '<li>fred</li><li>barney</li>'

// template function's source
str = 'hello world';
tmpl = tmplEngine(str);
tmpl.source
// 'function(obj) {\nobj || (obj = {});\nvar __t, __p = \'\';\nwith (obj) {\n__p += \'hello world\';\n\n}\nreturn __p\n}'

Including Templates

A template could be included into another template.

<% include templateFileName %>

The above line will be replaced by the content of file templateFileName.template.

The default path of included template files is the current working directory process.cwd(), and the default extension of template files is .template. You could customize them by setting Option.includePath and Option.includeExt.

Option.includePath is to specify the location of a directory of template files relative to process.cwd() directory. It could also be an absolute path.

const tmplEngine = require('tarim');
const str = '<% include data %>';
const tmpl = tmplEngine(str, {
  includePath: './templates',
  includeExt: '.template',
});

Config

const tmplEngine = require('tarim');

let str;
let option;
let tmpl;

// data object
str = 'hello <%= data.user %>!';
option = { 'variable': 'data' };
tmpl = tmplEngine(str, option);
tmpl({ user: 'world'})
// 'hello world!'

// sourceURL option
str = 'hello <%= user %>!';
option = {
  'sourceURL': '/basic/greeting.jst'
};
tmpl = tmplEngine(str, option);
// => source file "greeting.jst" will be
// => under the Sources tab or Resources panel of the web inspector.

// custom template delimiters
option = {
  'interpolate': /{{([\s\S]+?)}}/g
}
str = 'hello {{ user }}!';
tmpl = tmplEngine({ 'user': 'mustache' });
// 'hello mustache!'

API

tmplEngine([string=''], [options={}])

Arguments

  • [string=''] (string): The template string.
  • [options={}] (Object): The options object.
  • [options.escape=_.templateSettings.escape] (RegExp): The HTML "escape" delimiter.
  • [options.evaluate=_.templateSettings.evaluate] (RegExp): The "evaluate" delimiter.
  • [options.imports=_.templateSettings.imports] (Object): An object to import into the template as free variables.
  • [options.interpolate=_.templateSettings.interpolate] (RegExp): The "interpolate" delimiter.
  • [options.sourceURL='lodash.templateSources[n]'] (string): The sourceURL of the compiled template.
  • [options.variable='obj'] (string): The data object variable name.
  • [options.includePath=process.cwd()] (string): The path of included templates.
  • [options.includeExt='.template'] (string): The file extension of included templates.

Returns

(Function): Returns the compiled template function.

License

MIT