V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
renminghao
V2EX  ›  Node.js

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

  •  2
     
  •   renminghao · 2017-03-13 20:34:31 +08:00 · 2247 次点击
    这是一个创建于 2597 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    EPT

    前言

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

    优势

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

    项目开发

    	git clone [email protected]: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
    
    • 集成模板渲染
      • web 环境
    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 配置,使得能够正常的去查找被继承文件

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

    继承语法

    • 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 (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.

    6 条回复    2017-03-14 13:49:13 +08:00
    7anshuai
        1
    7anshuai  
       2017-03-14 09:41:18 +08:00
    一眼看上去,模板代码可读性不太好
    renminghao
        2
    renminghao  
    OP
       2017-03-14 11:55:47 +08:00
    @7anshuai 是因为指令的关键字不够优雅还是为啥嘞 欢迎讨论
    7anshuai
        3
    7anshuai  
       2017-03-14 12:14:39 +08:00
    @renminghao 比如渲染字符串和.ept 文件中一些 html 特殊字符 `<>` 都必须转义?
    Mbin
        4
    Mbin  
       2017-03-14 12:46:49 +08:00
    感觉和 EJS 没什么区别阿。楼主?
    renminghao
        5
    renminghao  
    OP
       2017-03-14 13:47:56 +08:00
    @7anshuai 不不不 不是的 这个是因为在 markdown 里面显示 所以我得转义,真正使用的时候不用的
    renminghao
        6
    renminghao  
    OP
       2017-03-14 13:49:13 +08:00
    @Mbin 相比之下 ept 除了{{%和%}}是必须写的 剩下的就是纯粹的 js 了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3354 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:53 · PVG 19:53 · LAX 04:53 · JFK 07:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.