
// Retrieve a template registered using twig function 'jsTemplate'
export function getJsTemplate(id) {
    if (window.template[ id ] == null) {
        throw 'Invalid template "'+id+'"';
    }
    return window.template[ id ];
}

/**
 * @deprecated use renderJsTemplate2 instead
 */
export function renderJsTemplate(tpl, mapReplace) {
    for (const k in mapReplace) {
        tpl = tpl.replaceAll('%'+k+'%', mapReplace[ k ]);// TODO : HTML escape ?
    }
    return tpl;
}

export function renderJsTemplate2(tpl, mapReplace) {
    for (const k in mapReplace) {
        tpl = tpl.replaceAll(k, mapReplace[ k ]);// TODO : HTML escape ?
    }
    return tpl;
}

/**
 * @param {string} s 
 */
export function parseHtml(s) {
    const parserEl = document.createElement('div'); // TODO
    parserEl.innerHTML = s;
    return parserEl.firstElementChild;
}


export function createElement(name, mapAttribute) {
    let el = document.createElement(name);
    for (const attrKey in mapAttribute) {
        el.setAttribute(attrKey, mapAttribute[ attrKey ]);
    }
    return el;
}