分享 一个纯js语法的 nodejs模板解析引擎
发布于 7 年前 作者 renminghao 4393 次浏览 来自 分享

Github地址:https://github.com/renminghao/ept 内容更新完啦~

### EPT #### 前言

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

#### 优势

  • 代码简单
    • 只有不到100行代码,就能简单的实现核心功能,指令分解,指令收集,指令解析,能够输入一段ept模板代码然后输出html代码
  • 纯javascript语法
    • 能够100%的服用javascript的语法,只需要在指令内容前后加上{{% xxx %}}代码就ept引擎就能够正常的解析并且返回数据

#### 项目开发

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

#### 安装

	npm install ept

#### 使用

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

  • 渲染字符串 <pre> 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() </pre> render方法执行渲染,渲染结果是 <pre> 1-0 test 2-1 3-2 4-3 5-4 6-5 7-6 8-7 9-8 </pre>

  • 集成模板渲染

    • web环境 <pre> 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() </pre>
    • node环境 <pre> 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() </pre>

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

  • test.ept <pre> <!doctype> <html> <head> <title>Test Demo</title> </head> <body> {{% block() %}} </body> </html> </pre>

### 继承语法

  • block()

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

  • extend(xxx)-/extend

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

### 暴露方法(dist/mod/instruction)

  • getInstruction(string)

传入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>,
	{{% } %}}
]
  • formatInstruction(instructionArray,data)

将输入的指令和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 © 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.

3 回复

@zengming00 一起维护一起用起来~

readme还没完善 可以先看代码来用 反正比较简单 这两天会好好完善下readme~

回到顶部