Skip to content

Add Function Support #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# t.js
## A tiny javascript templating framework in ~400 bytes gzipped

`t.js` is a simple solution to interpolating values in an html string for insertion into the DOM via `innerHTML`.

### Features
* Simple interpolation: `{{=value}}`
* Scrubbed interpolation: `{{%unsafe_value}}`
* Name-spaced variables: `{{=User.address.city}}`
* If/else blocks: `{{value}} <<markup>> {{:value}} <<alternate markup>> {{/value}}`
* If not blocks: `{{!value}} <<markup>> {{/!value}}`
* Object/Array iteration: `{{@object_value}} {{=_key}}:{{=_val}} {{/@object_value}}`
* Multi-line templates (no removal of newlines required to render)
* Render the same template multiple times with different data
* Works in all modern browsers

### How to use

var template = new t("<div>Hello {{=name}}</div>");
document.body.innerHtml = template.render({name: "World!"});

For more advanced usage check the [`t_test.html`](https://github.com/jasonmoo/t.js/blob/master/t_test.html).

This software is released under the MIT license.

___

# t.js
## A tiny javascript templating framework in ~400 bytes gzipped

`t.js` is a simple solution to interpolating values in an html string for insertion into the DOM via `innerHTML`.

### Features
* Simple interpolation: `{{=value}}`
* Scrubbed interpolation: `{{%unsafe_value}}`
* Name-spaced variables: `{{=User.address.city}}`
* If/else blocks: `{{value}} <<markup>> {{:value}} <<alternate markup>> {{/value}}`
* If not blocks: `{{!value}} <<markup>> {{/!value}}`
* Object/Array iteration: `{{@object_value}} {{=_key}}:{{=_val}} {{/@object_value}}`
* Function Support: `{{#value|func,func2}}`
* Multi-line templates (no removal of newlines required to render)
* Render the same template multiple times with different data
* Works in all modern browsers

### How to use

var template = new t("<div>Hello {{=name}}</div>");
document.body.innerHtml = template.render({name: "World!"});

### How Function works

var template = new t("<div>{{#name|hello,world}}</div>");
template.register("hello",function(data){return data+" Hello";})
template.register("world",function(data){return data+" world!";})
document.body.innerHtml = template.render({name: "Icy!"});

//return Icy! Hello world!

For more advanced usage check the [`t_test.html`](https://github.com/jasonmoo/t.js/blob/master/t_test.html).

This software is released under the MIT license.

___

[Coffeescript version](https://github.com/davidrekow/t.coffee) maintained by [@davidrekow](https://github.com/davidrekow)
230 changes: 131 additions & 99 deletions t.js
Original file line number Diff line number Diff line change
@@ -1,99 +1,131 @@
/*
_ _
| | (_)
| |_ _ ___
| __| | / __|
| |_ _| \__ \
\__(_) |___/
_/ |
|__/

t.js
a micro-templating framework in ~400 bytes gzipped

@author Jason Mooberry <[email protected]>
@license MIT
@version 0.1.0

*/
(function() {

var blockregex = /\{\{(([@!]?)(.+?))\}\}(([\s\S]+?)(\{\{:\1\}\}([\s\S]+?))?)\{\{\/\1\}\}/g,
valregex = /\{\{([=%])(.+?)\}\}/g;

function t(template) {
this.t = template;
}

function scrub(val) {
return new Option(val).innerHTML.replace(/"/g,"&quot;");
}

function get_value(vars, key) {
var parts = key.split('.');
while (parts.length) {
if (!(parts[0] in vars)) {
return false;
}
vars = vars[parts.shift()];
}
return vars;
}

function render(fragment, vars) {
return fragment
.replace(blockregex, function(_, __, meta, key, inner, if_true, has_else, if_false) {

var val = get_value(vars,key), temp = "", i;

if (!val) {

// handle if not
if (meta == '!') {
return render(inner, vars);
}
// check for else
if (has_else) {
return render(if_false, vars);
}

return "";
}

// regular if
if (!meta) {
return render(if_true, vars);
}

// process array/obj iteration
if (meta == '@') {
for (i in val) {
if (val.hasOwnProperty(i)) {
vars._key = i;
vars._val = val[i];
temp += render(inner, vars);
}
}
delete vars._key;
delete vars._val;
return temp;
}

})
.replace(valregex, function(_, meta, key) {
var val = get_value(vars,key);

if (val || val === 0) {
return meta == '%' ? scrub(val) : val;
}
return "";
});
}

t.prototype.render = function (vars) {
return render(this.t, vars);
};

window.t = t;

})();
/*
_ _
| | (_)
| |_ _ ___
| __| | / __|
| |_ _| \__ \
\__(_) |___/
_/ |
|__/

t.js
a micro-templating framework in ~400 bytes gzipped

@author Jason Mooberry <[email protected]>
@license MIT
@version 0.1.0

*/
(function() {

var blockregex = /\{\{(([@!]?)(.+?))\}\}(([\s\S]+?)(\{\{:\1\}\}([\s\S]+?))?)\{\{\/\1\}\}/g,
valregex = /\{\{([=%#])(.+?)\}\}/g;

function t(template) {
this.t = template;
}

function scrub(val) {
return new Option(val).innerHTML.replace(/"/g,"&quot;");
}

function get_value(vars, key) {
var parts = key.split('.');
while (parts.length) {
if (!(parts[0] in vars)) {
return false;
}
vars = vars[parts.shift()];
}
return vars;
}

function render(fragment, vars) {
return fragment
.replace(blockregex, function(_, __, meta, key, inner, if_true, has_else, if_false) {

var val = get_value(vars,key), temp = "", i;

if (!val) {

// handle if not
if (meta == '!') {
return render(inner, vars);
}
// check for else
if (has_else) {
return render(if_false, vars);
}

return "";
}

// regular if
if (!meta) {
return render(if_true, vars);
}

// process array/obj iteration
if (meta == '@') {
for (i in val) {
if (val.hasOwnProperty(i)) {
vars._key = i;
vars._val = val[i];
temp += render(inner, vars);
}
}
delete vars._key;
delete vars._val;
return temp;
}

})
.replace(valregex, function(_, meta, key) {
if (meta=='#') {
var fn=key.split('|');
key=fn[0];
var val=get_value(vars,key);
if (fn.length>1){
var methods=fn[1].split(',');
var j=methods.length;
var i=0;
while(i<j){
if (t.hasOwnProperty(methods[i])) {
val=t[methods[i++]](val);

};
};
}
return val;
};

var val = get_value(vars,key);

if (val || val === 0) {
return meta == '%' ? scrub(val) : val;
}
return "";
});
}


t.prototype.render = function (vars) {
return render(this.t, vars);
};

t.prototype.register = function(fname, fn) {
if(t.hasOwnProperty(fname)) {
return false;
}
return t[fname]=fn;
};

t.prototype.unregister = function(fname) {
if(t.hasOwnProperty(fname)) {
return delete t[fname];
}
};

window.t = t;

})();
4 changes: 2 additions & 2 deletions t.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading