List the double braced Template tags


#1

Hey everyone!

My project had a little desire, and I think Meteor can do it, but I am not quite sure on how to do it.

Here’s the thing:
Consider I created a template named "headerMeta"
In this “headerMeta”, I have used some double braced template tags which aren’t defined yet.
I want to get the list of these tags using some function. Any idea how can I do this ?

Example:

<template name=“headerMeta”>
<h1> {{ Name }} </h1>
<p> {{ Tagline }} </p>
</template>

I need some function that tells me that in headerMeta, the tags used are:

  1. Name
  2. Tagline

#3

I don’t know if it is possible to achieve what you want automatically, but you can do this:

Create global helper (Template.registerHelper), call it register, for example, and use it in this way:

<template name="headerMeta">
    {{register 'Name, TagLine'}}
    <h1>{{Name}}</h1>
    <p>{{TagLine}}</h1>
</template>

Template.registerHelper('register', function(input) {
    Template.instance().usedHelpers = input.split(', ');
});

#4

What you’re looking for is the Spacebars compiler that Meteor itself uses to parse templates. I’m not sure how much documentation there is about how to use it, but that’s one way to solve this properly.

Another way would be to parse the template files manually and just look for simple regular expressions, like… /\{\{\s*(\w+)\s*\}\}/ which would match your expressions with Name and Tagline there, but not if the expression contained dots or slashes or additional parameters to the helper (like {{ getName this }}), which are all also valid spacebars helper expressions. You could enhance the regex a bit so it would match most of those, without having to dig into the spacebars compiler.
(Edit: And it would also not match block helpers and their closing tags {{# and {{/, and template inclusions {{>, but I understood that you’re not interested in those.)