0.2.4 • Published 9 years ago

question-helper v0.2.4

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

question-helper NPM version

Template helper that asks a question in the command line and resolves the template with the answer.

Inspired by conversations with Jon Schlinkert

Install with npm

$ npm i question-helper --save

Usage

var question = require('question-helper');

// Prompt a user for a question and get an answer back.
question("What's your name?", function (err, answer) {
  console.log(chalk.green("Hi %s!"), answer);
});

Params

  • key {String}: Either a key on a questions object on the context or a question to ask.
  • options {String}: Additional options to pass.
  • callback {Function}: Callback function that will be passed an error and/or the results of asking the question.

Examples

Pass a questions object, where the value of each property is a question to ask:

var context = {
  questions: {
    name: 'What is your name?',
    description: 'Project description?',
    author: {
      name: 'Author name?',
      url: 'Author url?',
    }
  }
};

// Q: 'What is your name?'
question('name', context, function (err, answer) {
  if (err) console.log(err);
  // A: 'Jon'
});

// Q: 'Project description?'
question('description', context, function (err, answer) {
  if (err) console.log(err);
  // A: 'Template helper that asks a question...'
});

// Q: 'Author name?'
question('author.name', context, function (err, answer) {
  if (err) console.log(err);
  // A: 'Brian Woodward'
});

Template

To use with template:

npm i template --save

Then add to your project.

var template = require('template');

handebars

Register handlebars as an engine:

template.engine('hbs', require('engine-handlebars'));

var question = require('question-helper');
var context = {questions: {name: "What's your name?"}};

template.page('author.hbs', "Author: {{question 'name'}}");
template.render('author.hbs', context, function (err, content) {
  if (err) return console.log('error', err);
  console.log(content);
});

Lo-Dash

Register Lo-Dash as an engine:

template.engine('html', require('engine-lodash'));

var context = {questions: {name: "What's your name?"}};
template.page('author.html', "Author: <%= question('name') %>");

template.render('author.html', context, function (err, content) {
  if (err) return console.log('error', err);
  console.log(content);
});

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Related

  • engine-handlebars: Handlebars engine, consolidate.js style but with enhancements. This works with Assemble, express.js, engine-cache or any… more
  • engine-lodash: Lo-Dash engine, consolidate.js style but with enhancements. Works with Assemble, express.js, engine-cache or any application… more
  • handlebars-helpers: 120+ Handlebars helpers in ~20 categories, for Assemble, YUI, Ghost or any Handlebars project. Includes… more
  • template-helpers: Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… more
  • template: Render templates using any engine. Supports, layouts, pages, partials and custom template types. Use template… more

Author

Brian Woodward

License

Copyright © 2015 Brian Woodward Released under the MIT license.


This file was generated by verb-cli on August 07, 2015.

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago