Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
a tpl engine for html template with directives attributes
npm install d-tpl
mocha test/test.js
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
});
FAQs
a tpl engine for html template with directives attributes
We found that d-tpl demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.