0.1.0 • Public • Published


NPM version npm Travis

An NPM for template parsing and rendering supporting Smarty(the syntax, not all of php), Handlebars and UBB. Easy to extend to make your own tag parser.

Usage as a Base Class

First include the module:

var TagTemplate = require('tag-template');

then, extend the object using Classy (this will likely switch to prime in the future):

var AwesomeTemplate = new Class({
    Extends : TagTemplate,
    strict : true,
    initialize: function(template){
        this.parent(template, {
            environments : []
    renderNode : function(node){
        //handle an individual node
    render : function(data, callback){
        this.parent(data, fn.bind(function(rendered){
            if(this.parser.text != ''){
                rendered += this.parser.text;
                this.parser.text = ''
        }, this));

In order to actually parse things, though you'll have to describe the tags in your document, which was done above with this line:

this.parent(template, {
    environments : []

The actual tag descriptions are simply objects, so lets's assume we want to parse some macros that look like:

(tagName one two three)

(tagName one="1" two="2" three="3")

Then we would configure the tags like:

    name : 'parenthetical-tag',
    sentinels : [['(', ')']],
    onParse : function(tag, parser){
        //do stuff here

and the incoming parse would look something like:

    name : 'tagName',
    text : 'tagName one two three',
    parts : ['one', 'two', 'three'],
    attributes : {}

    name : 'tagName',
    text : 'tagName one="1" two="2" three="3"',
    parts : ['one="1"', 'two="2"', 'three="3"'],
    attributes : {
        one : '1',
        two : '2',
        three : '3'

Then you just need to extend renderNode in order to output the results from a given node. check the existing implementations for further detail.


UBB Codes are an ancient mapping to allow basic markup in posts but restrict users usage of HTML, particularly popular on Forums. Check out some UBB Code docs for information.

Include it:

var UBB = require('tag-template/ubb');

Then use it:

var template = new UBB(
    //do stuff


This provides basic support for Smarty template parsing without any of the standard function libraries smarty usually depends on. Through careful management of macro usage, templates may be shared... but it is unlikely an existing template will drop in without modification or extending the internal macros.

  1. value Any value can be be output in the form:



  2. if the if construct allows you to conditionally execute logic, for example:

     {if $bobMckenzieIsDirecting}

    it also supports else clauses:

     {if $myList.length > 0}
         <!--iterate over list-->
         <!--show 'no data' state-->
  3. foreach You can iterate across a collection using the foreach macro which supports 3 attributes

    1. from : the object or array we are iterating over
    2. item : the variable name we store the current iteration's value in
    3. key : the variable name we store the current iteration's key in

    as an example:

     {foreach from="$thing" item="item" key="key"}
  4. literal An encapsulation to prevent '{}' from attempting to parse as macros, useful for wrapping inline js or css.

Include it:

var Smarty = require('tag-template/smarty');

To actually parse a template:

var template = new Smarty(
    {foreach from="$list" item="value" key="name"}\
template.render(data, function(rendered){
    //do stuff!

And to add a macro:

Smarty.registerMacro(<name>, function(node, template){


Given that this implementation currently has an incomplete set of macros, I'll hold off on documenting this just now. The main impetus is just not using generated code in a language where you define macros with functions, and also as another test for the tag parser base.


Run the tests at the project root with:



-Abbey Hawk Sparrow

Package Sidebar


npm i tag-template

Weekly Downloads






Last publish


  • khrome