Closed twentyfortysix closed 2 years ago
I can't still figure the right solution out.
I don't how you guys would do it.. but I would not end the action with error, but will clean the template instead https://docs.omniref.com/js/npm/twig/0.5.2/symbols/Twig.validateId
I think the problem is related to this https://github.com/kss-node/kss-node/issues/305
Use try/catch:
try {
var template = Twig.twig({
id: template_id,
href: template,
async: true,
load: function(template) {
do_something();
}
});
}
catch(err) {
if(err.message=="There is already a template with the ID detalles"){
do_something();
}
}
function do_something(){
var ajaxTemplate = Twig.twig({ ref: template_id }); var output = ajaxTemplate.render(data); }
Confirm, the same issue and i can not use already rendered template (with existing ID). Is there a way how to destroy rendered piece of html?
Guys, I've found a solution, at least it helped me.
I was using in a loop the code below and i was receiving the error There is already a template with the ID xxx
.
After some investigation I fixed it like this:
Twig.cache(); // <- this helped me to clear all the cached templates
Twig.twig({
id: 'include',
data: fs.readFileSync('views/include.twig').toString()
});
var html = Twig.twig({
id: 'layout',
ref: 'include',
allowInlineIncludes: true,
data: fs.readFileSync('views/layout.twig').toString()
}).render();
In my understanding, the idea was to use an ID which you can call multiple times. Maybe I'm missing something. Have the same error. For now @ddimitrioglo solution with Twig.cache();
worked for me. If anybody has an idea, please help :)
Do you define a template in ajax success callback? If so, try to define it before ajax.
var template = twig({
id: "events",
href: "{{ theme.link }}/twig/twig-js/calendar.twig",
async: false
});
$.ajax({
success: function (resp){
events = resp.items;
var postsHTML = template.render({
events : events
});
document.getElementById("calendar").innerHTML = postsHTML;
}
});
To check if a template has already been loaded, you can do the following:
if (Twig.twig({'ref': 'id'}) === null) {
// not loaded yet
}
Hello
what should I do in order to stop this error
Uncaught TwigException: There is already a template with the ID xyz
the error shows up when the usser does the AJAX call on same page twice.
Is there some proper way how to check the template existence or delete it before it is called again, or ..?
the code is something like this: