Transparency is a client-side template engine which binds data to DOM. Just call .render(data).
<divid="template"><spanclass="greeting"></span><spandata-bind="name"></span></div>varhello={greeting: 'Hello',name: 'world!'};$('#template').render(hello);<divid="template"><spanclass="greeting">Hello</span><spandata-bind="name">world!</span></div>- Semantic data binding - No need for
<%=foo%>or{{foo}}assignments - Collection rendering - No need for hand-written loops
- Valid HTML templates - Write templates as a part of the HTML, in plain HTML
- View logic in JavaScript - No crippled micro-template language, just plain JavaScript functions
Transparency is compatible with IE9+, Chrome, Firefox, iOS, Android and other mobile browsers. Support for IE8 requires jQuery.
- IRC: freenode/#transparency.js
- Google Groups: transparencyjs@googlegroups.com
We are looking for new maintainers. Anyone with an accepted pull request will get commit rights. Current maintainers are
Try Transparency with interactive examples.
curl https://raw.github.com/leonidas/transparency/master/dist/transparency.min.js
# Or with Bower
npm install -g bower
bower install transparency
<scriptsrc="js/jquery-1.7.1.min.js"></script><scriptsrc="js/transparency.min.js"></script>require(['jquery','transparency'],function($,Transparency){// Without jQueryTransparency.render(document.getElementById('template'),data);// With jQueryjQuery.fn.render=Transparency.jQueryPlugin;$('#template').render(data);});npm install transparency
For server-side use, see examples/hello-server.
Here are short and detailed examples how to use Transparency. For more elaborate examples, see User manual and FAQ. Feel free to add your own examples in the wiki, too!
Implementation details are explained in the annotated source code.
Transparency binds values in-place. That means, you don't need to write separate template sections on your page. Instead, compose just a normal, static html page and start binding some dynamic values on it!
By default, Transparency binds JavaScript objects to a DOM element by id, class,name attribute and
data-bind attribute. Default behavior can be changed by providing a custom matcher function, as explained in section
Configuration. Values are escaped before rendering.
Template:
<divid="container"><divid="hello"></div><divclass="goodbye"></div><inputtype="text" name="greeting" /><buttonclass="hi-button" data-bind="hi-label"></button></div>Javascript:
varhello={hello: 'Hello',goodbye: '<i>Goodbye!</i>',greeting: 'Howdy!','hi-label': 'Terve!'// Finnish i18n};// with jQuery$('#container').render(hello);// ..or withoutTransparency.render(document.getElementById('container'),hello);Result:
<divclass="container"><divid="hello">Hello</div><divclass="goodbye">lt;i>Goodbye!</i></div><inputtype="text" name="greeting" value="Howdy!" /><buttonclass="hi-button" data-bind="hi-label">Terve!</button></div>Template:
<ulid="activities"><liclass="activity"></li></ul>Javascript:
varactivities=[{activity: 'Jogging'},{activity: 'Gym'},{activity: 'Sky Diving'},];$('#activities').render(activities);// orTransparency.render(document.getElementById('activities'),activities);Result:
<ulid="activities"><liclass="activity">Jogging</li><liclass="activity">Gym</li><liclass="activity">Sky Diving</li></ul>With plain values, Transparency can't guess how you would like to bind the data to DOM, so a bit of help is needed. Directives are just for that.
Access to the plain values within the directives is provided through this.value. There's a whole
lot more to say about the directives, but that's all we need for now. For the details, see
section Directives.
Template:
<div><divclass="comments"><label>Comments:</label><spanclass="comment"></span></div></div>Javascript:
varcomments,directives;comments=["That rules","Great post!"];// See section 'Directives' for the detailsdirectives={comment: {text: function(){returnthis.value;}}};$('.comments').render(comments,directives);Result:
<div><divclass="comments"><label>Comments</label><spanclass="comment">That rules</span><label>Comments</label><spanclass="comment">Great post!</span></div></div>Template:
<divclass="container"><h1class="title"></h1><pclass="post"></p><divclass="comments"><divclass="comment"><spanclass="name"></span><spanclass="text"></span></div></div></div>Javascript:
varpost={title: 'Hello World',post: 'Hi there it is me',comments: [{name: 'John',text: 'That rules'},{name: 'Arnold',text: 'Great post!'}]};$('.container').render(post);Result:
<divclass="container"><h1class="title">Hello World</h1><pclass="post">Hi there it is me</p><divclass="comments"><divclass="comment"><spanclass="name">John</span><spanclass="text">That rules</span></div><divclass="comment"><spanclass="name">Arnold</span><spanclass="text">Great post!</span></div></div></div>Template:
<divclass="person"><divclass="firstname"></div><divclass="lastname"></div><divclass="address"><divclass="street"></div><divclass="zip"><spanclass="city"></span></div></div></div>Javascript:
varperson={firstname: 'John',lastname: 'Wayne',address: {street: '4th Street',city: 'San Francisco',zip: '94199'}};$('.person').render(person);Result:
<divclass="container"><divclass="firstname">John</div><divclass="lastname">Wayne</div><divclass="address"><divclass="street">4th Street</div><divclass="zip">94199<spanclass="city">San Francisco</span></div></div></div>Directives are actions Transparency performs while rendering the templates. They can be used for setting element
text or html content and attribute values, e.g., class, src or href.
Directives are plain javascript functions defined in a two-dimensional object literal, i.e.,
directives[element][attribute] = function(params) {...}
where element is value of id, class, name attribute or data-bind attribute of the target element. Similarly,
attribute is the name of the target attribute.
When a directive function is executed, this is bound to the current model object. In addition, the directive function
receives current element as params.element, current index as params.index and current value as params.value.
The return value of a directive function is assigned to the matching element attribute. The return value should be string, number or date.
Template:
<divclass="person"><spanclass="name">My name is </span><aclass="email"></a></div>Javascript:
varperson,directives;person={firstname: 'Jasmine',lastname: 'Taylor',email: 'jasmine.tailor@example.com'};directives={name: {text: function(params){returnparams.value+this.firstname+" "+this.lastname;}},email: {href: function(params){return"mailto:"+this.email;}}};$('.person').render(person,directives);Result:
<divclass="person"><spanclass="name">My name is Jasmine Taylor</span><aclass="email" href="mailto:jasmine.tailor@example.com">jasmine.tailor@example.com</a></div>Template:
<divclass="person"><spanclass="name"></span><spanclass="email"></span><divclass="friends"><divclass="friend"><spanclass="name"></span><spanclass="email"></span></div></div></div>Javascript:
person={firstname: 'Jasmine',lastname: 'Taylor',email: 'jasmine.taylor@example.com',friends: [{firstname: 'John',lastname: 'Mayer',email: 'john.mayer@example.com'},{firstname: 'Damien',lastname: 'Rice',email: 'damien.rice@example.com'}]};nameDecorator=function(){"<b>"+this.firstname+" "+this.lastname+"</b>";};directives={name: {html: nameDecorator},friends: {name: {html: nameDecorator}}};$('.person').render(person,directives);Result:
<divclass="person"><spanclass="name"><b>Jasmine Taylor</b></span><spanclass="email">jasmine.taylor@example.com</span><divclass="friends"><divclass="friend"><spanclass="name"><b>John Mayer</b></span><spanclass="email">john.mayer@example.com</span></div><divclass="friend"><spanclass="name"><b>Damien Rice</b></span><spanclass="email">damien.rice@example.com</span></div></div></div>Transparency can be configured to use custom matcher for binding the values to DOM elements.
For example, one might want to bind only with data-bind attribute, but not with class or id attributes.
Custom matcher function should take key and element as parameters and return true if the
corresponding value should be bind to the given DOM element.
// The custom matcher gets Transparency `Element` wrapper with following properties// element.el: Raw DOM element// element.name: Lower-cased name of the DOM element, e.g., 'input'// element.classNames: List of class names of the DOM element, e.g., ['person', 'selected']Transparency.matcher=function(element,key){returnelement.el.getAttribute('data-bind')==key;};// Values are bind only with 'data-bind' attributetemplate.render({name: "Will Smith"});http://leonidas.github.com/transparency/ is great place to fiddle around with your data and templates.
To enable debug mode, call .render with a {debug: true} config and open the javascript console.
$('container').render(data,{},{debug: true});Install node.js 0.8.x and npm. Then, in the project folder
$ npm install grunt -g # command-line build tool to enable TDD, auto-complation and minification
$ npm install # Install the development dependencies
$ grunt # Compile, run tests, minify and start watching for modifications
The annotated source code should give a decent introduction.
All the following are appreciated, in an asceding order of preference
- A feature request or a bug report
- Pull request with a failing unit test
- Pull request with unit tests and corresponding implementation
In case the contribution is changing Transparency API, please create a ticket first in order to discuss and agree on design.
