atmin / jtmpl Goto Github PK
View Code? Open in Web Editor NEWMustache syntax + automatic data-binding = Live templates ☆
Home Page: jtmpl.com
License: MIT License
Mustache syntax + automatic data-binding = Live templates ☆
Home Page: jtmpl.com
License: MIT License
Blocks and template inheritance akin to Django template inheritance
<script id="base" type="text/html">
{{+primary_block}}
Block contents is template code, of course
{{/primary_block}}
{{+secondary_block}}
Some secondary content
{{/secondary_block}}
</script>
<script id="descendant" type="text/html">
{{<"#base"}}
{{+primary_block}}
Will override #base's primary_block content
{{/primary_block}}
{{+secondary_block}}
If this block was non-existent, you would see "Some secondary content" from #base
{{/secondary_block}}
</script>
Syntax inspired by Dust
jtmpl('GET', 'url', function (response) { ... } )
When content-type: application/json, response
is parsed object, otherwise string
jtmpl('POST', 'url' [, params][, function (error, response) { ... }])
var model = {
'#route1': function () { ... },
'#another-route': function () { ... }
}
Template:
<a href="#route1">Trigger model['#route1']</a>
<a href="#another-route">Trigger model['#another-route']</a>
Format variable output
{{name|capitalize}}
hey i think this project looks really cool. found it from your comment here: https://moot.it/blog/technology/frameworkless-javascript.html
i think its interesting that you chose not to have jquery as a dependency, since its already pretty ubiquitous. maybe this is out of the scope of the project but how would you recommend syncing model with a server without having to include jquery?
it would be a shame to have to include jquery to do this, and if you assume people would need it for other functionality, you might as well make it a dependency if it makes coding simpler.
Make jtmpl a processor of lists of rules:
compile rules, like:
{
pattern: /regexp with groups/,
replace: function (groups, model) { ... },
push: boolean or undefined,
pop: boolean or undefined
}
bind rules, TBD
Wanted effect: DRY code, pay technical debt, plugin system:
jtmpl.compileRules.push({ pattern: ..., replace: .... });
jtmpl.bindRules.push({ ... });
jtmpl.filters.push({ ... });
jtmpl. ... .push({ ... });
Can be applied on arrays and event streams.
{{#sequence|mappingFunction}}
onevent="{{eventHandler|eventFilter}}"
// This function lives in `jtmpl.mappings` or in `model` where `sequence` is defined
// jtmpl.mappings.mappingFunction = function (el, i) ...
// or
model.sequence = [...];
model.mappingFunction = function (el, i) {
if (el.conditionSatisfied) {
return mappedElement;
}
// `return` or `return null` means this element should be filtered
}
model.eventFilter = function (event) {
return someCondition ? event : null; // or any falsy value
}
var model = {
firstName: 'Random',
lastName: 'Number',
fullName: function () {
return this('firstName') + ' ' + this('lastName');
}
}
jtmpl('{{fullName}}', model)
// Random Number
fullName
will be called whenever any of the dependent fields changes. A function, that takes care of dependency tracking and returns property value is passed via the this
context.
var model = {
firstName: 'Random',
lastName: 'Number',
fullName: function (newValue) {
// Setter? (idiomatic comparison, getters always receive a callback argument)
if (typeof newValue !== 'function') {
var names = newValue.split(' ');
this('firstName', names[0]);
this('lastName', names[1]);
}
// Getter
return this('firstName') + ' ' + this('lastName');
}
}
jtmpl('#target', '<input value="{{fullName}}">', model);
// If you input "Random Number" into the field, then:
// model.firstName === 'Random' and model.lastName === 'Number'
var model = {
id: 1,
contents: function (callback) {
jtmpl('GET', 'http://example/RESTservice/' + this('id'), callback);
// if the function returns undefined, jtmpl expects callback to be called
// with the result value
}
jtmpl('#target', '{{contents}}', model);
// a service will return contents for the respective model.id
model.id = 2
// "http://example/RESTservice/2" will be fetched
var model = {
id: 1,
person: function (callback) {
// API should return 'application/json' response, so it's parsed
// Whenever model.id is changed, person context is updated
jtmpl('GET', '/api/person/' + this('id'), callback);
}
}
Template:
{{#person}}
{{firstName}} {{lastName}} is {{age}} years old
{{/person}}
you can edit a few items in parallel. Try to double click an item and then do the same to another. he correct behavior would be for the 1st item to stop being editted
section
inside <table>
has bug: model's section
changes, the <table>
doesn't change.
See it in JSFiddle: http://jsfiddle.net/SJ4J4/
<table id="tableNotUpdate">
<tbody>
<!-- {{#section}} -->
<tr>
<td>{{name}}</td>
<td>{{score}}</td>
</tr>
<!-- {{/section}} -->
</tbody>
</table>
var model = {
section: [
{ name: 'Tom', score: 90 },
{ name: 'John', score: 84 }
]
};
jtmpl('#tableNotUpdate', '#tableNotUpdate', model);
Run model.section.push({ name: 'Mary', score: 97 })
in console.
Throw Uncaught TypeError: Cannot read property 'innerHTML' of undefined
Partials (template include)
{{>var_template_id_or_url}}
{{>"#template-id"}}
{{>"//xhr-fetch-template.url"}}
"http:" or "https:" part can be omitted, so it'll inherit current scheme
Included templates inherit their parent context.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.