• .NET Developer Tools DevTools

    UI controls for ASP.NET AJAX, MVC, WPF,
    Silverlight, Windows 8 and Windows Phone

  • Hybrid Mobile Development Icenium

    Cross-platform Mobile Development Tool
    with cloud-based architecture

  • HTML5 / JavaScript Development Kendo UI

    Everything you need to build sites and
    mobile apps with JavaScript and HTML5

  • Testing Tools TestStudio

    One easy tool for Functional, Performance,
    Load and Mobile software testing

  • Web Presence Platform Sitefinity CMS

    Everything for your online business - content
    management, ecommerce, emarketing

  • Agile Project Management TeamPulse

    Simple and intuitive project management
    and collaboration software

Contact us

We are here for you.
  • usa+1‒888‒365‒2779
  • uk+44‒20‒7291‒0580
  • bg+359‒2‒8099850
  • de+49‒89‒2441642‒70
  • au+61‒2‒8090‒1465
  • emailsales@telerik.com
Your account Access to your products, updates and support
Telerik Product Families
  • Your Account
    Your Account
    Log in
  • ABOUT US

    About Telerik

    • Company
    • Press Center
    • Customers
    • Community
    • Careers
    • Contacts
Kendo UI - The way of HTML5
Products ▼
Kendo UI Web Kendo UI Mobile Kendo UI DataViz Server Side Wrappers
Demos Purchase Download
Blogs Documentation
Support ▼
Premium Forums StackOverflow Forums
Resources ▼

Featured Resource

Kendo UI Dojo


Blogs Code Library Demos Documentation FAQ Testing
Premium Forums Roadmap User Voice Videos Webinars More Resources
Contact Us Search
 

Blogs

Using Kendo UI templates with Backbone

Friday, January 11, 2013 by Kendo UI Team Blog | Comments 3

Backbone and Kendo make a beautiful combination as we've already seen. But this dynamic duo doesn't stop with the use of the Kendo control suite. Backbone's flexibility and defining characteristic of open and unopinionated structure allow it to be combined with more of Kenod's capabilities than just the controls. For example, we can use Kendo's templates with Backbone views very easily. Before I dive in to that code, though, I'll take a moment to review Backbone's basic view rendering.

Rendering A Backbone.View

Most Backbone.View objects have a render method that is used to generate the HTML content for the view. The actual mechanics of generating that HTML, though, is left up to the developer and team that is writing the application. Frameworks like MarionetteJS, ChaplinJS and others, abstract this detail away by providing defaults that can be overridden easily. But when writing a raw Backbone.View, the choice is left for you to make.

You could easily hard code a string with HTML tags in it:

var MyView = Backbone.View.extend({ 

  render: function(){
    var content = "<h2>Hello there!</h2>";
this.$el.html(content); } });

Or you can use a template engine such as Underscore.js' built in template function, Handlebars, Mustache, Jade, or any of a dozen other JavaScript template engines, including Kendo UI's.

The majority of these template engines have a two-step process for rendering the desired content:

  1. Compile the raw template string in to a JavaScript function
  2. Call the function and apply some data to the compiled template

When working with Backbone, there are a few additional steps that need to be injected such as pulling the raw template content from the DOM and building a JavaScript object literal with the data that the compiled template needs. You also need to populate the view with the resulting HTML string when the template has been run.

The end result often looks like this (using Underscore.js - a prerequisite of Backbone.js and a common choice for rendering Backbone views):

<script type="text/html" id="about-me-template">
  <h2>About <%= name %></h2>
  <ul>
    <li>Employer: <%= employer %></li>
    <li>Title: <%= title %></li>
    <li>Job Description: <%= description %></li>
  </ul>
</script>

var MyView = Backbone.View.extend({

  render: function(){

    // step 1: grab the template from the DOM
    var templateHtml = $("#about-me-template").html();

    // step 2: compile the template
    var compiled = _.template(templateHtml);

    // step 3: build an object literal with the data
    // that we want to display in the template
    var data = {
      name: "Derick Bailey",
      employer: "Telerik",
      title: "Developer Advocate for Kendo UI"
    };

    // step 4: apply the data to the template to
    // produce a raw string that contains the HTML
    var content = compiled(data);

    // step 5: populate the view with the result
    this.$el.html(content);
  }
});

Rendering A Backbone.View With A Kendo UI Template

Having established a baseline for rendering a Backbone.View, here is what it takes to convert the above example in to a Kendo UI template:

<script type="text/html" id="about-me-template">
  <h2>About #= name #</h2>
  <ul>
    <li>Employer: #= employer #</li>
    <li>Title: #= title #</li>
  </ul>
</script>  

var MyView = Backbone.View.extend({

  render: function(){

    // step 1: grab the template from the DOM
    var templateHtml = $("#about-me-template").html();

    // step 2: compile the template
    var compiled = kendo.template(templateHtml);

    // step 3: build an object literal with the data
    // that we want to display in the template
    var data = {
      name: "Derick Bailey",
      employer: "Telerik",
      title: "Developer Advocate for Kendo UI"
    };

    // step 4: apply the data to the template to
    // produce a raw string that contains the HTML
    var content = compiled(data);

    // step 5: populate the view with the result
    this.$el.html(content);
  }
});

The result can be found in this JSFiddle.

Note that there isn't much of a difference between the underscore and Kendo UI version. In the template, underscore uses <%= ... %> blocks to denote data while Kendo uses #= ... # blocks. Then in the view itself, we only needed to change one line in the render method to say kendo.template instead of _.template. Other than that, the view and template are the same between the two versions of this example.

More Than Simple Templates

As you can see, Backbone's flexibility allows it to be combined with Kendo's capabilities easily. And Kendo offers more than just the simple templates that I've shown above. It has many more options and capabilities that can be found in the documentation and demos, and provides additional functionality such as input validation, globalization and more that can be added in to a Backbone application.

About the Author
Derick Bailey is a developer, trainer, speaker and Developer Advocate for Kendo UI. He's been slinging code since the late 80’s and doing it professionally since the mid 90's. These days, Derick spends his time primarily writing javascript with back-end languages of all types, including Ruby, NodeJS, .NET and more. Derick blogs at DerickBailey.LosTechies.com, produces screencasts at WatchMeCode.net, tweets as @derickbailey and provides support and assistance for JavaScript, BackboneJS, MarionetteJS and much more around the web.

3 Comments

  1. 1 Tim Branyen 11 Jan 2013
    I noticed you removed `return this;` from your render calls.  Is this intentional? Just wondering if you've found that chaining may not always be desirable.

    I've been pondering the appropriate return value of `render`.
  2. 2 Derick Bailey 11 Jan 2013
    Hey Tim,

    I think that was partially intentional, and partially just forgetting to add it. :) I very rarely use the return value of render to chain calls, so I usually don't include it when I'm writing my own views.

    It's more a personal preference than anything. I don't like seeing like

      $("#foo").html(view.render().$el);

    there's too much code and behavior in that one line, for my taste. I prefer seeing more explicit code like 

      view.render();
      $("#foo").html(view.$el);

    But for consistency with Backbone's view, I should update this post to include the "return this;".
  3. 3 Nizzy 20 Feb 2013
    Kendo UI template is the fastest template engine according to performance testings, but you can't just it as in in mobile applications. Kendo UI library is huge. You need to extract their template engine itself and use it within mobile apps.

Comment

  1. Click to add

  2. Click to add

  3. Click to add

  4.    
     
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
     
      
       
Blogs feed
Categories

  • Tutorials (26)
  • Release (33)
  • Browsers (7)
  • Extensions (3)
  • Tip of the Week (10)
  • Videos (5)
  • Concepts and Theory (13)
  • Misc. (25)
  • Framework Constructs (6)
  • Mobile (6)
  • UI Widgets (5)
  • Blogs (1)
Archive
  • 2013 May (6)
  • 2013 April (10)
  • 2013 March (9)
  • 2013 February (12)
  • 2013 January (10)
  • 2012 December (9)
  • 2012 November (11)
  • 2012 October (6)
  • 2012 September (7)
  • 2012 August (8)
  • 2012 July (10)
  • 2012 June (8)
  • 2012 May (10)
  • 2012 April (7)
  • 2012 March (13)
  • 2012 February (10)
  • 2012 January (6)
  • 2011 December (10)
  • 2011 November (4)
  • 2011 October (6)
  • 2011 September (5)
  • 2011 August (9)
Home Web Mobile DataViz Server Wrappers Whitepapers Surveys Chrome Icenium Contact Us

Kendo UI framework is developed by Telerik - a leading provider of UI components for web, desktop and mobile applications. Trusted by over 100,000 customers worldwide for our devotion to quality and industry-best technical support, Telerik helps professionals maximize their productivity and "deliver more than expected" every day.

kendoui - powered by html5, css3 & jquery
get social
  • Twitter
  • Facebook
  • Google plus
  • RSS
Privacy Policy | Branding Guidelines
Powered by Sitefinity CMS

Copyright © 2011 - 2013 Telerik Inc. All rights reserved.