- Notifications
You must be signed in to change notification settings - Fork69
Light-weight, logic-less, DSL-free, templates for all javascript environments!
License
flatiron/plates
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Plates (short for templates) binds data to markup. Plates has NO special syntax. It works in the browser and inNode.js.
- DSLs (Domain Specific Languages) such as
<%=foo%>
or{{foo}}
reduce portability. - DOM templating is SLOW.
- 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.
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.
There are a few ways to useplates
. Install the library using npm. You can addit to yourpackage.json
file as a dependency, or include the script in yourHTML page.
By default,plates
will try to match the key in the data to anid
in thetag, since both should be unique.
varPlates=require('plates');varhtml='<div>Old Value</div>';vardata={"test":"New Value"};varoutput=Plates.bind(html,data);
A common use case is to apply the new value to each tag's body based on theclass
attribute.
varhtml='<span>User</span>...<span>User</span>';vardata={"username":"John Smith"};varmap=Plates.Map();map.class('name').to('username');console.log(Plates.bind(html,data,map));
Another common case is to replace the value of an attribute if it is a match.
varhtml='<a href="/"></a>';vardata={"newurl":"http://www.nodejitsu.com"};varmap=Plates.Map();map.where('href').is('/').insert('newurl');console.log(Plates.bind(html,data,map));
Partial value replacement
varhtml='<a href="/foo/bar"></a>';vardata={"newurl":"bazz"};varmap=Plates.Map();map.where('href').has(/bar/).insert('newurl');// `has` can take a regular expression.console.log(Plates.bind(html,data,map));
In even more complex cases, an arbitrary attribute can be specified. If a valueis matched, a specific value can be used and then used as another attribute'svalue.
varhtml='<img data-foo="bar" src=""></img>';vardata={"imageurl":"http://www.nodejitsu.com"};varmap=Plates.Map();map.where('data-foo').is('bar').use('imageurl').as('src');console.log(Plates.bind(html,data,map));
Plates can also iterate through collections:
varhtml='<div></div>';varcollection=[{'name':'Louis CK'},{'name':'Andy Kindler'},{'name':'Greg Giraldo'}];console.log(Plates.bind(html,collection));
Plates also supports partials:
varpartial='<li></li>';varbase='<div><h1></h1><ul></ul></div>';varbaseData={foo:'bar'};varmapping=Plates.Map();mapping.class('menu').append(partial);console.log(Plates.bind(base,baseData,mapping));
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()`.@return {String} The result of merging the data and html.
function Plates.Map(options)@options {Object} An object literal that contains configuration options. - @option where {String} The default attribute to match on instead of ID. - @option as {String} The default attribute to replace into.@return {Object} An object that represents a reusable map, has mapping methods.
function Map#where(attribute)@param attribute {String} An attribute that may be found in a tag.This method will initiate a clause. Once a clause has been established,other member methods may be chained to each other in any order.
function Map#class(attribute)@param attribute {String} A value that may be found in the `class` attribute of a tag.
function Map#is(value)@param value {String} The value of the attribute specified in the `where` clause.
function Map#has(value)@param value {String|RegExp} The value of the attribute specified in the `where` clause.
function Map#insert(attribute)@param attribute {String} A string that represents a key. Data will be inserted intothe attribute that was specified in the `where` clause.
function Map#use(key)@param key {String|Function} A string that represents a key in the data object that was provided or a function which returns a string value to use.If a function is provided, it will be passed data, value and tagbody parameters.
function Map#to(key)@param key {String|Function} A string that represents a key in the data object that was provided or a function which returns a string value to use.If a function is provided, it will be passed data, value and tagbody parameters.Same as `use` method.
function Map#as(attribute)@param attribute {String} A string that represents an attribute in the tag.If there is no attribute by that name found, one may be created depending on the optionsthat were passed to the `Map` constructor.
function Map#remove()Removes the matching elements from the template.
function Map#append(html, data, map)@param html {String} A string that represents the new template that needs to beadded.@param data {Mixed} data for the partial, if it's a string it's a reference to akey in the data structure that was supplied to the main template.@param map {Plates.Map} data mapping for the partial.If the supplied HTML string doesn't contain any HTML markup we assume that wethe given string is the location of the template. When you are using Plates onthe browser is assumes that you supplied it with an id selector and will fetchthe innerHTML from the element. If you are using Plates in Node.js it assumesthat you gave it a file path that is relative to the current working directory.
(The MIT License)
Copyright (c) 2011 Arnout Kazemier, Martijn Swaagman, & the Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy ofthis software and associated documentation files (the 'Software'), to deal inthe Software without restriction, including without limitation the rights touse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies ofthe Software, and to permit persons to whom the Software is furnished to do so,subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESSFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS ORCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHERIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
About
Light-weight, logic-less, DSL-free, templates for all javascript environments!