[Solved] Templates currently rendered in Blaze?

How do I know what templates have been rendered in Blaze ?

Where can I put a callback to get informed every time a new template is rendered ?

maybe this could work?

  var templateNames = _.keys(Blaze.Template);
  _.each(templateNames, function(templateName){
    template = Template[templateName];
    if(template.onRendered){
      template.onRendered(function(context){
        console.log("rendered!", template, context);
      });
    }
  });

EDIT, Put it in a closure :

Template.Layout.onRendered(function(){

  var templateNames = _.keys(Blaze.Template);

  _.each(templateNames, function(templateName){
    template = Template[templateName];
    if(template.onRendered){
      var showTemplate = function(iHateClosures){
        iHateClosures.onRendered(function(){
          console.log("rendered!", iHateClosures);
        });
      }
      showTemplate(template);
    }
  });
})

Running in Template.Layout.onRendered (or Template.body.onRendered if you don’t use any router which is my case) is too late, some other templates may already have been rendered. Running in Template.body.onCreated is better. It just misses the body which can be manually added

Template.body.onCreated(function () {
  for (let name in Template) {
    const template = Template[name]
    if (template.onRendered) template.onRendered(function () {
      console.log("rentered", this.view.name)
    })
  }
})

I don’t understand why the closure is needed. After some testing it just works without the closure - at least on my app.

that’s great you got it to work! I think the reason I needed a closure was the iterated value “templateName” was not scoped to the each loop, since it’s a var. You’re using es6 let so your iterated value is scoped to the loop, so you don’t need the closure.