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

d-tpl

v0.1.0

Published

a tpl engine for html template with directives attributes

Downloads

2

Readme

d-tpl

a tpl engine for html template with directives attributes

Install

npm install d-tpl

Test

mocha test/test.js

Example

if we have a HTML fragment with directives like this:

<div q-cname="test">
    <div q-repeat="list | getList">
        <div q-text="name" q-show="isShow" q-class="red: isRed, bold: size | isBold"></div>
        <input type="text" q-value="pwd" />
        <input type="checkbox" q-value="isCheck" />
        <img src="" alt="" q-src="imgSrc" />
        <img src="" alt="" q-attr="src: imgSrc, attrs"/>
        <div q-text="list | insert 234 | length"></div>
    </div>
</div>

we can compile it to a tpl fun with filters:

var tpl = require('d-tpl');
var src = getSrc(); // get the HTML fragment
var tplFun = tpl.compile({
    raw: src
});

then, we can use this tpl function to output the HTML by datas and filters:

var data = getData(); // get the data
var filters = require('./filters');
var output = tplFun(data, {
    filters: filters
});

if the data like this:

// data.json
/*
 * format:
 * {
 *      componentName: {
 *          key: value,
 *          subComponentName: {
 *              key: value
 *          }
 *      }
 * }
 * 
 * this data structure use nesting to represent the relationship between components
 * 
 */
{
    "test": {
        "list": [{
            "name": "Jack",
            "isShow": true,
            "isRed": true,
            "size": 12,
            "pwd": "123456",
            "isCheck": true,
            "imgSrc": "http://www.baidu.com/logo1.png",
            "attrs": {
                "width": 30,
                "height": 30
            },
            "list": [1, 2, 3]
        }, {
            "name": "John",
            "isShow": true,
            "isRed": false,
            "size": 12,
            "pwd": "123451231236",
            "isCheck": false,
            "imgSrc": "http://www.baidu.com/logo2.png",
            "attrs": {
                "width": 300,
                "height": 300
            },
            "list": [1, 2]
        }, {
            "name": "Anne",
            "isShow": false,
            "isRed": false,
            "size": 7,
            "pwd": "123412312356",
            "isCheck": false,
            "imgSrc": "http://www.baidu.com/logo3.png",
            "attrs": {
                "width": 300,
                "height": 300
            },
            "list": [1, 2, 3, 222]
        }]
    }
}

and the filters resource like this:

// filters.js
/*
 * format:
 * module.exports = {
 *      componentName: {
 *          filterName: filterFun
 *      }
 * }
 * 
 * this data structure will not be nested
 * that is, this data structure does not contain the relationship between components
 * 
 */
module.exports = {
    test: {
        getList: function(list) {
            list.forEach(function(item) {
                item.name += '_long';
            });
            return list;
        },
        isBold: function(size) {
            return size > 10;
        },
        insert: function(list, item) {
            list.push(item);
            return list;
        },
        length: function(list) {
            return list.length;
        }
    }
};

we will get the HTML like this:

<div q-cname="test">
    <div>
        <div q-text="name" q-show="isShow" q-class="red: isRed, bold: size | isBold" style="display: block;" class="red bold">Jack_long</div>
        <input type="text" q-value="pwd" value="123456">
        <input type="checkbox" q-value="isCheck" checked>
        <img src="http://www.baidu.com/logo1.png" alt="" q-src="imgSrc">
        <img src="http://www.baidu.com/logo1.png" alt="" q-attr="src: imgSrc, attrs" width="30" height="30" >
        <div q-text="list | insert 234 | length">4</div>
    </div><div>
        <div q-text="name" q-show="isShow" q-class="red: isRed, bold: size | isBold" style="display: block;" class=" bold">John_long</div>
        <input type="text" q-value="pwd" value="123451231236">
        <input type="checkbox" q-value="isCheck" >
        <img src="http://www.baidu.com/logo2.png" alt="" q-src="imgSrc">
        <img src="http://www.baidu.com/logo2.png" alt="" q-attr="src: imgSrc, attrs" width="300" height="300" >
        <div q-text="list | insert 234 | length">3</div>
    </div><div>
        <div q-text="name" q-show="isShow" q-class="red: isRed, bold: size | isBold" style="display: none;" class=" ">Anne_long</div>
        <input type="text" q-value="pwd" value="123412312356">
        <input type="checkbox" q-value="isCheck" >
        <img src="http://www.baidu.com/logo3.png" alt="" q-src="imgSrc">
        <img src="http://www.baidu.com/logo3.png" alt="" q-attr="src: imgSrc, attrs" width="300" height="300" >
        <div q-text="list | insert 234 | length">5</div>
    </div>
</div>

for performance, the engine will set a function value named funSerializationStr into the tpl function, this value is the serialization of tpl function, you can save it in file as a node module for example, like this:

var fs = require('fs');
fs.writeFileSync('./tplFun.js', tplFun.funSerializationStr);

// other modules can use it
var tplFun2 = require('./tplFun');
var data = getData(); // get the data
var filters = require('./filters');

var output = tplFun2(data, {
    filters: filters
});

Reference

  • Q.js this tpl is implemented to serve the mvvm library, Q.js, in the 0.0.1 version.

TODO

  • 现在的data是以组件名作为key的,如何解决包含重复组件的data?
  • 在data格式中,子组件的data是属于父组件data的一个值,同样以组件名为key(会出现上一个问题);如何解决父组件自身数据与子组件名同名问题?
  • 实现通用方案,通过提供扩展点,实现能编译所有directives式语言的模板的能力