scottmcpherson/meteor-io

how would you go about generating a variable number of, say, helper methods?

Closed this issue · 3 comments

I see how you can interpolate data, but that seems to rely on the base template being pretty static. Imagine you want to build a modal popup that lets developers specify the names of helper methods (or events, etc) to add, and then generate empty function definitions in the helpers and events map--how would I go about that? What is the best practices way you would recommend? What about a an api method to generate a map of methods/properties?

Here's how I'd imagine doing that now:

Template.{{name}}.helpers({
{{helpers}}
});

this.writeTemplateWithData({
      src: [this.ioTemplatesPath, 'template.js'],
      dest: [this.config.dest, fileBaseName + '.js'],
      data: { name: templateName, helpers: formattedStringOfHelpers }
});

It just seems that formatting the helpers string might get annoying, and being able to provide an array of helper names could be something that makes sense as a core api feature. Here's an example of how it could look :

Template.{{name}}.helpers({
    {{io.helpers helperNames}}
});

this.writeTemplateWithData({
      src: [this.ioTemplatesPath, 'template.js'],
      dest: [this.config.dest, fileBaseName + '.js'],
      data: { name: templateName, helperNames: ['selected', 'posts', 'etc'] }
});

The next step would be being able to provide a value for the function as well, so in this example helperNames could be an array of arrays, where each child array has 2 items: the name and then the function string. It would also be nice if there was a way to generate an ES6 class

or maybe, I'm missing something and it's done simply like this:

Template.{{name}}.helpers({
    {{#each  helper in helperNames}}
           {{helper}}: function() {

           }
    {{/each}}
});

Hey @faceyspacey. I haven't tested this yet, but you should definitely be able to do this.

You might need to pass in a collection instead of an array of strings though.

So maybe something like this:

Template.{{name}}.helpers({
    {{#each helperNames}}
        {{name}}: function() {

        }
    {{/each}}
});

this.writeTemplateWithData({
     src: [this.ioTemplatesPath, 'template.js'],
     dest: [this.config.dest, fileBaseName + '.js'],
     data: { name: templateName, helperNames: [{ name: 'selected'}, { name: 'posts' }] }
});

I haven't tested this out so let me know if this works out for you. If not, we'll get it figured out.

And as far as generating es6 classes, you can do that too. io can generate any kind of code.

@faceyspacey Closing this for now. Let me know if this works out for you