howardroark / pollinate

Template your base files and generate new projects from Git(Hub).
The Unlicense
225 stars 16 forks source link

Enable custom nunjucks `filters` as a {"filter name":"module path"} schema object property. #29

Closed ben657 closed 8 years ago

ben657 commented 8 years ago

First off, great little tool!

Would be nice to be able to do some parsing of the passed in options though, things like capitalising a name and having that available to templates too. Maybe give a js file which could export a function to do some work on the data.

e.g.

module.exports = function(data) {
  let capitalName = data.name[0].toUpperCase() + data.name.slice(1);
  data.capitalName = capitalName;
}

Then {{ capitalName }} would be available in templates.

howardroark commented 8 years ago

Thanks! I may be making assumptions but if you wanted to transform the data before outputting to a template then you can use Nunjucks' built-in filters... https://mozilla.github.io/nunjucks/templating.html#builtin-filters

Is that on track? Could be possible to extend upon that if not everything is covered... certainly would require some creativity :)

howardroark commented 8 years ago

@ben657 I wonder if this could be accomplished like this... http://stackoverflow.com/a/30920598

Perhaps a filters object could be added to the schema where each custom filter is a path to a module's file.

ben657 commented 8 years ago

Yeah, having the ability to add filters would be a good step. Could have filters as an object with keys being the name, values being the relative file to require, which should export a function.

howardroark commented 8 years ago

100% agree :)

{
  ...
  "filters": {
    "markdown": "filters/markdown.js"
  } 
}

That would be nice because if you are really crafty you can require modules in your filters.

howardroark commented 8 years ago

I added a start, but it's untested. I just assumed the stackoverflow article is all that needs to be done.

howardroark commented 8 years ago

I feel like it should work, but nunjucks doesn't seem to pickup the filter...

@ben657 Have a look if you are curious :)

howardroark commented 8 years ago

@ben657 Got it working... you can test out 1.8.0 :)

You can find a working template example here... https://github.com/howardroark/webapp

ben657 commented 8 years ago

Sorry for the silence, been busy recently, finally got a chance to try this out!

howardroark commented 8 years ago

Nice! Is it working out for you? I also added the option supply files that work with "inquirer.js" to prompt for data.

ben657 commented 8 years ago

Just got the templates written out, haven't got a use for the filters yet but will be going back and using them. Questions don't seem to be working though, got "questions": "template/prompts.js" in the template.json, but it doesn't seem to be getting run.

howardroark commented 8 years ago

Maybe open and issue and post the contents of that file or a pseudo version that is similar? I really didn't test that feature very carefully... cause I don't use it :P

howardroark commented 8 years ago

This file has become a bit wacky I feel... https://github.com/howardroark/pollinate/blob/develop/lib/extend.js

ben657 commented 8 years ago

My bad actually, hadn't updated my global install of pollinate :P Do agree with extend.js though, seems like there should be a flow that fits it better

howardroark commented 8 years ago

Good news! Yeah... I thought it was a good idea to have, but sorta just chucked it in :P At least it works!