Quantcast
Channel: Infragistics Community
Viewing all articles
Browse latest Browse all 2374

Using different template engines with Ignite UI controls

$
0
0

header imageCreating an application is not only about making it functional it is about how the users will react to it. Did you know that when you create a mobile app for example a user decides in the first 10 seconds whether he likes it or not? So imagine how will you feel if the application is slow and you have to wait these seconds only for it to render? For us in Infragistics the User Experience is a top priority, that is why in the new 14.1 release we went through some major changes in order to optimize the UX of our users.

First we deprecated the rowTemplate option of the igGrid and thus we increased the templated igGrid rendering with more than 40%. And second we removed the dependence between the template and the syntaxes of the Infragistics Template Engine.  The second alteration was driven by our desire users to feel free to use any Template Engine they like. In this blog we are going to go through some of the most popular template engines and see how to use them with the Infragistics Ignite UI controls.

What’s New in 14.1 Template Engine

A major changes in the new 14.1 release is that the rowTemplate option has been deprecated. This is a breaking change from the previous versions’ functionality. The igGrid control now uses only the templates for individual columns. As before you can use templates only for some of them (will be rendered through the template engine) and  the other columns will render along with the grid render. This actually increases the render of the grid with more than 40% and thus with this alteration we did not only make the control faster but we also optimized the user experience when interacting with it.

Let’s see the grid in action. For the demo I’m going to use the Infragistics Template Engine. For some of the columns I‘m going to use different templates and the other I will leave unattached with template. Setting the template option separately to every column is very handy because it gives you the opportunity to manipulate them differently. You can check out the demo in jsFiddle to see it for yourself.

Condition Template:

  1. <scriptid="colTmpl"type="text/template">
  2.  
  3.     {{if${SoldLastMonth}<=${SoldThisMonth} }}         
  4.     <imgwidth='10'height='15'src='http://igniteui.com/images/samples/templating-engine/colTemplateWithConditionalCell/arrowUp.gif'/>
  5.     {{else}}
  6.     <imgwidth='10'height='15'src='http://igniteui.com/images/samples/templating-engine/colTemplateWithConditionalCell/arrowDown.gif'/>
  7.     {{/if}}
  8.  
  9. </script>

Grid Initialization:

  1. $("#grid1").igGrid({
  2.     primaryKey: "ProductID",
  3.     width: '700px',
  4.     height: '600px',
  5.     rowVirtualization: true,
  6.     virtualizationMode: "fixed",
  7.     avgRowHeight: "60",
  8.     autoGenerateColumns: false,
  9.     columns: [
  10.         { headerText: "Product ID", key: "ProductID", dataType: "number" },
  11.         { headerText: "Units in Stock", key: "UnitsInStock", dataType: "number", template: "Units in Stock: ${UnitsInStock}" },
  12.         { headerText: "Product Description", key: "ProductDescription", dataType: "string" },
  13.         { headerText: "Unit Price", key: "UnitPrice", dataType: "string", template: "Unit Price: ${UnitPrice}" },
  14.         { headerText: "Sold last month", key: "SoldLastMonth", dataType: "number", template: "Sold last month: ${SoldLastMonth}" },
  15.         { headerText: "Sold this month", key: "SoldThisMonth", dataType: "number", template: "Sold this month: ${SoldThisMonth}" + $("#colTmpl").html() }
  16.     ],
  17.     tabIndex: 1,
  18.     dataSource: namedData
  19. });

Another change is that the template is no longer tied to the Infragistics Template Engine syntax which gives you the opportunity to use any other template system and override the template. Further in the blog I am going to show you how to do that in details.

Fire up Ignite UI controls with different template engines

The increased use of JavaScript led to the development of many JS libraries to support the applications developed in this language. When it comes to client side data-binding method it is convenient to use one of those templating libraries. As you know Infragistics has a great Templating Engine and you can easily use it to create templates and use them with the Ignite UI controls. But in case that is not enough for you and you want to use another Framework that is not a problem anymore - you just have to override the template. We are going to look at some of the most popular template engines and see how to do that and fire up some of the Ignite UI controls with the different frameworks. By overriding the Ignite UI template function you can easily use different template engines. The main idea is that every control that has a template option goes through the template function. This function takes two arguments – template as string and the data object to be rendered (called automatically by the controls) and should return the final result as HTML string.

HandleBars.js and Mustache.js

Mustache for example is often considered as a base for the JavaScript templating. This template system is described as a “logic-less” system because it lacks any explicit control flow statements.  On top of it is build the HandleBars  template engine. This is one of the most popular templating library and it adds a lot of helpers to Mustache. The syntaxes of these two templates is similar - they both use double curly braces.

If you choose to use HandleBars.js to render a template you should first create the template  and then you can compile it by using the Handlebars.compile function. To get the HTML result of the evaluated template you have to execute the template with the data that you want to use.

  1. $.ig.tmpl = function (tmpl, data) {            
  2.     var template = Handlebars.compile(tmpl);
  3.     return template(data);
  4. };
  5.  
  6. $("#grid1").igGrid({
  7.         primaryKey: "ProductID",
  8.         width: '700px',
  9.         height: '600px',
  10.         rowVirtualization: true,
  11.         virtualizationMode: "fixed",
  12.         avgRowHeight: "60",
  13.         autoGenerateColumns: false,
  14.         columns: [
  15.                { headerText: "Product ID", key: "ProductID", dataType: "number", template: "PID: {{ProductID}}" },
  16.             { headerText: "Units in Stock", key: "UnitsInStock", dataType: "number", template: "In stock: {{UnitsInStock}}" },
  17.             { headerText: "Product Description", key: "ProductDescription", dataType: "string", template: "Desc: {{ProductDescription}}" },
  18.             { headerText: "DateAdded", key: "DateAdded", dataType: "date", hidden: true, template: "Date: {{DateAdded}}" },
  19.             { headerText: "Unit Price", key: "UnitPrice", dataType: "string", template: "unitprice: {{UnitPrice}}" }
  20.         ],               
  21.         tabIndex: 1,                
  22.         dataSource: namedData
  23.     });

The template for Mustache.js is the same as the one we use with HandleBars because they have similar syntax. The difference appears in the override function – you have to use the Mustache.render to compile the template.

  1. $.ig.tmpl = function (tmpl, data) {
  2.     var template = Mustache.render(tmpl, data);
  3.     return template;
  4. };

 

Ignite UI igGrid with Hadlebars.js template

On jsFiddle can check out a demo using the igGrid with HandleBars Template Engine .

Underscore.js

The Underscore is a JavaScript library that provides  functional helpers without extending any built-in objects.  As any template Underscore has its specific syntax so you can either use this syntax when you call the template or you can change the Underscore’s template settings to use different symbols. This can be accomplished by defining a regex to  match the expression that should be evaluated.

Using Underscore syntax:

  1. $.ig.tmpl = function (tmpl, data) {
  2.     var template = _.template(tmpl);          
  3.     return template(data);
  4. };
  5.  
  6.  
  7. $("#grid1").igGrid({
  8.         primaryKey: "ProductID",
  9.         width: '700px',
  10.         height: '600px',
  11.         rowVirtualization: true,
  12.         virtualizationMode: "fixed",
  13.         avgRowHeight: "60",
  14.         autoGenerateColumns: false,
  15.         columns: [
  16.                { headerText: "Product ID", key: "ProductID", dataType: "number", template: "PID: <%- ProductID %>" },
  17.             { headerText: "Units in Stock", key: "UnitsInStock", dataType: "number", template: "In stock: <%- UnitsInStock %>" },
  18.             { headerText: "Product Description", key: "ProductDescription", dataType: "string", template: "Desc: <%- ProductDescription %>" },
  19.             { headerText: "DateAdded", key: "DateAdded", dataType: "date", hidden: true, template: "Date: <%- DateAdded %>" },
  20.             { headerText: "Unit Price", key: "UnitPrice", dataType: "string", template: "unitprice: <%- UnitPrice %>" }
  21.         ],               
  22.         tabIndex: 1,                
  23.         dataSource: namedData
  24.     });
  25. });

Changed Underscore template setting to perform HandleBars.js style template:

  1.     $.ig.tmpl = function (tmpl, data) {
  2.  
  3.         _.templateSettings = {
  4.             interpolate: /\{\{(.+?)\}\}/g
  5.         };
  6.         var template = _.template(tmpl);          
  7.         return template(data);
  8.     };
  9.  
  10.  
  11.     $("#grid1").igGrid({
  12.             primaryKey: "ProductID",
  13.             width: '700px',
  14.             height: '600px',
  15.             rowVirtualization: true,
  16.             virtualizationMode: "fixed",
  17.             avgRowHeight: "60",
  18.             autoGenerateColumns: false,
  19.             columns: [
  20.                    { headerText: "Product ID", key: "ProductID", dataType: "number", template: "PID: {{ProductID}}" },
  21.                 { headerText: "Units in Stock", key: "UnitsInStock", dataType: "number", template: "In stock: {{UnitsInStock}}" },
  22.                 { headerText: "Product Description", key: "ProductDescription", dataType: "string", template: "Desc: {{ProductDescription}}" },
  23.                 { headerText: "DateAdded", key: "DateAdded", dataType: "date", hidden: true, template: "Date: {{DateAdded}}" },
  24.                 { headerText: "Unit Price", key: "UnitPrice", dataType: "string", template: "unitprice: {{UnitPrice}}" }
  25.             ],               
  26.             tabIndex: 1,                
  27.             dataSource: namedData
  28.         });

On jsFiddle you can see how to use igCombo with Underscore.

Ignite UI igCombo with Underscore.js

jsRender

I have already written a blog about the jsRender integration with the igGrid, but that was about the 13.2 release. Now after the deprecation of the rowTemplate let’s go back and see how to change the Heat map sample. It is actually pretty simple all you need to do is set different templates to the columns that you want to participate in the establishment of the heat map. You can check out a demo of the sample in jsFiddle.

  1. $("#grid").igGrid({
  2.     width: "600px",
  3.     height: "500px",
  4.     dataSource: busy,
  5.     columns: [
  6.         { headerText: "Id", key: "ID" },
  7.         { headerText: "Name", key: "Name" },
  8.         { headerText: "Mon", key: "Mon", template: "<td style='background: {{:~colorChange(Mon)}};'><b>{{>Mon}}%</b></td>" },
  9.         { headerText: "Tue", key: "Tue", template: "<td style='background: {{:~colorChange(Tue)}};'><b>{{>Tue}}%</b></td>" },
  10.         { headerText: "Wed", key: "Wed", template: "<td style='background: {{:~colorChange(Wed)}};'><b>{{>Wed}}%</b></td>" },
  11.         { headerText: "Thu", key: "Thu", template: "<td style='background: {{:~colorChange(Thu)}};'><b>{{>Thu}}%</b></td>" },
  12.         { headerText: "Fri", key: "Fri", template: "<td style='background: {{:~colorChange(Fri)}};'><b>{{>Fri}}%</b></td>" }
  13.     ],
  14.     templatingEngine: "jsrender"
  15. });

Ignite UI igGrid heat map with jsRender

By following the above shown examples you can use any template engine you like and override the template in a similar way.

Summary

Every web developer has his preferences when it comes to using a JS libraries to help you create stunning applications. And what is better than being able to use them along with Ignite UI controls.  With the latest release of our Web widgets we give you the opportunity to blend your favorite template engine with our controls by overriding the template function. Our desire was to make you feel comfortable and thus create your apps faster and easier.

 

You can follow us on Twitter @Infragistics and stay in touch on Facebook, Google+ and LinkedIn!


Viewing all articles
Browse latest Browse all 2374

Trending Articles