pz
v1.1.1
Published
Dead simple templating.
Downloads
9
Readme
pz
Dead simple templating. Requires Node.js.
Install
npm install -g pz
Creating your first template
$ pz --create template
? Name: foo
? Template file extension: tmpl
? View engine: lodash
? Outputted file name: ./somewhere/<%= foo_name %>.js
Generating a file using your first template.
Your template is empty (we'll fix that later), but you can already use it to generate new files:
$ pz --create foo
? Name: bar
? Description: A pretty useless file, for now!
Customizing your template.
In the subdirectory /pz-templates/create/foo, you'll find two files: foo.tmpl and index.js. We can edit the prompts defined in index.js. Pz uses Inquirer to handle command prompting. We'll answer these prompts whenever we create a new foo file.
module.exports = {
translate: {
'foo.tmpl': './somewhere/<%= foo_name %>.js'
},
engine: 'lodash',
beforePrompt: function(context, done) {
//this is an optional function
console.log('I\'mma bout to prompt you.');
done();
},
beforeRender: function(context, done) {
//this is an optional function
//context.viewModel.anotherValue = 1;
//console.log(context.viewModel);
done();
},
prompts: [
{
'name': 'foo_name',
'message': 'Name'
},
{
'name': 'description',
'message': 'Description'
},
{
'name': 'message',
'message': 'Message',
'default': 'Aloha, World!'
}
],
after: function() {
//this is an optional function
//console.log('all done.');
}
};
Let's edit the template file, foo.tmpl, to make use of our prompts:
'use strict';
/*
* <%= foo_name %>
* <%= description %>
*/
(function($) {
$(function() {
alert('<%= message %>');
});
})(jQuery);
Time to generate a new foo file:
$ pz --create foo
I'mma bout to prompt you.
? Name: hello
? Description: Say hello using javascript.
? Message: Aloha, world!
Here's the generated somewhere/hello.js file:
'use strict';
/*
* hello
* Say hello using javascript.
*/
(function($) {
$(function() {
alert('Aloha, World!');
});
})(jQuery);
Sharing templates and view models
Let's say our foo template lives in git/pz-templates. But we want to use foo in the directory git/chocolate. First, we'll need to create a pz config file in the chocolate folder:
$ cd chocolate
$ pz --create config
Then, open the generated config file, at documents/git/chocolate/pz-templates/config.json, and add the full git/pz-templates path to the paths array. We'll also add some context variables, just for kicks:
{
"context": {
"author": "Evan Nagle",
"email": "[email protected]",
"version": "1.0.0"
},
"paths": [
"C:/.../pz-templates"
]
}
We're good to go!
When working in the chocolate directory, our template files can make use of the context variables:
'use strict';
/*
* <%= foo_name %>
* <%= description %>
*
* Author: <%= context.author %>
* Email: <%= context.email %>
* Version: <%= context.version %>
*/
(function($) {
$(function() {
alert('<%= message %>');
});
})(jQuery);