When building a web application with Knockout.js, working with more than a handful of templates is a common occurrence. As the number of templates in your application grows, adding additional script tags for these templates becomes a pain point and a maintenance issue. As an alternative, having less templates by using control-flow bindings (if, with, and foreach) in larger blocks of markup is also not an ideal solution, as it does not promote reuse and has the same maintenance concerns. A better technique is to move each template’s markup into individual files.
Why External Templates?
Defining templates in external files has a number of advantages:
* Files with an html extension and normal markup are generally handled better by most editors than trying to edit markup inside of a script tag.
* You have more encouragement to create reusable templates, as the pain of adding and maintaining more script tags is removed.
* The purpose and focus of smaller, more modular files is easier to ascertain.
* You have less potential for merge conflicts than when using a single large file, since you will have a better chance to work in separate files.
Your new post is loading...