twing
twing copied to clipboard
Added a web loader
Needs unit tests and documentation
Example:
enum TemplateTypes{view}
class Templates{
static renderTemplate(page:string, templateType:TemplateTypes){
return new Promise((resolve,reject) => {
const pViews = new Promise<string>((resolve, reject) => {
$.get(page + "?type=" + TemplateTypes[templateType], (data:string) => {
resolve(data);
});
});
const pModels = new Promise<string>((resolve, reject) => {
$.get(page + "?type=model", (data:string) => {
resolve(data);
});
});
this.loadTwig(pViews, pModels, resolve);
});
}
static loadTwig(pViews:Promise<string>, pModels:Promise<Object>, resolve:Function){
Promise.all([pViews, pModels]).then(data => {
new Promise<string>((resolve) => {
//the loader is to get other views but no models
let webLoader = new WebLoader((path:string)=>{
return new Promise((resolve) => {
$.get(path + "?type=" + TemplateTypes[TemplateTypes.view], (data:string) => {
resolve(data);
});
});
},data[0], data[1]);
webLoader.check.then(()=>{
var env = new twing.TwingEnvironment(webLoader);
resolve(env.createTemplate(data[0], 'main').render(data[1]));
});
}).then((res:string) => {
console.log(res);
});
});
}
}
let templateType = TemplateTypes.view;
Templates.renderTemplate("index", templateType).then(() => {
console.log('page loaded');
});
the check param is a workaround and I will probably remove it. As it's almost impossible to extend a Promise I build a Capsule for it which works ... maybe I will implement it tomorrow.
Also I added a "fake" super for the ArrayLoader because if I pass null it will break and I don't have the data ready for it to process:
super({'webLoader':'Templates are loaded using a WebLoader'});
I just noticed by the lack of tests in your PR that the code coverage indicator doesn't consider sources without tests. It means that there mey be some sources that are not covered without trigerring an alert. I need to fix this.
And you need to add some tests for your loader.
I will do that. Sorry for being inactive working hard on another project but will add it to my todo. @ericmorand also do you think we should add it to the main library or add it to an extra one?
@noelelias, don't know yet if it should be separate but probably. Anyway, I'm working on bringing async support to Twing (for Twing@4) and it will probably be needed for what you did.
@ericmorand ah yes I just implemented async support by pre loading the templates which works fine but it's not needed for the current version
hi @ericmorand I added the tests and fixed some stuff and also made it easier to use ... the tests can be used as an example.
I still have the super({'webLoader':'Templates are loaded using a WebLoader'});
for extending the TwingLoaderArray maybe we can allow the creation of TwingLoaderArray without a first template?
https://github.com/NightlyCommit/twing/pull/437
Once implemented can be markes as fixed:
- https://github.com/NightlyCommit/twing/issues/288
- https://github.com/NightlyCommit/twing/issues/388
@ericmorand I don't get this ... can you help me with the coverage/coveralls?
Fixed some stuff in this tree but I'm also working on the incremental dom implementation maybe it makes sense to wait until i finished working on the id implementation (still not every twing function is supported) and add both at the same time
Any concept of basepath
for the loader?
Like when http://localhost:80/templates/a/b/c.html.twig
contains {% extends "a/d.html.twig" %}
?
the loader I developed calls a callback function and passes the url "a/d.html.twig". You can decide how you want to get the content (maybe a simple ajax call)
Thank you for the answer. I hope this can make into a release "soon".