Your First Conditional Template with Dart Web Components

As if data binding and custom elements with Dart Web UI isn't enough, you can also conditionally render blocks of elements via simple declarative attributes! The conditionals are "live" and are re-evaluated when data changes, keeping your page dynamic and up-to-date.

(You may want to read Your First Model Driven View with Dart, which contains more information on getting started and configure the necessary tools. This post assumes you've read my previous posts on Web Components and Dart.)

Conditionals templates

With Dart Web UI, you can conditionally display elements. For example, you can declaratively say "If the checkbox is checked, then display the thank you notice." Here's an example:

 <template if="acceptLicense">  
  <p>We'll send you an activation key to your email.</p>  

In the above example, the <h2> and <p> will only be added to the DOM if the acceptLicense variable becomes true. If acceptLicense becomes false, the template contents are removed from the DOM.

A <template> is an inert set of elements, which is parsed but not activated and not immediately added to the DOM. To render the elements in a template, the conditional in the if must become true. You can use any Dart code that results in a boolean for the conditional.

Full example

Here is a complete Dart Web UI page with a conditional template. The below example also demonstrates data binding with a checkbox.

 <!DOCTYPE html>  
   <meta charset="utf-8">  
   <title>Simple Conditional</title>  
   <link rel="stylesheet" href="App.css">  
   <h1>Simple Conditional with Data Binding</h1>  
     <input type="checkbox" id="accept-license" bind-checked="acceptLicense">  
     I accept the license  
   <template if="acceptLicense">  
    <p>We'll send the activation key to your email.</p>  
   <script type="application/dart">  
    bool acceptLicense = false;  
    main() { }  
   <script src=""></script>  

To compile the above file into vanilla JavaScript and HTML, learn more about the Dart Web Components tools.

Working example

Enjoy this fully working example, compiled to JavaScript.


Use conditional templates with data binding to control the display of blocks of elements. Use a <template if="some_boolean_expression"> to conditionally render elements to a page. The conditional template is "live" and is re-evaluated when data changes.

You can see the source for this and more Dart Web Components samples on Github. Meanwhile, read more about Dart Web Components. Have fun!

(image courtesy of
Post a Comment

Popular posts from this blog

  • Sponsor:  Register today for  New Game, the conference for HTML5 game developers . Learn from Mozilla, Opera, Google, Spil, Bocoup, Mandreel, Subsonic, Gamesalad, EA, Zynga, and others at this intimate and technically rich conference. Join us for two days of content from developers building HTML5 games today. Nov 1-2, 2011 in San Francisco.  Register now ! This is the second article in a Box2D series, following the Box2D Orientation article. The Box2DWeb port of Box2D contains a nice example to show off the basics of integrating physics simulations into your web app. This post will provide a walkthrough of the example, explaining the high level concepts and code. First, let's see the example in action. The code for the above is open source and available on GitHub. It was adapted from Box2DWeb's example . Animating Before we look at Box2D, it's important to understand how the above simulation is animated. You might think setInterval or setTimeout is
  • In which I port a snazzy little JavaScript audio web app to Dart , discover a bug, and high-five type annotations. Here's what I learned. [As it says in the header of this blog, I'm a seasoned Dart developer. However, I certainly don't write Dart every day (I wish!). Don't interpret this post as "Hi, I'm new to Dart". Instead, interpret this post as "I'm applying what I've been documenting."] This post analyzes two versions of the same app, both the original (JavaScript) version and the Dart version. The original version is a proxy for any small JavaScript app, there's nothing particularly special about the original version, which is why it made for a good example. This post discusses the differences between the two implementations: file organization, dependencies and modules, shims, classes, type annotations, event handling, calling multiple methods, asynchronous programming, animation, and interop with JavaScript libraries. F
  • Angular and Polymer, sitting in a DOM tree, B-i-n-d-i-n-g. First comes components, Then comes elements, Then comes the interop with the node dot bind. Angular , a super heroic MVC framework, and Polymer , polyfills and enhancements for custom elements built on top of Web Components, can live harmoniously in the same app. This post shows you how to connect Angular-controlled components to Polymer-controlled elements via data binding. And we do it all in Dart . Angular and Polymer I get asked "Should I use Angular or Polymer?" a lot. My answer is, "Yes". That is, both libraries have distinct strengths, and you can use both in the same app. Polymer excels at creating encapsulated custom elements. You can use those custom elements in any web app or web page, regardless if that app is built with Angular, Ember, etc. Angular excels at application engineering, with dependency injection, end-to-end testability, routing, and services. Here are som