分享 一个纯 js 语法的 nodejs 模板解析引擎

2017-03-13 20:34:31 +08:00
 renminghao

Github 地址:https://github.com/renminghao/ept

EPT

前言

目前 node 平台上面的模板解析数不胜数,各个解析引擎都的优势都各有千秋,但是我自己在项目实践中,却发现很难满足我的需求,因为我自己是一个 js 工程师,我肯定希望能够以 js 的语法写 javascript 内容的同时能够来写模板,这样子能够大量减少我的工作思维切换的成本,目前平台上面流行的模板,或多或少的都是有自己自定义的语法,因此我需要创建一个解析引擎来 100%的集成 javasciprt 的预发,减少思维切换的成本

优势

项目开发

	git clone git@github.com:renminghao/ept.git
	npm install
	npm run dev

安装

	npm install ept

使用

浏览器版本在 window 对象下面暴露了一个 EPT 对象,改 EPT 对象和 node 模式下的 EPT 对象使用方法基本保持一致

new EPT('\<ul\>
		{{% for(var i = 0; i < items.length; i++){ %}}
			{{% if(i == 1){ %}}
				\<span class="{{% if(items[i]){ %}}test{{% } %}}"\>test\</span\>
			{{% } %}}
			\<li\>
				{{items[i]}}-{{i}}
			\</li\>
		{{% } %}}
	\</ul\>',{
		items : [1,2,3,4,5,6,7,8,9]
	},callback).render()			

render 方法执行渲染,渲染结果是

1-0
test
2-1
3-2
4-3
5-4
6-5
7-6
8-7
9-8
new EPT('
	{{% extend("./test.ept") %}}
		\<ul\>
			{{% for(var i = 0; i < items.length; i++){ %}}
				{{% if(i == 1){ %}}
					\<span class="{{% if(items[i]){ %}}test{{% } %}}"\>test\</span\>
				{{% } %}}
				\<li\>
					{{items[i]}}-{{i}}
				\</li\>
			{{% } %}}
		\</ul\>
	{{% /extend %}}',{
		items : [1,2,3,4,5,6,7,8,9]
	},callback).render()	
-	node 环境
new EPT('
	{{% extend("./test.ept") %}}
		\<ul\>
			{{% for(var i = 0; i < items.length; i++){ %}}
				{{% if(i == 1){ %}}
					\<span class="{{% if(items[i]){ %}}test{{% } %}}"\>test\</span\>
				{{% } %}}
				\<li\>
					{{items[i]}}-{{i}}
				\</li\>
			{{% } %}}
		\</ul\>
	{{% /extend %}}',{
		items : [1,2,3,4,5,6,7,8,9]
	},callback,{
		context : __dirname
	}).render()	

两者区别在于, web 环境我可以自己去查找集成的文件地址,但是 node 环境下需要手动增加 context 配置,使得能够正常的去查找被继承文件

\<!doctype\>
\<html\>
	\<head\>
		\<title\>Test Demo\</title\>
	\</head\>
	\<body\>
		{{% block() %}}
	\</body\>
\</html\>

继承语法

block()为填充内容, block 为保留字,后续集成模板的模板内容会填充到{{%block()%}}的地方,将该字符串进行替换

extend 为集成方法保留字, ept 会获取()中间的文件的内容,并且将extend-/extend中的和被继承模板进行结合

暴露方法(dist/mod/instruction)

传入 string , getInstruction 会将其中的指令拆出来,返回一个 array , array 内容为指令内容

input

	{{% for(var i = 0; i < items.length; i++){ %}}
		{{% if(i == 1){ %}}
			<span class="{{% if(items[i]){ %}}test{{% } %}}">test</span>
		{{% } %}}
		<li>
			{{items[i]}}-{{i}}
		</li>
	{{% } %}}

output

[
	{{% for(var i = 0; i < items.length; i++){ %}},
	{{% if(i == 1){ %}},
	<span class=",
	{{% if(items[i]){ %}},
	test,
	{{% } %}},
	">test</span>,
	{{% } %}},
	<li>,
	{{items[i]}},
	-,
	{{i}},
	</li>,
	{{% } %}}
]

将输入的指令和 data 内容进行整合,并且返回最新运算后的内容,如果出错,则抛出异常

input

[
	{{% for(var i = 0; i < items.length; i++){ %}},
	{{% if(i == 1){ %}},
	<span class=",
	{{% if(items[i]){ %}},
	test,
	{{% } %}},
	">test</span>,
	{{% } %}},
	<li>,
	{{items[i]}},
	-,
	{{i}},
	</li>,
	{{% } %}}
],
{
	items : [1,2,3,4]
}

output

1-0
test
2-1
3-2
4-3

欢迎使用

MIT License

Copyright (c) 2016 renminghao

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2256 次点击
所在节点    Node.js
6 条回复
7anshuai
2017-03-14 09:41:18 +08:00
一眼看上去,模板代码可读性不太好
renminghao
2017-03-14 11:55:47 +08:00
@7anshuai 是因为指令的关键字不够优雅还是为啥嘞 欢迎讨论
7anshuai
2017-03-14 12:14:39 +08:00
@renminghao 比如渲染字符串和.ept 文件中一些 html 特殊字符 `<>` 都必须转义?
Mbin
2017-03-14 12:46:49 +08:00
感觉和 EJS 没什么区别阿。楼主?
renminghao
2017-03-14 13:47:56 +08:00
@7anshuai 不不不 不是的 这个是因为在 markdown 里面显示 所以我得转义,真正使用的时候不用的
renminghao
2017-03-14 13:49:13 +08:00
@Mbin 相比之下 ept 除了{{%和%}}是必须写的 剩下的就是纯粹的 js 了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/347185

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX