English 中文(简体)
KnockoutJS - Templating
  • 时间:2024-11-03

KnockoutJS - Templating


Previous Page Next Page  

Template is a set of DOM elements which can be used repetitively. Templating makes it easy to build complex apppcations due to its property of minimizing duppcation of DOM elements.

There are 2 ways of creating templates.

    Native templating − This method supports the control flow bindings such as foreach, with, and if. These bindings capture HTML markup existing in the element and use it as template for random items. No external pbrary is required for this templating.

    String-based templating − KO connects to the third party engine to pass ViewModel values into it and injects the resulting markup into the document. For example, JQuery.tmpl and Underscore Engine.

Syntax

template: <parameter-value>

<script type = "text/html" id = "template-name">
   ...
   ...   // DOM elemets to be processed
   ...
</script>

Note that type is provided as text/html in the script block to notify KO that, it is not an executable block rather just a template block which needs to be rendered.

Parameters

Combination of the following properties can be sent as parameter-value to template.

    name − This represents the name of the template.

    nodes − This represents an array of DOM nodes to be used as the template. This parameter is ignored if the name parameter is passed.

    data − This is nothing but data to be shown via the template.

    if − Template will be served if the given condition results in true or true-pke value.

    foreach − To serve template in foreach format.

    as − This is just to create an apas in foreach element.

    afterAdd, afterRender, beforeRemove − These are all to represent callable functions to be executed depending on the operation performed.

Observations

Rendering a named Template

Templates are defined imppcitly by HTML markup inside DOM when used with control flow bindings. However if you want to, you can factor out templates into a separate element and then reference them by name.

Example

<!DOCTYPE html>
   <head>
      <title>KnockoutJS Templating - Named Template</title>
      <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.3.0.js"
         type = "text/javascript"></script>
   </head>

   <body>
      <h2>Friends List</h2>
      Here are the Friends from your contact page:
      <span data-bind = "template: { name:  friend-template , data: friend1 }"></span>
      <span data-bind = "template: { name:  friend-template , data: friend2 }"></span>

      <script type = "text/html" id = "friend-template">
         <h3 data-bind = "text: name"></h3>
         <p>Contact Number: <span data-bind = "text: contactNumber"></span></p>
         <p>Email-id: <span data-bind = "text: email"></span></p>
      </script>

      <script type = "text/javascript">
         function MyViewModel() {
            this.friend1 = { 
               name:  Smith , 
               contactNumber: 4556750345, 
               email:  smith123@gmail.com  
            };
            
            this.friend2 = { 
               name:  Jack , 
               contactNumber: 6789358001, 
               email:  jack123@yahoo.com  
            };
         }

         var vm = new MyViewModel();
         ko.applyBindings(vm);
      </script>
      
   </body>
</html>

Output

Let s carry out the following steps to see how the above code works −

    Save the above code in template-named.htm file.

    Open this HTML file in a browser.

    Here, friend-template is used 2 times.