twing icon indicating copy to clipboard operation
twing copied to clipboard

Added a web loader

Open noel-schenk opened this issue 5 years ago • 12 comments

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');
});

noel-schenk avatar Oct 06 '19 22:10 noel-schenk

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'});

noel-schenk avatar Oct 06 '19 22:10 noel-schenk

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.

ericmorand avatar Oct 09 '19 08:10 ericmorand

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?

noel-schenk avatar Oct 10 '19 21:10 noel-schenk

@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 avatar Oct 11 '19 10:10 ericmorand

@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

noel-schenk avatar Oct 11 '19 12:10 noel-schenk

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

noel-schenk avatar Oct 12 '19 18:10 noel-schenk

Once implemented can be markes as fixed:

  • https://github.com/NightlyCommit/twing/issues/288
  • https://github.com/NightlyCommit/twing/issues/388

noel-schenk avatar Oct 12 '19 18:10 noel-schenk

@ericmorand I don't get this ... can you help me with the coverage/coveralls? image

noel-schenk avatar Oct 12 '19 19:10 noel-schenk

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

noel-schenk avatar Oct 13 '19 12:10 noel-schenk

Any concept of basepath for the loader? Like when http://localhost:80/templates/a/b/c.html.twig contains {% extends "a/d.html.twig" %} ?

drzraf avatar Dec 13 '19 12:12 drzraf

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)

noel-schenk avatar Dec 13 '19 13:12 noel-schenk

Thank you for the answer. I hope this can make into a release "soon".

drzraf avatar Feb 17 '20 19:02 drzraf