Jan Vercauteren
Jan Vercauteren

Reputation: 128

knockoutjs 1.3PRE "This template engine does not support anonymous templates nested within its templates"

I am having a problem with the new anonymous template engine. It cannot use nested templates. I get the error: "This template engine does not support anonymous templates nested within its templates".

My question: how do I force knockoutJS to use the jquery templates engine and not the new one. (until it also supports nested templates off course)

Thank you

Kindest regards Jan

Upvotes: 8

Views: 2259

Answers (2)

RP Niemeyer
RP Niemeyer

Reputation: 114792

A couple things that should help clear this up:

  • If you reference jquery.tmpl.js before Knockout.js, then KO will set the jQueryTmplTemplateEngine as the default.
  • The jQueryTmplTemplateEngine does not support anonymous templates. This means that you can't use the control-flow bindings like foreach, if, ifnot, and with within it. You can still use the template binding (including nested templates).
  • You can control the default template engine by calling ko.setTemplateEngine(). You would pass an instance of the engine that you want to use like ko.setTemplateEngine(new ko.nativeTemplateEngine)
  • You can also pass the template engine to the template binding in the templateEngine parameter.

So, for your case, you would likely want to remove the reference to jquery.tmpl.js, so that the native template engine is being used by default. The native template engine does support nested templates (anonymous or named).

Quick sample showing a named template with anonymous templates inside of it using the native template engine: http://jsfiddle.net/rniemeyer/GXFYB/

Upvotes: 21

Mark Robinson
Mark Robinson

Reputation: 13278

I got this problem when I removed the include to the tmpl js library but re-adding it fixed the problem. Are you sure you have included the jquery.tmpl.js library?

<script src='..<some path>../jquery.tmpl.js")' type="text/javascript"></script>

Also, I include the tmpl library first and the knockout library second. Maybe this is a factor?

Upvotes: 0

Related Questions