Created
May 24, 2011 17:16
-
-
Save maxpert/989159 to your computer and use it in GitHub Desktop.
Femto JS Templating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= Femto Javascript Template just 325 bytes = | |
It can't be any simpler... sized under 325 bytes, it supports looping and direct DOM insertion... Hope you enjoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
The MIT License | |
Copyright (c) 2010 Zohaib Sibt-e-Hassan | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the 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 | |
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. | |
**/ | |
//No library dependencies just 325 bytes!!! fitb => fill in the blanks | |
function fitb(b,c,d){var e=/(:\w+)/g,iht=b.innerHTML,tpl=((b._$$f=b._$$f||iht))||b;d=d||{};return(iht&&(b.innerHTML=((d.append&&b.innerHTML)||"")+(d.pre||"")+fitb(tpl,c,d)+(d.post||"")))||(c.length&&c.map(function(a){return fitb(tpl,a,d)}).join(d.glue||""))||(tpl.replace(e,function(a){return(c[a.substr(1)]||d.none||"")}))}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="area1"> </div> | |
<hr/> | |
<div id="area2"> </div> | |
<hr/> | |
<div id="area3"> This DOM element was :what </div> | |
<hr/> | |
<ul id="list">:v</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Test sutie code | |
window.onload = function() { | |
// Simple string fill | |
var res = fitb('hello :name this was a simple :type', | |
{'name': 'WORLD', 'type': 'string'}); | |
// Simple string fill with automatic looping | |
res = fitb('hello :name this was a simple :type', | |
//Yep passing a simple array results in looping | |
[{'name': 'Again', 'type': 'array'}, {'name': 'Looping', 'type': 'array'}], | |
{'glue': '<br/>'}); | |
document.getElementById('area2').innerHTML=res; | |
// Direct manipulation on DOM elements | |
fitb(document.getElementById('area3'), {'what': 'manipulated'}); | |
// Additional options :D | |
fitb(document.getElementById('list') , | |
[{'v': 'Some dummy element'},{'v': 'Another one'}], | |
{'pre': '<li>', 'post': '</li>','glue': '</li><li>'}); | |
fitb(document.getElementById('list') , | |
[{'v': 'Append mode is also there'},{'v': 'Yep templates are cached too'}], | |
{'pre': '<li>', 'post': '</li>', 'glue': '</li><li>', 'append': true}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment