How to get a list of all templates?


#1

A newbie here. I got tired of adding my new and temporary templates to Iron Router, and thought to add a syntax like this:

<template name="myTemplate" routable=true>

I don’t know if it’s at all possible, but thought I’d try.

To do that, I wanted to obtain a list of all templates.

Since we see a lot of syntax like Template.myTemplate, I thought Template is something like a hash of all templates. So I went to a debugger in Chrome and evaluated Template.

To my surprise, Template evaluated to a function. When clicking to that function, I got this source code:

    Blaze.Template = function (viewName, renderFunction) {
  if (! (this instanceof Blaze.Template))
    // called without `new`
    return new Blaze.Template(viewName, renderFunction);

  if (typeof viewName === 'function') {
    // omitted "viewName" argument
    renderFunction = viewName;
    viewName = '';
  }
  if (typeof viewName !== 'string')
    throw new Error("viewName must be a String (or omitted)");
  if (typeof renderFunction !== 'function')
    throw new Error("renderFunction must be a function");

  this.viewName = viewName;
  this.renderFunction = renderFunction;

  this.__helpers = new HelperMap;
  this.__eventMaps = [];

  this._callbacks = {
    created: [],
    rendered: [],
    destroyed: []
  };
};
var Template = Blaze.Template;

So, I guess I have a few questions.

  1. If Template evaluates to a function, then what makes the syntax of Template.myTemplate possible?
  2. How do I get the list of templates?
  3. Am I on the right track?
  4. Is there a better way to do it?