-
Notifications
You must be signed in to change notification settings - Fork 0
0.3.0 Templates
var T = require('oswst');
- .compile(filebody: string, filepath: string)
- .include(id: string)
- .render(data: TData, callback: TCallback, context?: TContext)
- .renderContext(string: string, context: Object, callback: TCallback)
- .renderAttributes(attributes: TAttributes, callback: TCallback, context?: TContext)
- [.renderSelector(attributes: TAttributes, selector: TSelector)](0.3.0-Templates#renderSelector)
- .sync(argument: Function)
- .isSyncFunction(argument: Function)
- .async(argument: Function)
- .isAsyncFunction(argument: Function)
Available only on a server!
(filebody: string, filepath: string) => Module.exports;
String as a separate module.
Do not cache the result.
T.compile('module.exports = __dirname;', 'script.js');
// => "script.js"
(id: string) => Module.exports;
As require, but do not cache the result.
// module.js
module.exports = 1;
// index.js
T.include('module.js');
// => 1
(data: TData, callback: TCallback, context?: TContext) => void;
Universal renderer.
Ignores native data JavaScript. Processes only the data of module.
Asynchronous only.
Not for manual use! For internal use only!
// syntax example
T.render(123, function(error, result) {
error // null
result // 123
}, { a: 1 });
// native data
T.render(123, console.log); // null, 123
T.render('string', console.log); // null, "string"
T.render(function() {}, console.log); // null, function() {}
T.render([1, 2, 3], console.log); // null, [1, 2, 3]
T.render({ a: 1, b: 2, c: 3}, console.log); // null, { a: 1, b: 2, c: 3 }
// data of module
T.render(T.sync(function() { return 123; }), console.log); // null, 123
T.render(T.async(function(callback) { callback(null, 123); }), console.log); // null, 123
T.render(T.Renderer().data(123)), console.log); // null, 123
T.render([
T.sync(function() { return 1; }),
T.async(function() { callback(null, 2); }),
T.Renderer().data(3)
], console.log); // null, [1, 2, '3']
T.render({
a: T.sync(function() { return 1; }),
b: T.async(function() { callback(null, 2); }),
c: T.Renderer().data(3)
}); // null, { a: 1, b: 2, c: '3' }
(string: string, context: Object, callback: TCallback) => void;
By default - wrap around method _.template.
You can override this method in its sole discretion.
Asynchronous only.
Not for manual use! For internal use only!
T.renderContext('<%= a %>', { a: 1 }, function(error, result) {
error // null
result // "1"
});
(attributes: TAttributes, callback: TCallback, context?: TContext) => void;
Hash object representing attributes turns into a string syntax XML/XHML/HTML.
T.renderAttributes({
a: 'string',
b: null,
c: T.sync(function() { return 'sync' }),
d: T.async(function(callback) { callback(null, 'async'); }),
e: T.Renderer().data('<%= e %>')
}, function(error, result) {
error // null
result // ' a="string" b c="sync" d="async" e="data"'
}, { e: 'data' });
(attributes: TAttributes, selector: TSelector) => void;
Parser for userful TSelector.
Used regular expressions:
(/(\[)|(\])|#([-\w\d]+)|\.([-\w\d]+)|([\w\d-]+)="(['\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]*)"|([\w\d-]+)='(["\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]*)'|([\w\d-]+)=([\w\d-:\\\/\.={}<>%@#$%^&*~`]*)|("['\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]+")|('["\w\d\s-:\\\/\.\,\]\[={}<>%@#$%^&*~`]+')|([_\w-:\\\/]+)/g)
Tested here: (https://www.regex101.com/r/cM5jC6/13)[https://www.regex101.com/r/cM5jC6/13]
With selector:
.class-name.withBig.letters.AndFromIt#Id1#Id2[attr1=http://link.without/quotes.png,attr2='http://link.with/single/quotes'][attr3="http://link.with/double/quotes",attr4'attr5' "attr6"][alt=#simple!]
Result TAttributes:
var attributes = {};
T.renderSelector = function(attributes, selector) {
class: 'class-name withBig letters AndFromIt',
id: 'Id2',
attr1: 'http://link.without/quotes.png',
attr2: 'http://link.with/single/quotes',
attr3: "http://link.with/double/quotes",
attr4: null,
"'attr5'": null,
'"attr6"': null,
alt: "#simple"
};
(argument: () => any) => Function;
Wrap function. Says how perform this function to get the result.
Do not accept arguments.
Synchronous only.
var s = T.sync(function() { return 123; });
// calling
s() // => 123
// .toString
s.toString() // => 123
String(s) // => 123
s + '' // => 123
console.log(s) // 123
// Templates.render
T.render(s, console.log) // null, 123
// rerender
T.sync(function() { return T.data(123); })// => 123
(argument: Function) => boolean;
T.isSyncFunction(function() {}); // => false
T.isSyncFunction(T.sync(function() {}); // => true
(argument: (callback: (error, result) => void) => void) => Function;
Wrap function. Says how perform this function to get the result.
It is assumed asynchronous use, but there is partial support synchronous call.
var a1 = T.async(function(callback) { callback(null, 123); });
var a2 = T.async(function(callback) {
setTimeout(function() { callback(null, 123); }, 10);
});
// async
a1(console.log); // null, 123
a2(console.log); // null, 123
// sync
a1(); // => 123
a2(); // Error throwed 'Asynchrony can not be converted into synchronicity!'
// .toString
a1.toString(); // => 123
a2.toString(); // Error throwed 'Asynchrony can not be converted into synchronicity!'
String(a1); // => 123
String(a2); // Error throwed 'Asynchrony can not be converted into synchronicity!'
a1 + ''; // => 123
a2 + ''; // Error throwed 'Asynchrony can not be converted into synchronicity!'
console.log(a1); // 123
console.log(a2); // Error throwed 'Asynchrony can not be converted into synchronicity!'
// Templates.render
T.render(a, console.log) // null, 123
// rerender
T.async(function(call) { callback(null, T.data(123)); })(console.log) // null, 123
(argument: Function) => boolean;
T.isAsyncFunction(function() {}); // => false
T.isAsyncFunction(T.async(function() {}); // => true
- TData: Renderer|sync|async|Mixin
- TCallback: (error, result) => void
- TSelector: string
- TInjector: () => void
- TAttributes: [name: string]: TData
- TContext: TData
Node.js:
var T = require('oswst');
Require.js:
define(['oswst'], function(T) {});
window
:
var T = window.oswst(_, async);
-
Templates
- .compile
- .include
- .render
- .renderContext
- .renderAttributes
- .renderSelector
- .sync
- .isSyncFunction
- .async
- .isAsyncFunction
- .Prototype()
- .Renderer > .Prototype
- .Data > .Renderer
- .data > .Data
- .Tag > .data
- .Single > .Tag
- .singles[string]
- .Double > .Tag
- .doubles[string]
- .Doctype > .Tag
- .doctypes[string]
- .xml > .Tag
- .Mixin > .Data
- .mixin > .Mixin
- .mixins[string]
- .Module > .Renderer
- .with