闲的无聊写的一个bigpipe的template engine
发布于 9 年前 作者 joesonw 4193 次浏览 最后一次编辑是 8 年前 来自 分享

https://www.npmjs.com/package/btljs

html的格式 可以方便的扩展 异步执行(每解析一个tag都直接stream输出)

##Description

This is a template language aimed for bigpipe with async support

Template is parsed line by line.

No more messed-up&splitted-everywhere modules needed.

Write bigpipe in pure html

##Example

###Template

	<html>
		<head>
			<title>Hello World</title>
		</head>
		<body>
			<for start='0' end='10' step='+2' iterator='j'>
				<script wait>
					this.j++;
					setTimeout(this.fulfill,2000) //2000ms
				</script>
				<p> Aloha!!!!</p>
			</for>	
		</body>
	</html>

###NodeJs

	var koa 	= require('koa');
	var fs		= require('fs');
	var Parser  = require('./lib/parser');
	var co 		= require('co');
	var app 	= koa();
	var html 	= fs.readFileSync('you file');

	var parser 	= Parser();

	co(function*() {
		yield parser.load(html);
		app.use(function* () {
			this.type = 'html';
			this.body = parser.stream();
		});
	});
	
	app.listen(3000);

##Customization

###Add custom tag


parser.addTag('MyTag',function* (ele,push,context) {
	push('<p>This is a test tag</p>');
});

回到顶部