Knockout.js 如何将模板与敲除、敲除映射和jsrender一起使用

Knockout.js 如何将模板与敲除、敲除映射和jsrender一起使用,knockout.js,knockout-mapping-plugin,jsrender,Knockout.js,Knockout Mapping Plugin,Jsrender,我想使用jsRender模板来呈现fullName,它只是firstName+''+lastName。它不是用数据呈现模板,而是呈现为{{=firstName}{{=lastName}。我怎样才能做到这一点 实例: JS: HTML: 模板: <script id="fullNameTemplate" type="text/x-jquery-tmpl"> {{=firstName}} {{=lastName}} </script> {{=firstName}{

我想使用jsRender模板来呈现
fullName
,它只是
firstName+''+lastName
。它不是用数据呈现模板,而是呈现为
{{=firstName}{{=lastName}
。我怎样才能做到这一点

实例:

JS:

HTML:


模板:

<script id="fullNameTemplate" type="text/x-jquery-tmpl">
  {{=firstName}} {{=lastName}}
</script>

{{=firstName}{{=lastName}

输出内容如下:

您必须安装自己的
模板引擎。以下是最终结果:

以下是相关代码:

ko.jsrenderTemplateEngine = function () { };
ko.jsrenderTemplateEngine.prototype = ko.utils.extend(new ko.templateEngine(), {
    renderTemplateSource: function (templateSource, bindingContext, options) {
        // Precompile the wrapping div for templating
        var precompiled = templateSource['data']('precompiled');
        if (!precompiled) {
            precompiled = $('<div>', { text: templateSource.text() });
            templateSource['data']('precompiled', precompiled);
        }
        // Unwrap observables
        var unwrapped = ko.mapping.toJS(bindingContext.$data);
        // Render and parseHTMLFragment
        return ko.utils.parseHtmlFragment(precompiled.render(unwrapped));
    }
});
ko.setTemplateEngine(new ko.jsrenderTemplateEngine());
ko.jsrenderTemplateEngine=function(){};
ko.jsrenderTemplateEngine.prototype=ko.utils.extend(新的ko.templateEngine(){
renderTemplateSource:函数(templateSource、bindingContext、选项){
//为模板化预编译包装div
var precompiled=templateSource['data']('precompiled');
如果(!预编译){
预编译=$('',{text:templateSource.text()});
templateSource['data']('precompiled',precompiled);
}
//展开观测值
var unwrapped=ko.mapping.toJS(bindingContext.$data);
//Render和parseHTMLFragment
返回ko.utils.parseHtmlFragment(预编译的.render(未包装));
}
});
setTemplateEngine(新的ko.jsrenderTemplateEngine());
我还更改了您的jsrender模板:

<script id="fullNameTemplate" type="text/x-jquery-tmpl">
    {{:firstName}} {{:lastName}}
</script>

{{:firstName}{{:lastName}
我从这里抄袭了基本代码设计:


另一方面,这个解决方案似乎并没有那么快,因为jsrender的最佳状态由于必须始终打开可观察对象而变得毫无用处。我认为最好改用Knockout的原生模板。

使用jQuery.tmpl的解决方案:文档中说,此时它将更新为jsRender,但显然不是。此处的文档:knockoutjs.com/Documentation/template binding.html

在这种情况下,您将如何使用Knockout的本机模板?如果您确实需要使用jsRender,请查看此问题的答案:。正确处理jsRender的模板引擎示例:
ko.jsrenderTemplateEngine = function () { };
ko.jsrenderTemplateEngine.prototype = ko.utils.extend(new ko.templateEngine(), {
    renderTemplateSource: function (templateSource, bindingContext, options) {
        // Precompile the wrapping div for templating
        var precompiled = templateSource['data']('precompiled');
        if (!precompiled) {
            precompiled = $('<div>', { text: templateSource.text() });
            templateSource['data']('precompiled', precompiled);
        }
        // Unwrap observables
        var unwrapped = ko.mapping.toJS(bindingContext.$data);
        // Render and parseHTMLFragment
        return ko.utils.parseHtmlFragment(precompiled.render(unwrapped));
    }
});
ko.setTemplateEngine(new ko.jsrenderTemplateEngine());
<script id="fullNameTemplate" type="text/x-jquery-tmpl">
    {{:firstName}} {{:lastName}}
</script>