Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including smtpl with all npm packages installed. Try it out:

var smtpl = require("smtpl")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

smtpl v0.2.2

Simple string tpl replace

smtpl Build Status

Simple string tpl replace

Installation

use in nodejs:

$ npm install smtpl

use in browser:

$ bower install smtpl

Usage

render directly

smtpl('$valueapos;, {value: 1})  // out: 1
smtpl('%value%', {value: 1}, '%')   // out: 1
smtpl('$value$p.childapos;, {value: 1, p: {child: 2}})   // out: '1$p.childapos;

generate render function

var nostr_render = smtpl(null, {value: 1, value2:2});
nostr_render('$valueapos;)   // out: 1
nostr_render('$value2apos;)  // out: 2

var noparam_render = smtpl('$value$value2apos;);
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$value2apos;, {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.childapos;, {p:{child: 1}})      // out: 1

Render Plugin

render url

smtpl.url('http://www.qq.com/$#cgi$?t=$#tamp;s=$samp;key=$keyamp;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=

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free