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:
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.
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.
Summary
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 http://www.flickr.com/photos/84987970@N00/4981201339/)
(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">
<h2>Thanks!</h2>
<p>We'll send you an activation key to your email.</p>
</template>
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>
<html>
<head>
<meta charset="utf-8">
<title>Simple Conditional</title>
<link rel="stylesheet" href="App.css">
</head>
<body>
<h1>Simple Conditional with Data Binding</h1>
<p>
<label>
<input type="checkbox" id="accept-license" bind-checked="acceptLicense">
I accept the license
</label>
</p>
<template if="acceptLicense">
<h2>Thanks!</h2>
<p>We'll send the activation key to your email.</p>
</template>
<script type="application/dart">
bool acceptLicense = false;
main() { }
</script>
<script src="https://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js"></script>
</body>
</html>
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.
Summary
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 http://www.flickr.com/photos/84987970@N00/4981201339/)