twigjs / twig.js

JS implementation of the Twig Templating Language
BSD 2-Clause "Simplified" License
1.89k stars 275 forks source link

TwigException: There is already a template with the ID ... #414

Closed twentyfortysix closed 2 years ago

twentyfortysix commented 8 years ago

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:

events = resp.items;
var template = twig({
id: "events",
href: "{{ theme.link }}/twig/twig-js/calendar.twig",
      async: false
});
var postsHTML = template.render({
     events : events
});

document.getElementById("calendar").innerHTML = postsHTML;
twentyfortysix commented 8 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

piratawww commented 8 years ago

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

ddimitrioglo commented 7 years ago

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?

ddimitrioglo commented 7 years ago

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();
antoinehenrich commented 5 years ago

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 :)

toptalo commented 5 years ago

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;
    }
});
willrowe commented 2 years ago

To check if a template has already been loaded, you can do the following:

if (Twig.twig({'ref': 'id'}) === null) {
    // not loaded yet
}