直接贴 TemplateHelper 代码了:

var TemplateHelper = {englishChars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",beginForeachChars: "{$foreach begin$}",endForeachChars: "{$foreach end$}",beginIfChars: "{$if begin$}",endIfChars: "{$if end$}",validateBeginEndTagGrammar: function (template, beginTag, endTag){if (!template){return { success: false, message: "", beginIndex: -1, endIndex: -1 };}var beginTagIndex = template.indexOf(beginTag);var endTagIndex = template.indexOf(endTag);if (beginTagIndex == -1 && endTagIndex == -1){// 没有需要 foreach 的内容return { success: false, message: "", beginIndex: beginTagIndex, endIndex: endTagIndex };}if (beginTagIndex == -1 || endTagIndex == -1){// 缺失 foreach begin 或者 foreach endreturn {success: false,message: "模板有错误,模板里面缺失 " + beginTag + " 或 " + endTag + "。\n" + template,beginIndex: beginTagIndex,endIndex: endTagIndex};}if (beginTagIndex > endTagIndex){return {success: false,message: "模板有错误," + beginTag + " 必须位于 " + endTag + " 的前面!\n" + template,beginIndex: beginTagIndex,endIndex: endTagIndex};}return { success: true, message: "", beginIndex: beginTagIndex, endIndex: endTagIndex };},validateForeachGrammar: function (template){return this.validateBeginEndTagGrammar(template, this.beginForeachChars, this.endForeachChars);},validateIfGrammar: function (template){return this.validateBeginEndTagGrammar(template, this.beginIfChars, this.endIfChars);},getTagCoreContent: function (template, beginTag, endTag){if (!template){return "";}var data = this.validateBeginEndTagGrammar(template, beginTag, endTag); // 验证模板的语法if (!data.success){// 失败if (data.message.length > 0){alert(data.message);}return "";}return template.substring(data.beginIndex + beginTag.length, data.endIndex);},getForeachCoreContent: function (template){return this.getTagCoreContent(template, this.beginForeachChars, this.endForeachChars);},getIfCoreContent: function (template){return this.getTagCoreContent(template, this.beginIfChars, this.endIfChars);},replaceTagCoreContent: function (template, newForeachContent, beginTag, endTag){if (!template || !newForeachContent){return template;}var data = this.validateBeginEndTagGrammar(template, beginTag, endTag); // 验证模板的语法if (!data.success){// 失败if (data.message.length > 0){alert(data.message);}return template;}return template.substr(0, data.beginIndex) + newForeachContent + template.substr(data.endIndex + endTag.length);},replaceForeachCoreContent: function (template, newForeachContent){return this.replaceTagCoreContent(template, newForeachContent, this.beginForeachChars, this.endForeachChars);},replaceIfCoreContent: function (template, newForeachContent){return this.replaceTagCoreContent(template, newForeachContent, this.beginIfChars, this.endIfChars);},removeTagCoreContent: function (template, beginTag, endTag){if (!template){return template;}var data = this.validateBeginEndTagGrammar(template, beginTag, endTag); // 验证模板的语法if (!data.success){// 失败if (data.message.length > 0){alert(data.message);}return template;}return template.substr(0, data.beginIndex) + template.substr(data.endIndex + endTag.length);},removeForeachCoreContent: function (template){return this.removeTagCoreContent(template, this.beginForeachChars, this.endForeachChars);},removeIfCoreContent: function (template){return this.removeTagCoreContent(template, this.beginIfChars, this.endIfChars);}
};var TemplateConfig = {reg_rowNum: new RegExp('{\\$rowNum\\$}', 'ig'),reg_englishChar: new RegExp('{\\$englishChar\\$}', 'ig')
};

也许你还会喜欢:

一个简单的 JavaScript 模板引擎

一个轻量级 Javascript 模板引擎 front.js【二】

谢谢浏览!

转载于:https://www.cnblogs.com/Music/archive/2013/03/11/javascript-custome-template-helper.html

自定义 Javascript 模板规则,打造轻量级模板引擎相关推荐

  1. php中smarty模板的优点,Smarty模板引擎的优点

    Smarty模板引擎的优点 对PHP语言熟悉的程序员就会知道有个Smarty的名词,那么这个具体是什么呢?smarty是一个使用PHP编写的PHP模板引擎,是目前业务最著名,功能最强大的一种PHP模板 ...

  2. 模板 (函数模板语法 ,类模板与函数模板的区别,:函数模板案例,普通函数与函数模板的区别,普通函数与函数模板调用规则,模板的局限性,类模板分文件编写.cpp,Person.hpp,类模板与友元)

    **01:函数模板语法: #include<iostream> using namespace std;//交换两个整型函数 void swapInt(int &a ,int &a ...

  3. C++ 泛型编程(一):模板基础:函数模板,类模板,模板原理,模板匹配规则

    类模板 函数模板 泛型编程 泛型编程,泛型即是指具有在多种数据类型上皆可操作的含义,其实就是能够帮助开发者编写完全一般化并可重复使用的算法,同样的工作不需要做多次,同样的算法针对不同的类型也不应该写多 ...

  4. XSL学习笔记6 XSLT内置模板规则

    XSL学习笔记6 XSLT内置模板规则 定义正确的模板规则来匹配XML树中的节点是XSLT应用的关键.为了让源文档树的节点在没有明确匹配规则的情况下,能够被递归处理,XSLT定义了几个内置的模板规则, ...

  5. c++模板---1(模板概念,利用模板实现数组排序,函数模板调用规则)

    什么叫泛型编程?1. 参数类型化. 2. 模板 模板概念 c++提供了函数模板,所谓函数模板,实际上是建立一个通用函数,其函数类型和形参类型不具体制定,用一个虚拟的类型来代表.这个通用函数就成为函数模 ...

  6. C++ 泛型编程(一):模板基础:函数模板、类模板、模板推演成函数的机制、模板实例化、模板匹配规则

    文章目录 泛型编程 函数模板 函数模板实例化 隐式实例化 显式实例化 函数模板的匹配规则 类模板 类模板的实例化 泛型编程 泛型编程旨在削减重复工作,如: 将一个函数多次重载不如将他写成泛型. voi ...

  7. 如何在PowerPoint中将自定义模板设置为默认模板

    Microsoft PowerPoint allows users to set a custom template as the default theme when creating a new ...

  8. dedecms 自定义表单html,dedecms自定义表单和自定义表单如何用自己模板教程

    dedecms自定义表单和自定义表单如何用自己模板视频教程,无声,但有具体的操作和演示的模板,以及详细的说明. 在线预约一个表单,后台可以直接实现自定义表单功能 注意看有个 表单的id 其他的不用管, ...

  9. dz每个php模板页文件,dz模板引擎分析

    在UCH中,模板与动态数据分离,所以在很多php文件的最后,我们会看到包含了模板文件,如cp_blog.php最后有include_once template("cp_blog") ...

最新文章

  1. 物料凭证不产生会计凭证的几种情况
  2. SAP SMARTFORMS 之由竖打向横打的转换
  3. 【数据结构与算法】之深入解析“将有序数组转换为二叉搜索树”的求解思路与算法示例
  4. 【渝粤题库】陕西师范大学210019 学前教育科研方法 作业(专升本)
  5. vue项目编写html,从头搭建、编写一个VUE项目
  6. Linux平台gcc和动态共享库的基础知识
  7. 我的docker随笔27:基于容器的sqlite测试
  8. JavaScript数据结构——栈(Stack)
  9. java.util报错
  10. 金立S6:因“耀”开启金属手机2.0时代
  11. ora-28547 可能是oracle net 管理错误_PostgreSQL与Oracle:成本、易用性和功能上的差异...
  12. Mysql授权root账户允许远程连接访问
  13. 【前端_React】React小书
  14. 去除pdf文件的许可口令(密码)
  15. 图片验证码的实现以及校验验证码
  16. 一名高级软件测试工程师,需要具备哪些能力
  17. 微信小程序:聊天斗图微信表情包
  18. Web前端和Web后端的区分
  19. Java接口练习(组装电脑)
  20. Mac下挂载NTFS硬盘

热门文章

  1. linux中apache服务器的搭建与配置
  2. 业务、架构、技术,我们应该关注什么
  3. linux 邮件附件 中文,linux bash下通过mailx发送中文内容显示为附件的解决
  4. 用java输出真值表离散数学_离散数学 第一章 命题逻辑 1-4真值表与等价公式
  5. spark 把一列数据合并_Spark DataFrame列的合并与拆分
  6. c语言非法字符空格,98行的四则计算器.(支持括号)加入了非法字符的检测
  7. Spring学习之旅(三)之运行值时注入
  8. c语言将一个实型变量f=55.5678,《C语言程序设计》第2章2 常量和变量
  9. 苹果手机怎么编辑word文档_可以一键导入word图文的微信编辑软件有什么?编辑器怎么使用?...
  10. 用c语言实现倍增原理,RLE算法变体C语言实现