Skip to content
Browse files

[dist] examples

  • Loading branch information...
1 parent 1b4fdd5 commit 9ef11b7cacc44fb6951ed7ea84d6319279d84fb0 Paolo Fragomeni committed Jan 12, 2012
Showing with 309 additions and 81 deletions.
  1. +73 −73 README.md
  2. +0 −8 examples/basic.js
  3. +19 −0 examples/browser/index.js
  4. +217 −0 examples/browser/plates.js
View
146 README.md
@@ -1,27 +1,36 @@
-# Plates [![Build Status](https://secure.travis-ci.org/flatiron/plates.png)](http://travis-ci.org/flatiron/plates)
+
+![](http://flatironjs.org/img/flatiron.png)
+
+# Plates
+The anti-templating tool for every javascript environment.
## Synopsys
-Plates (short for templates) binds data to markup. There's NO special syntax. It works in the browser and in `Node.js`. The right way to do this is with a DOM. Unfortunately, at the moment, the DOM is slow. On the server, it is quite slow.
+Plates (short for templates) binds data to markup. Plates has NO special syntax. It works in the browser and in `Node.js`.
## Motivation
-- No NON-HTML in your HTML such as <%=foo%> or {{foo}}.
-- Promote portable code/markup by decoupling decision making from presentation.
-- Make both the code and markup more readable and maintainable.
-- Allow designers to write markup and test styling without impacting logic or special placeholders.
+- DOM templating is SLOW.
+- DSLs (Domain Specific Languages) such as <%=foo%> or {{foo}} reduce portability.
+- Promote the separation of concerns principle by decoupling decision making from presentation.
+- Make both the code and markup more readable and maintainable by a wider audience.
-## Usage
+## Status
-### On the Server
+[![Build Status](https://secure.travis-ci.org/flatiron/plates.png)](http://travis-ci.org/flatiron/plates)
-Install the library using npm or add it to your `package.json` file as a dependancy.
+## Features
-```bash
- npm install plates
-```
+- Automatically bind data to a tag's body by matching unique tag IDs to data keys.
+- Bind data to a tag's body based on any attribute's values.
+- Bind data to a tag's attribute based on any attribute's values.
+
+- TODO: Specify option to create attribute if it does not exist.
-Take some markup, some data, bind them, done.
+## Installation
+There are a few ways to use `plates`. Install the library using npm. You can add it to your `package.json` file as a dependancy, or include the script in your HTML page.
+
+## Usage
```js
@@ -32,92 +41,83 @@ Take some markup, some data, bind them, done.
var output = Plates.bind(html, data);
- //
- // with the output, you could serve it up or process it further with JSDOM
- //
-
- ...
- response.end(output);
- ...
-
-```
-
-### On the client
-
-Include the script somehow wherever you are going to use it.
-
-```html
- <script type="text/javascript" src="plates.js"></script>
```
-Here's a contrived example using jQuery.
+### Matching a data key to a class
+Consider the case where you want to apply a values to each tag that has a class.
-```html
+```js
+ var html = '<span class="name">User</span>...<span class="name">User</span>';
-<html>
- <head>
- <script type="text/javascript">
+ var data = { "username": "John Smith" };
+ var map = Plates.Map();
- var html = $('#template1')[0];
- var data = { "template1": "New Value" };
+ map.where('class').is('name').use('username');
- var output = Plates.bind(html, data);
+ console.log(Plates.bind(html, data, map));
+```
- $('#template1').html(output);
- $('#ui')
+### Matching a data key to a class and inserting the new value into an attribute
- </script>
- <style>
- .templates { display: none; }
- </style>
- <body>
+```js
+ var html = '<a href="/"></a>';
- <div class="templates">
- <div id="template1">Old Value</div>
- </div>
+ var data = { "newurl": "http://www.nodejitsu.com" };
+ var map = Plates.Map();
- <div class="ui">
- </div>
- </body>
-</html>
+ map.where('href').is('/').use('newurl').as('href');
+ console.log(Plates.bind(html, data, map));
```
-### Defining explicit instructions for matching data keys with html tags.
-
-Plates will attempt to match the data key to the `id` of the element. If you want to be specific about the relationship between
+## API
-#### An example of matching a data key to a class
+### Static Methods
-```js
-
- var html = '111<div id="outer">222<div id="inner">333</div><img class="test" src=""/>444</div>555';
-
- var data = { "foo": "New Value" };
- var map = Plates.Map();
+```
+ function Plates.bind(html, data, map)
+ @param html {String} A string of well formed HTML.
+ @param data {Object} A JSON object.
+ @param map {Object} an instance of `Plates.Map()`.
- map.where('class').is('test').use('foo');
+ @return {String} the result of merging the data and html.
+```
- console.log(Plates.bind(html, data, map));
+### Constructors
```
+ function Plates.Map(options)
+ @options {Object} an object literal that contains configuration options.
+ @return {Object} an object that represents a reusable map, has mapping methods.
+```
-#### An example of putting the new value into the attribute rather than the tag body.
-
-```js
+### Member Methods
- var html = '111<div id="outer">222<div id="inner">333</div><img class="test" src=""/>444</div>555';
+```
+ function Map#where(attribute)
+ @param attribute {String} an attribute that may be found in a tag
- var data = { "foo": "New Value" };
- var map = Plates.Map();
+ This method will initiate a `clause`. Once a clause has been established,
+ other member methods may be chained to eachother in any order.
+```
- map.where('class').is('test').use('foo').as('src');
+```
+ function Map#is(value)
+ @param value {String} the value of the attribute specified in the `where` clause.
+```
- console.log(Plates.bind(html, data, map));
+```
+ function Map#use(key)
+ @param key {String} a string that represents a key in the data object that was provided.
+```
```
+ function Map#as(attribute)
+ @param attribute {String} a string that represents an attribute in the tag.
-The options object contains a mapping of option-key to data-key. In the above example, the option-key has an array value. The array can contain two values, the attribute name (1) and optionally the attribute name (2) to populate with the new value.
+ If there is no attribute by that name found, one may be created depending on the options
+ that were passed to the Map constructor.
+```
## License
@@ -129,4 +129,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. FUCK YEAH.
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
8 examples/basic.js
@@ -1,8 +0,0 @@
-var Plates = require('../lib/plates');
-
-var html = '<div id="test">Old Value</div>';
-var data = { "test": "New Value" };
-
-var output = Plates.bind(html, data);
-
-console.log(output);
View
19 examples/browser/index.js
@@ -0,0 +1,19 @@
+
+$(function() {
+
+ var html = $('#template1').html();
+ var data = {
+ "address": "http://github.com/hij1nx",
+ "name": "Github"
+ };
+
+ var map = new Plates.Map();
+ map.where('class').is('link').use('address').as('href');
+ map.where('class').is('link').use('name');
+
+ var output = Plates.bind(html, data, map);
+
+ $('#ui').html(output);
+ $('#ui').html();
+
+});
View
217 examples/browser/plates.js
@@ -0,0 +1,217 @@
+
+;var Plates = function Plates(undefined) {
+
+ var Merge = function Merge() {};
+
+ Merge.prototype = {
+
+ tag: new RegExp([
+ '<',
+ '(/?)', // 2 - is closing
+ '([-:\\w]+)', // 3 - name
+ '((?:[-\\w]+(?:', '=',
+ '(?:\\w+|["|\'](?:.*)["|\']))?)*)', // 4 - attributes
+ '(/?)', // 5 - is self-closing
+ '>'
+ ].join('\\s*')),
+
+ attr: /([\-\w]*)=(?:["\']([\-\.\w\s\/]*)["\'])/gi,
+
+ hasClass: function(str, className) {
+ return str.indexOf(className) > -1;
+ },
+
+ bind: function bind(html, data, map) {
+
+ html = html || '';
+ data = data || {};
+
+ var that = this;
+
+ if (map) {
+ map = map.mappings;
+ }
+
+ var openers = 0,
+ components,
+ attributes,
+ intag = false,
+ tagname = '',
+ isClosing = false,
+ isSelfClosing = false
+ matchmode = false;
+
+ var c,
+ buffer = '',
+ left;
+
+ for (var i = 0, l = html.length; i < l; i++) {
+
+ c = html[i];
+
+ if (c === '!' && intag && !matchmode) {
+ intag = false;
+ buffer += html.slice(left, i+1);
+ }
+ else if (c === '<' && !intag) {
+ closing = true;
+ intag = true;
+ left = i;
+ }
+ else if (c === '>' && intag) {
+
+ intag = false;
+ tagbody = html.slice(left, i+1);
+
+ components = this.tag.exec(tagbody);
+
+ if(!components) { continue; }
+
+ isClosing = components[1];
+ tagname = components[2];
+ attributes = components[3];
+ isSelfClosing = components[4];
+
+ if (matchmode) {
+
+ //
+ // and its a closing.
+ //
+ if (!!isClosing) {
+
+ if (openers <= 0) {
+ matchmode = false;
+ }
+ else {
+ --openers;
+ }
+ }
+ //
+ // and its not a self-closing tag
+ //
+ else if (!isSelfClosing) {
+ ++openers;
+ }
+ }
+
+ if (attributes && !isClosing && !matchmode) {
+
+ //
+ // if there is a match in progress and
+ //
+ if (map) {
+
+ for (var ii = map.length - 1; ii >= 0; ii--) {
+
+ tagbody = tagbody.replace(this.attr, function(str, key, value, a) {
+
+ var newdata = map[ii].dataKey ? data[map[ii].dataKey] : data[key];
+
+ if (map[ii].replace === key) {
+
+ //
+ // if there is data intended to replace the attribute, use that
+ // otherwise look at the data structure and try to use that.
+ //
+ var newdata = map[ii].dataKey ? data[map[ii].dataKey] : data[key];
+ return key + '="' + (newdata || '') + '"';
+ }
+ else if (!map[ii].replace && map[ii].attribute === key) {
+
+ if (map[ii].value === value || that.hasClass(value, map[ii].value)) {
+
+ buffer += tagbody + (newdata || '');
+ matchmode = true;
+ }
+ }
+
+ return str;
+
+ });
+ }
+ }
+ else {
+
+ //
+ // if there is no map, we are just looking to match
+ // the specified id to a data key in the data object.
+ //
+ tagbody.replace(
+ this.attr,
+ function (attr, key, value, idx) {
+ if (key === 'id' && data[value]) {
+ buffer += tagbody + data[value];
+ matchmode = true;
+ }
+ }
+ );
+ }
+ }
+
+ //
+ // if there is currently no match in progress
+ // just write the tagbody to the buffer.
+ //
+ if (!matchmode) {
+ buffer += tagbody;
+ }
+
+ }
+ else if (!intag && !matchmode) {
+
+ //
+ // currently not inside a tag and there is no
+ // match in progress, we can write the char to
+ // the buffer.
+ //
+ buffer += c;
+ }
+
+ }
+ return buffer;
+ }
+
+ };
+ var Mapper = function Mapper(val) {
+ if (!(this instanceof Mapper)) { return new Mapper(val); }
+ this.mappings = [];
+ };
+
+ function last(newitem) {
+ if (newitem) {
+ this.mappings.push({});
+ }
+ return this.mappings[this.mappings.length-1];
+ }
+
+ Mapper.prototype = {
+ use: function(val) {
+ last.call(this).dataKey = val;
+ return this;
+ },
+ where: function(val) {
+ last.call(this, true).attribute = val;
+ return this;
+ },
+ is: function(val) {
+ last.call(this).value = val;
+ return this;
+ },
+ as: function(val) {
+ last.call(this).replace = val;
+ return this;
+ }
+ };
+
+ var host = (typeof process !== 'undefined' && process.title === 'node' ? exports : Plates);
+
+ host.bind = function (html, data, map) {
+ var merge = new Merge();
+ return merge.bind(html, data, map);
+ };
+
+ host.Map = Mapper;
+
+ return host;
+
+}();

0 comments on commit 9ef11b7

Please sign in to comment.
Something went wrong with that request. Please try again.