﻿/*
    HOW TO USE:
    var placeholder = { Place1: ['Platzhalterwert1', 'Platzhalterwert2', 'Platzhalterwert3'], Place2: ['David', 'Elsner', 'Schwarz'], Place3: ['yea', 'jo'], Place4: ['ysssea', 'jssso'], Place5: ['HALLO'] };
    var filled1 = parseTemplates.getTemplate("getLi", placeholder);
    document.body.innerHTML = filled1;


*/
var Templateparser = {
    templatecontents: '',
    placeholder: null,
    clear: function () {
        this.placeholder = {};
    },
    parseTemplate: function (template) {
        if (template == undefined) return false;
        var arrplaceholder;
        arrloops = template.match(/%START%[\s\S]*?%END%/mg);
        //Diese Schleife durchläuft alle gefundenen Schleifen
        if (arrloops != null) {
            for (var a = 0; a < arrloops.length; a++) {
                var content = '';
                arrplaceholder = new Array();
                //arrplaceholder contains all the placeholdernames with Brackets
                arrplaceholder = arrloops[a].match(/{\w*?}/g);
                if (arrplaceholder != null) {
                    //Iterating through every placeholder                
                    if (Templateparser.placeholder[arrplaceholder[0].toString().replace("{", "").replace("}", "")]) {
                        for (var x = 0; x < Templateparser.placeholder[arrplaceholder[0].toString().replace("{", "").replace("}", "")].length; x++) {
                            var req = arrloops[a];
                            //The following loop is called that often like the first placeholder in this loop has elements
                            for (var i = 0; i < arrplaceholder.length; i++) {
                                req = req.toString().replace(arrplaceholder[i], Templateparser.placeholder[arrplaceholder[i].toString().replace("{", "").replace("}", "")][x] != undefined ? Templateparser.placeholder[arrplaceholder[i].toString().replace("{", "").replace("}", "")][x] : "");
                            }
                            content += req;
                        }
                    }
                }
                arrloops[a] = content.toString().replace(/%START%/g, "").toString().replace(/%END%/g, "");
            }
        }
        //Replacing the finished loops in the Template
        var tmparrloops = template.match(/%START%[\s\S]*?%END%/g);
        for (var y in tmparrloops) {
            template = template.toString().replace(tmparrloops[y], arrloops[y]);
        }
        y = 0;
        //Replacing Placeholders without loop
        arrplaceholder = template.match(/{\w*?}/g);
        for (var y in arrplaceholder) {
            if (Templateparser.placeholder[arrplaceholder[y].toString().replace("{", "").toString().replace("}", "")] != undefined) {
                template = template.replace(arrplaceholder[y].toString(), Templateparser.placeholder[arrplaceholder[y].toString().replace("{", "").toString().replace("}", "")]);
            } else {
                //THIS PART HAS TO BE FIX IN THE FUTURE ;)
                if (arrplaceholder[y].toString().indexOf("{") > -1 && arrplaceholder[y].toString().indexOf("}") > -1) {
                    if (arrplaceholder[y].length<=30)
                        template = template.replace(arrplaceholder[y].toString(), "");
                }
            }

            //template = template.replace(arrplaceholder[y].toString(), "xxx");
            //template = template.replace(arrplaceholder[y].toString(), Templateparser.placeholder[arrplaceholder[y].toString().replace("{", "").replace("}", "")] != undefined ? Templateparser.placeholder[arrplaceholder[y].toString().replace("{", "").replace("}", "")] : "");
        }

        return template;
    },
    fillTemplate: function (results) {
        Templateparser.templatecontents = results;
    }
}



















