预编译jsrender模板 [英] pre-compiling jsrender templates

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

问题描述

我有几个在所有页面上使用的jsRender模板.与其使用script标签方法,不如预编译它们并将结果函数放在外部js文件中.

I have a couple of jsRender templates that are used on all pages. Instead of using the script tag method, I would prefer to precompile them and put resulting functions in an external js file.

我看到了上一个问题,但此方法没有似乎不再工作了,或者我做错了事.

I saw a previous question that asked this but the method described doesn't seem to work anymore or I am doing something wrong.

我从控制台调用了$ .templates(#myTemplate"),msanjay建议并复制/重命名了该匿名函数,并得到了类似的内容(对我未经训练的人来说有点奇怪):

I called $.templates("#myTemplate") from the console which msanjay suggested and copied/renamed the anonymous function and get something like this (which looks a bit odd to my untrained eye):

function myTemplate(data, view, j, b, u) {
    var j = j || jQuery.views, h = j.converters.html, ret; try {

        return "<div...";

    } catch (e) { return j._err(e); }
}

如果我随后尝试通过调用var html = $ .render(data,myTemplate);来使用此模板,我收到一个错误:

If I then try and use this template by calling var html = $.render( data, myTemplate ); I get an error:

未捕获的TypeError:对象函数(a,b)的属性"render" {返回新的p.fn.init(a,b,c)}不是函数

不确定jsRender是否已更改,并且不再可行,或者我做错了什么,但是任何指导都将不胜感激.

Not sure if the jsRender has changed and this is no longer possible or if I am doing something wrong, but any guidance would be highly appreciated.

推荐答案

您可能会通过

You might get what you want by compiling templates from strings. In brief, it looks like this:

$.templates({
    myTemplateName: " blah blah {{>something}} blah "
});

var tempHtml = $.render.myTemplateName(myData);

我意识到您可能会因为预编译模板会节省一些执行时间而陷入困境,但是以我的经验,jsrender是如此之快,我从未注意到它.

I realize you may be stuck on the notion of having precompiled templates on the notion that it will save you some execution time, but in my experience jsrender is so fast I never notice it.

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

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