1 // If you want to make a custom template engine,
3 // [1] Inherit from this class (like ko.nativeTemplateEngine does)
4 // [2] Override 'renderTemplateSource', supplying a function with this signature:
6 // function (templateSource, bindingContext, options) {
7 // // - templateSource.text() is the text of the template you should render
8 // // - bindingContext.$data is the data you should pass into the template
9 // // - you might also want to make bindingContext.$parent, bindingContext.$parents,
10 // // and bindingContext.$root available in the template too
11 // // - options gives you access to any other properties set on "data-bind: { template: options }"
12 // // - templateDocument is the document object of the template
14 // // Return value: an array of DOM nodes
17 // [3] Override 'createJavaScriptEvaluatorBlock', supplying a function with this signature:
19 // function (script) {
20 // // Return value: Whatever syntax means "Evaluate the JavaScript statement 'script' and output the result"
21 // // For example, the jquery.tmpl template engine converts 'someScript' to '${ someScript }'
24 // This is only necessary if you want to allow data-bind attributes to reference arbitrary template variables.
25 // If you don't want to allow that, you can set the property 'allowTemplateRewriting' to false (like ko.nativeTemplateEngine does)
26 // and then you don't need to override 'createJavaScriptEvaluatorBlock'.
28 ko.templateEngine = function () { };
30 ko.templateEngine.prototype['renderTemplateSource'] = function (templateSource, bindingContext, options, templateDocument) {
31 throw new Error("Override renderTemplateSource");
34 ko.templateEngine.prototype['createJavaScriptEvaluatorBlock'] = function (script) {
35 throw new Error("Override createJavaScriptEvaluatorBlock");
38 ko.templateEngine.prototype['makeTemplateSource'] = function(template, templateDocument) {
40 if (typeof template == "string") {
41 templateDocument = templateDocument || document;
42 var elem = templateDocument.getElementById(template);
44 throw new Error("Cannot find template with ID " + template);
45 return new ko.templateSources.domElement(elem);
46 } else if ((template.nodeType == 1) || (template.nodeType == 8)) {
48 return new ko.templateSources.anonymousTemplate(template);
50 throw new Error("Unknown template type: " + template);
53 ko.templateEngine.prototype['renderTemplate'] = function (template, bindingContext, options, templateDocument) {
54 var templateSource = this['makeTemplateSource'](template, templateDocument);
55 return this['renderTemplateSource'](templateSource, bindingContext, options, templateDocument);
58 ko.templateEngine.prototype['isTemplateRewritten'] = function (template, templateDocument) {
59 // Skip rewriting if requested
60 if (this['allowTemplateRewriting'] === false)
62 return this['makeTemplateSource'](template, templateDocument)['data']("isRewritten");
65 ko.templateEngine.prototype['rewriteTemplate'] = function (template, rewriterCallback, templateDocument) {
66 var templateSource = this['makeTemplateSource'](template, templateDocument);
67 var rewritten = rewriterCallback(templateSource['text']());
68 templateSource['text'](rewritten);
69 templateSource['data']("isRewritten", true);
72 ko.exportSymbol('templateEngine', ko.templateEngine);