Search code examples
meteoriron-router

Meteor Check if Template Exists


I have Meteor application that uses Iron-Router in which the rendered screen is dynamic based on the URL.
I have two parameters from the URL, station and screen.

In the routing code:
this.render(stationParam+screenParam, {to: 'content'});

However, I want to be able to check if the template stationParam+screenParam exists.

Is this possible?


Solution

  • All templates are stored as a field of a global object, Template. The question then is: How to check if this global object has a given field?

    You have multiple ways to do it. My personal favourite using underscore:

    if (_.has(Template, stationParam + screenParam)) { /* ... */ }
    

    Underscore is included in Meteor. If you want to use it in a package, don't forget the api.use('underscore') in your describe callback.

    See also: Determining if a javascript object has a given property