Handlebars模板的文件扩展名 [英] Handlebars template's filename extension

查看:145
本文介绍了Handlebars模板的文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我改变了我的句柄模板的扩展名,并在调用handlebarjs的编译函数的函数中引用了它。



它工作得很好,没有问题。 >

但我很想知道其他人是否尝试过?
请让我知道,如果你认为这可能会导致任何原因的问题。



出于某种原因,我觉得扩展.handlebars是一个位长。我更喜欢将它保持为最多4个字符......类似.txt或.html。



请让我知道你是否看到这种方法的任何问题。例如,我将login.handlebars重命名为login.html



在getTemplate函数中(如下所示),我将调用这个模板进行编译



pre $ function getTemplate(name){

if(Handlebars .templates === undefined || Handlebars.templates [name] === undefined){
$ .ajax({
url:templates /+ name +.html,
成功:function(data){
if(Handlebars.templates === undefined){
Handlebars.templates = {};
}
Handlebars.templates [name] = Handlebars.compile(data);
},
async:false
});
}
返回Handlebars.templates [name];
}


解决方案

$ c> .handlebars ,以及Require.js和 Alex Sexton的require-处理器插件,并且这一切都没有问题。然而,更常见的后缀,以及我刚才提到的那个插件中的默认后缀是 .hbs (大概是因为 .hbs 是另一个文件类型尚未采用的3个字符的扩展名)。您可以使用 .hbs .handlebars ,甚至是不同的扩展名,对于任何类型的库(例如Require),它都可以正常工作其实很重要。当然没有任何保证,但是因为没有官方扩展库,作者通常比硬编码更好。



我会谨慎使用<$ c $对于这些文件,尽管...> c> .htm 或 .html 除非你有一个非常挑剔的IDE。大多数IDE可以设置为将 .hbs 视为HTML文件,用于语法着色,而不是。如果你的不能,那么 .htm 可能是有道理的。否则,我会保持文件扩展名不同,以便您可以轻松区分两种类型的文件。


I changed my handlebar template's extension and referred to the same in the function which called handlebarjs' compile function.

It worked perfectly fine with no issues.

But I'm curious to know if anyone else tried that? Please let me know if you think this could cause problems down the road for any reason.

For some reason I feel that the very extension .handlebars is a bit long. I prefer to keep it to a max of 4 chars ... something like .txt or .html.

Please let me know if you see any issues with this approach.

For example, I renamed login.handlebars to login.html

In the getTemplate function (as shown below), I will call this template for compilation

function getTemplate(name) {

if (Handlebars.templates === undefined || Handlebars.templates[name] === undefined) {
    $.ajax({
       url : "templates/" + name + ".html",
       success : function(data) {
       if (Handlebars.templates === undefined) {
           Handlebars.templates = {};
       }
       Handlebars.templates[name] = Handlebars.compile(data);
       },
      async : false
    });
    }
    return Handlebars.templates[name];
}

解决方案

My shop uses .handlebars, along with Require.js and Alex Sexton's require-handlebars plug-in, and it all works without issue. The far more common suffix however, and the default one in that plug-in I just mentioned, is .hbs (presumably because .hbs is a 3-character extension not already taken by another file type).

You can for example use .hbs, .handlebars, or even a different extension for that matter, and it should work just fine with any sort of library (eg. Require) where the suffix could actually matter. There are no guarantees of course, but because there is no official extension library authors generally know better than to hard-code one.

I would caution against using .htm or .html for these files though ... unless you have a really picky IDE. Most IDEs can be set to treat .hbs as if it were an HTML files, for syntax coloring and what not. If your's can't, then .htm might make sense. Otherwise I'd keep the file extension distinct, so that you can easily distinguish between the two types of files.

这篇关于Handlebars模板的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆