简体   繁体   English

Javascript模板系统 - PURE,EJS,jquery插件?

[英]Javascript template system - PURE, EJS, jquery plugin?

has anybody used a javascript template system? 有人用过javascript模板系统吗? I used to use the one that is embedded with JavascriptMVC but i now doing server side development so i wanted a more streamlined/thinner version.. 我曾经使用嵌入JavascriptMVC的那个,但我现在正在进行服务器端开发,所以我想要一个更简化/更薄的版本..

I have found 2. 1 is EJS which is the part that is included with JavascriptMVC 我发现2. 1是EJS,这是JavascriptMVC中包含的部分

http://embeddedjs.com/ http://embeddedjs.com/

and the other is Pure- which you can use with jquery 另一个是Pure-你可以用jquery

http://beebole.com/pure/index.html http://beebole.com/pure/index.html

has anyone had any experience with either, or is there something else that i have failed to find? 谁有任何经验,或者还有其他我找不到的东西? maybe a jquery type plugin or something.. 也许是一个jquery类型的插件或者什么..

basically i need to replace parts of a HTML document within javascript at runtine without a call to the server. 基本上我需要在runtine中的javascript中替换部分HTML文档,而无需调用服务器。

but my html replacement code needs to be saved in an external file and not embedded within js 但我的html替换代码需要保存在外部文件中,而不是嵌入在js中

Any help really appreciated 任何帮助真的很感激

thanks 谢谢

HAML Coffee. HAML咖啡。
Combining the two best meta-languages. 结合两种最好的元语言。

https://github.com/9elements/haml-coffee https://github.com/9elements/haml-coffee

Hey, I used this a time or two, and it was pretty simple. 嘿,我用了一两次,这很简单。 It's from the guy who wrote jquery. 这是来自编写jquery的人。

http://ejohn.org/blog/javascript-micro-templating/ http://ejohn.org/blog/javascript-micro-templating/

Here is one implemented in jQuery for the Smarty templating language. 这是在jQuery中为Smarty模板语言实现的。 http://www.balupton.com/sandbox/jquery-smarty/demo/ http://www.balupton.com/sandbox/jquery-smarty/demo/

One impressive feature is the support for dynamic updates. 一个令人印象深刻的功能是支持动态更新。 So if you update a template variable, it will update anywhere in the template where that variable is used. 因此,如果更新模板变量,它将更新模板中使用该变量的任何位置。 Pretty nifty. 很漂亮。

You can also hook into variable changes using a onchange event. 您还可以使用onchange事件挂接到变量更改。 So that is useful for say performing effects or AJAX when say the variable "page" changes ;-) 因此,当说变量“page”改变时,表示效果或AJAX很有用;-)

If you use the jQuery framework by any chance, I could recommend a plugin called jQote to you. 如果您有任何机会使用jQuery框架,我可以向您推荐一个名为jQote的插件。 Some guy took John Resig's engine and packed it into a plugin, making it easy as hell to do javascript templating. 有些人拿走了John Resig的引擎并将其打包成一个插件,这样就可以轻松完成javascript模板操作。

http://aefxx.com/jquery-plugins/jqote http://aefxx.com/jquery-plugins/jqote

Cheers! 干杯!

Prototype Template is quick and easy, if prototype is an option. 如果原型是一个选项, 原型模板是快速和简单的。 If you really need a jQuery plugin, I wrote a port of it (shameless plug). 如果你真的需要一个jQuery插件,我写了一个端口 (无耻插件)。

I've used EJS extensively. 我广泛使用了EJS。 Coming from a Rails background, it's been perfect for my needs since it's so similar to ERB. 来自Rails的背景,它完全符合我的需求,因为它与ERB非常相似。

I'd recommend it. 我推荐它。 It's being actively maintained and the developers are extremely responsive. 它正在积极维护,开发人员反应迅速。 Also, in the benchmarks I've run, it's very fast. 此外,在我运行的基准测试中,它的速度非常快。 I'm using it for a mobile site for iPhone/Android. 我正在将它用于iPhone / Android的移动网站。

For a few others, check out this blog post: http://www.viget.com/extend/benchmarking-javascript-templating-libraries/ 对于其他一些人,请查看此博客文章: http//www.viget.com/extend/benchmarking-javascript-templating-libraries/

Here's a stand alone, custom solution that I've written that is incredibly small and mimics Prototype's template system : 这是一个独立的定制解决方案,我写的非常小,模仿Prototype的模板系统

var templater = function(template, tokens, tokenIdentifier) {
    tokenIdentifier = tokenIdentifier || "$";
    // Decode HTML encoded template tokens %7B -> {, %7D -> }
    template = template.replace(
        new RegExp("\\" + tokenIdentifier + "%7B(\\w+)%7D", "gi"), 
        tokenIdentifier + "{$1}"
    );

    for ( var i in tokens ) {
        if ( tokens.hasOwnProperty(i) ) {
            template = template.replace(
                new RegExp("\\"+tokenIdentifier+"\\{"+i+"\\}", "g"),
                tokens[i]
            );
        }
    }

    return template;
};

Usage: 用法:

templater("Hi, my name is ${name}", {name: "Bobo the Clown"});
// The token identifier defaults to $, but can be changed arbitrarily
templater("#{title} #{surname} #{verb} #{noun}", {title: "Dr.", surname: "Amazing", verb: "packed", noun: "sand"}, "#");

Output: 输出:

Hi, my name is Bobo the Clown
Dr. Amazing packed sand

I agree with Jage. 我同意Jage。

http://ejohn.org/blog/javascript-micro-templating/ is very simple and quick to implement. http://ejohn.org/blog/javascript-micro-templating/非常简单,实施起来很快。 You don't have to do loads of work to get a good result. 您无需做大量工作即可获得良好的结果。

Take a look at hacktl.js http://code.google.com/p/hacktl/ . 看看hacktl.js http://code.google.com/p/hacktl/ Light and simple 轻巧简单

Mustache.js has worked well for me so far. 到目前为止, Mustache.js对我来说效果很好。 It's available for many server-side language too (Ruby, Python, Clojure, etc.) so you can use it in multiple contexts. 它也适用于许多服务器端语言(Ruby,Python,Clojure等),因此您可以在多个上下文中使用它。

如果您使用的是Script#,您可能需要考虑SharpTemplate ,这是一种强类型,超高效的HTML模板引擎。

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

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