1.0.1 • Published 5 years ago

node-micro-templating v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

微模板引擎

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