Socket
Socket
Sign inDemoInstall

smtpl

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    smtpl

Simple string tpl replace


Version published
Weekly downloads
11
decreased by-8.33%
Maintainers
1
Install size
18.1 kB
Created
Weekly downloads
 

Readme

Source

smtpl Build Status

Simple string tpl replace

Installation

use in nodejs:

$ npm install smtpl

use in browser:

$ bower install smtpl

Usage

render directly
smtpl('$value$', {value: 1})  // out: 1
smtpl('%value%', {value: 1}, '%')   // out: 1
smtpl('$value$$p.child$', {value: 1, p: {child: 2}})   // out: '1$p.child$'
generate render function
var nostr_render = smtpl(null, {value: 1, value2:2});
nostr_render('$value$')   // out: 1
nostr_render('$value2$')  // out: 2

var noparam_render = smtpl('$value$$value2$');
noparam_render({value:1})    // out: 1
noparam_render({value2:2})   // out: 2

custom value method

var render = smtpl.newSmtpl(function(_asName, _aoParams)
{
	return _aoParams[_asName] || _asName;
});

render('$value$$value2$', {value: 1})    // out: 1value2

// support child value
var child_render = smtpl.newSmtpl(function(_asName, _aoParams)
{
	var _oNames = _asName.split('.');
	var _oParams = _aoParams;
	for(var i = 0, len = _oNames.length; i < len; i++)
	{
		if (_oNames[i] in _oParams)
		{
			_oParams = _oParams[_oNames[i]];
		}
		else
		{
			return '';
		}
	}

	return _oParams;
}, '[\\w.]+?');

child_render('$p.child$', {p:{child: 1}})      // out: 1

Render Plugin

render url

smtpl.url('http://www.qq.com/$#cgi$?t=$#t$&s=$s$&key=$key$&blank=',
	{
		cgi	: 'index',
		t	: 'sim&v=<',
		key	: 'key&d=>'
	});
// out: http://www.qq.com/index?t=sim&v=<&s=&key=key%26d%3D%3E&blank=

Keywords

FAQs

Last updated on 04 May 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc