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

node-micro-templating

v1.0.1

Published

[![NPM version][npm-image]][npm-url] [![npm download][download-image]][download-url]

Downloads

2

Readme

微模板引擎

NPM version npm download

基础 Micro-Templating https://johnresig.com/blog/javascript-micro-templating/开发的,可以node下运行,并支持预编译到浏览器中运行。

Install

$ npm i node-micro-templating --save

Usage

const template = require('node-micro-templating');

const users =  [
    {url:'http://qqq', name: "jiamao"},
    {url:'http://qqq2', name: "jiamao2"}
];
// 字符串模板解析
let code = template.renderString(`<% for ( var i = 0; i < users.length; i++ ) { %>
    <li><a href="<%=users[i].url%>"><%=users[i].name%></a></li>
  <% } %>`, 
  {
      users: users
  });

文件模板解析

template.render('./user.html', {
    data: {
        users: users
    },
    root: path.resolve(__dirname, 'templates') // 模板所在路径,如果在浏览器中。这里可以是url
  }, function(err, res) {
    console.log(res);
  });

带子模板文件

支持 include 函数来加载子模板。

<!-- user.html -->
<div class="cell-box border-spacing js-top-banner">
    <% include("./banner.html") %>
</div>
<!-- banner.html -->
<% for ( var i = 0; i < users.length; i++ ) { %>
    <li><a href="<%=users[i].url%>"><%=users[i].name%></a></li>
<% } %>

预编译

如果有子模板文件,在开发环境下,会自动ajax请求异步渲染。 当我们部署到生产环境,自然不希望增加请求,这时就可以用预编译功能,它可以把模板压入同一js文件中。

// 预编译
  var usertpl = fs.readFileSync(path.join(__dirname,'./templates/user.html'), 'utf8');
  var tpl = engine.precompile(usertpl, {
      id: 'user.html'
  });    

  var bannertpl = fs.readFileSync(path.join(__dirname,'./templates/banner.html'), 'utf8');
  tpl += engine.precompile(bannertpl, {
    id: 'banner.html'
  });
// 浏览器只需要引用这个user.js即可
  var tplpath = path.join(__dirname,'./templates/user.js');
  fs.writeFileSync(tplpath, tpl);

示例

https://jiamao.github.io/node-micro-templating/test/index.html

<!doctype html>
<html>
	<head>
		<title>micro templating</title>
		<meta content="text/html; charset=UTF-8" http-equiv="content-type" />
		<meta name="viewport" content="width=device-width,initial-scale=1">
        <script src="../index.js"></script>
        <!--这里是预编译好的模板,如果不引用则会异步去拉取模板-->
        <!--<script src="templates/user.js"></script>-->
	</head>
	<body>
		<div id="root"></div>
        <script>
            window.MICROTEMPLATING.render('user.html', {
                root: 'templates',
                data: {
                    users: [
                        {url:'http://qqq.com', name: "jiamao"},
                        {url:'http://qqq2.com', name: "jiamao2"}
                    ]
                }
            }, function(err, res) {
                document.getElementById('root').innerHTML = res;
            });
        </script>
	</body>
</html>

License

MIT