当前最新版本: 0.6.8-stable

Juicer 是一个高效、轻量的前端 (Javascript) 模板引擎,效率和易用是它追求的目标。 除此之外,它还可以运行在 Node.js 环境中。

名字的由来

倘若我们把数据比作新鲜可口的水果,把模板看做是水,Juicer 就是把水果和水榨出我们需要的HTML代码片段的榨汁机。

juicer的引入

<script type=”text/javascript” src=”juicer-1.0.min.js”></script>

Juicer的使用方法

编译模板并根据给定的数据立即渲染模板

juicer(tpl, data);

仅编译模板暂不渲染,它会返回一个可重用的编译后的函数

var compiled_tpl = juicer(tpl);

根据给定的数据,对之前编译好的模板进行数据渲染

var compiled_tpl = juicer(tpl);
var html = compiled_tpl.render(data);

注册/注销自定义函数(对象)

juicer.register(‘function_name’, function);
juicer.unregister(‘function_name’);

自定义模板语法边界符,下边是 Juicer 默认的边界符。你可以借此解决 Juicer 模板语法同某些后端语言模板语法冲突的情况

juicer.set({'tag::operationOpen': '{@','tag::operationClose': '}','tag::interpolateOpen': '${','tag::interpolateClose': '}','tag::noneencodeOpen': '$${','tag::noneencodeClose': '}','tag::commentOpen': '{#','tag::commentClose': '}'
});

默认参数配置

{
cache: true [false],
strip: true [false],
errorhandling: true [false],
detection: true [false]
}

参数更改:

juicer.set('strip',false);
juicer.set('cache',false);或juicer.set({'strip': false,'cache': false
};

语法

${变量}

使用 ${} 输出变量值,其中 _ 为对数据源的引用(如 ${_},常用于数据源为数组的情况)。支持自定义函数(通过自定义函数你可以实现很多有趣的功能,类似 ${data|links} 就可以 通过事先定义的自定义函数 links 直接对 data 拼装出<a href=”..” alt=”..” /> ).

${name}
${name|function}
${name|function, arg1, arg2}

示例:

/*data数据*/
var json = {links: [{href: 'http://juicer.name', alt: 'Juicer'},{href: 'http://benben.cc', alt: 'Benben'},{href: 'http://ued.taobao.com', alt: 'Taobao UED'}]
};
/*模板*/
var tpl = ['{@each links as item}','${item|links_build} <br />','{@/each}'
].join('');/*函数*/
var links = function(data) {return '<a href="' + data.href + '" alt="' + data.alt + '" />';
};
/*注册自定义函数*/
juicer.register('links_build', links);
/*编译并渲染模板*/
juicer(tpl, json);

以上代码执行后会出现结果被转义了:

&lt;a href=&quot;http://juicer.name&quot; alt=&quot;Juicer&quot; <br />
&lt;a href=&quot;http://benben.cc&quot; alt=&quot;Benben&quot; <br />
&lt;a href=&quot;http://ued.taobao.com&quot; alt=&quot;Taobao UED&quot; <br />

转义/避免转义
出于安全角度的考虑,${变量} 在输出之前会对其内容进行转义,如果你不想输出结果被转义,可以使用 $${变量} 来避免这种情况

内联辅助函数{@helper}…{@/helper}

{@helper numberPlus}function(number) {return number + 1;}
{@/helper}var tpl = 'Number: ${num|numberPlus}';juicer(tpl, {num: 123
});//输出Number:124

循环遍历 {@each} … {@/each}

对数组进行循环遍历可取得元素或索引值

{@each list as item, index}${item.prop}${index}
{@/each}

辅助循环{@each i in range(m,n)}

{@each i in range(5, 10)}${i}; //输出 5;6;7;8;9;
{@/each}

判断{@if}…{@else if}…{@else}…{@/if}

{@each list as item,index}{@if index===3}the index is 3, the value is ${item.prop}{@else if index === 4}the index is 4, the value is ${item.prop}{@else}the index is not 3, the value is ${item.prop}{@/if}
{@/each}

注释{# 注释内容}

{# 我是注释}

子模板嵌套 {@include tpl, data}

HTML代码:

<script type="text/juicer" id="subTpl">I'm sub content, ${name}
</script>

javascript代码:

var tpl = 'Hi, {@include "#subTpl", subData}, End.';juicer(tpl, {subData: {name: 'juicer'}
});/*输出:Hi, I'm sub content, juicer End.*///或者通过数据引入子模板,下述代码也将会有相同的渲染结果:var tpl = 'Hi, {@include subTpl, subData}, End.';juicer(tpl, {subTpl: "I'm sub content, ${name}",subData: {name: 'juicer'}
});

完整示例

模板页面tpl_list.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>公告消息</title>
</head>{@each LIST as item,index}{@if item.NOTICE_READ==0}<ul><li ><div><ul><li><label >${item.NOTICE_TITLE}</label><span>${item.NOTICE_TIME}</span></li><li><label>${item.NOTICE_CONTENT}</label></li></ul>           </div></li></ul>{@/if}{@if item.NOTICE_READ==1}<ul><li><div><ul><li><label>${item.NOTICE_TITLE}</label><span>${item.NOTICE_TIME}</span></li><li><label>${item.NOTICE_CONTENT}</label></li></ul>           </div></li></ul>{@/if}{@/each}
</html>

主页面notice.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>公告消息</title>
<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="juicer-1.0.min.js"></script>
</head><body><div id="P040201"><div class="page" data-page="noticeMessage" title="公告消息" ></div><div class="page" data-page="noticeDetail" title="公告详情"></div></div>
</body><script type="text/javascript" src="notice.js"></script>
<script type="text/javascript">me.init();console.log("...end");
</script></html>

notice.js

var panel, pages, pageA;
var me = {init : function() {console.log("init");panel = $("#P040201");pages = panel.find(".page");pageA = pages.filter("[data-page='noticeMessage']");list ={LIST : [{"NOTICE_ID" : "1","NOTICE_TITLE" : "贷款","NOTICE_CONTENT" : "关于移动CRM的开发申请工作贷款流程审批关于移动CRM的开发申请工作贷款流程审批,关于移动CRM的开发申请工作贷款流程审批,关于移动CRM的开发申请工作贷款流程审批,关于移动CRM的开发申请工作贷款流程审批。","NOTICE_TIME" : "2018-1-25 9:00","NOTICE_READ" : "0"},{"NOTICE_ID" : "2","NOTICE_TITLE" : "jquery data","NOTICE_CONTENT" : "在元素上存放或读取数据,返回jQuery对象。当参数只有一个key的时候,为读取该jQuery对象对应DOM中存储的key对应的值,值得注意的是,如果浏览器支持HTML5,同样可以读取该DOM中使用 data-[key] = [value] 所存储的值。参见最后一个示例。当参数为两个时,为像该jQuery对象对应的DOM中存储key-value键值对的数据。\如果jQuery集合指向多个元素,那将在所有元素上设置对应数据。 这个函数不用建立一个新的expando,就能在一个元素上存放任何格式的数据,而不仅仅是字符串。\V1.4.3 新增用法, data(obj) 可传入key-value形式的数据。","NOTICE_TIME" : "2018-1-25 9:00","NOTICE_READ" : "0"}]};me.initPageA();},initPageA : function() {$.get("tpl_list.html",function(tpl){var tpl = juicer(tpl,list);pageA.html(tpl);}); }
};

Juicer——a fast template engine相关推荐

  1. python template engine

    Tenjin a fast and full-featured template engine based on embedded Python. install: sudo easy_install ...

  2. Very fast template matching(非常快的模板匹配)

    Very fast template matching(非常快的模板匹配) Integral image 代数矩的快速计算 快速模板匹配 算法步骤 结果 主要讲述文章"Very Fast T ...

  3. TweenLite – A Lightweight, FAST Tweening Engine

    TweenLite – A Lightweight, FAST Tweening Engine Version: 11.133 , Updated 1/18/2010 File size added ...

  4. Go 语言优秀资源整理,为项目落地加速

    指导原则 简单性 复杂性把可读的程序变得不可读,复杂性终结了很多软件项目. 可读性 代码是给人看的,代码阅读时长远超编写.程序必须可维护,那可读是第一步. 生产率 拥有众多的工具集和基础库,可以很简单 ...

  5. go学习资料以及开源代码

    Go 语言方面的大牛,或者优秀 Go 项目的组织 mattn - 写了数百个 Go 项目,盛产优质项目 Unknwon - gogs/macaron 等项目作者,<The Way to Go&g ...

  6. Juicer 中文文档

    Juicer 中文文档 当前最新版本: 0.6.8-stable Juicer 是一个高效.轻量的前端 (Javascript) 模板引擎,使用 Juicer 可以是你的代码实现数据和视图模型的分离( ...

  7. Build a web app fast: Python, HTML JavaScript resources

    转自:http://www.pixelmonkey.org/2012/06/14/web-app Wanna build a web app fast? Know a little bit about ...

  8. ORM框架-工具-产品开发之四 开发代码生成器 Template Studio Development (一)

    今天进入ORM工具开发系列的代码生成工具的开发.现在流行的代码生成工具,一般是基于模板的.T4,Code Smith在基于模板的代码生成方面相当流行.ORM工具,需要从不同的数据库中读取元数据,调用代 ...

  9. 如何使用Python的Flask和Google App Engine构建网络应用

    by Tristan Ganry 由Tristan Ganry 这是一个小型教程项目,用于为初学者学习Flask,API和Google App Engine. (This is a small tut ...

最新文章

  1. 为什么不建议用 equals 判断对象相等?
  2. 智源沙龙:人工智能的技术发展与投资
  3. 通用权限管理系统组件 (GPM - General Permissions Manager) 中后一个登录的把前一个登录的踢掉功能的实现...
  4. Python初探---2x版本与3x版本的区别
  5. (65)如何根据句柄从二级、三级结构句柄表中找到内核对象
  6. SpringMVC以及SSM整合
  7. Session id的存储
  8. Error dialog box generic entry point
  9. leetcode 477. 汉明距离总和(位运算)
  10. 信息学奥赛一本通(1009:带余除法)
  11. Android自定义开机和关机动画
  12. 如何安装一个优秀的BUG管理平台(转)
  13. spring in action 4 第6章 视图分发
  14. Android的banner(轮播图)的实现
  15. notepad++批量操作笔记(持续更新..)
  16. riot修改服务器,如何修改riot拳头账号所在地区 riot账号改地区教程
  17. 项目经理:什么是矩阵型组织结构?
  18. 华为mate40pro和华为mate30pro的区别
  19. 『杭电1859』最小长方形
  20. 百度网盘网页版视频在线倍速播放

热门文章

  1. 驰为hi10拓展Android分区,驰为 Hi10:软件及娱乐体验
  2. matlab的Scope 的工具栏显示出来
  3. Consider defining a bean of type ‘**.Mapper‘ in your configuration
  4. remote-cloudflare-kv 在 Vercel 上使用 Cloudflare KV
  5. Shell中的数组及其相关操作
  6. ERROR! MySQL server PID file could not be found!【给力方案】
  7. 多线程并发编程的基本问题
  8. 「CF103D」 Time to Raid Cowavans【分块】
  9. python 爬虫 requests学习案例
  10. 【弄nèng - Activiti6】Activiti6入门篇(十四)—— 补偿边界事件