简体   繁体   English

JavaScript模板引擎

[英]JavaScript Templating Engine

I would like to create universal templating engine in JavaScript, how to? 我想在JavaScript中创建通用模板引擎,怎么做?

HTML template HTML模板

<h1><%title1%></h1>
<h2><%title2%></h2>

JSON file JSON文件

{
  "title1" : "Hello World!",
  "title2" : "Hi World!"
}

Javascript Java脚本

  • Find in HTML file <%title1%> 在HTML文件<%title1%>中查找
  • Find in JSON file variable "title1" 在JSON文件变量“ title1”中查找
  • Replace <%title1%> with value of variable "title1" 将<%title1%>替换为变量“ title1”的值

Same for <%title2%> 对<%title2%>相同

Thanks! 谢谢!

Have a look at this article . 看一下这篇文章 It discusses a proposal (by microsoft) how support for templates could be added to the jQuery library. 它讨论了一个建议(由Microsoft提出),如何将对模板的支持添加到jQuery库中。

In the same article you will also find an overview of some already existing template solutions (maybe you'll find something that matches your needs, instead of re-inventing the wheel). 在同一篇文章中,您还将找到一些已经存在的模板解决方案的概述(也许您会找到满足您需求的内容,而不是重新发明轮子)。

Update (2012-07-23): 更新(2012-07-23):

The jQuery templates project was abandoned more than a year ago. jQuery模板项目已于一年多前被放弃。 It seems that Boris Moore continues his work with the new projects jsrender and jsviews . 鲍里斯·摩尔(Boris Moore)似乎继续他的新项目jsrenderjsviews的工作

John Resig Micro-Templating是很酷的解决方案

You might want to have a look at my jQuery templating plugin jQote2 . 您可能想看看我的jQuery模板插件jQote2 As far as usability and speed is concerned, I have yet to see a better templating solution (trust me, I've tried them all). 就可用性和速度而言,我还没有看到更好的模板解决方案(相信我,我已经尝试了所有方法)。

It has a built in closure compiler that let's you precompile your templates (handy if you want to keep your templates in .js files) and a caching mechanism. 它具有内置的关闭编译器,可让您预编译模板(如果希望将模板保存在.js文件中,则非常方便)和缓存机制。 The current version also comes with a couple of convenient methods that ease the pain of appending/prepending/replacing DOM nodes. 当前版本还提供了一些便捷的方法,可减轻添加/添加/替换DOM节点的麻烦。

Give it a try, you won't regret it. 试试看,您不会后悔的。 Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM