go-plugin-handlebars
Go plugin to apply Handlebars to files
Usage
$ npm install go go-plugin-handlebars
const go = go
API
Get root directory for templates
var /* string */ templatesDirectory = go // default: "./templates"
Set root directory for templates
var /* string */ normalizedTemplatesDirectory = go
Load template to use it multiple times
// If `templateName` is a relative path it will be resolved from project root directoryvar templateName = './relative/path' // resolve as: <project_dir>/relative/path // Or otherwise it will be loaded from templates directory (by default, from `./.templates`)var templateName = 'not/relative/path' // resolve as: <project_dir>/<templates_dir>/relative/path var /* function */ renderTemplate = await go // renders template with context object and resolves with the resulting stringvar /* string */ content = await // renders template with empty context and write the result to `path`var /* string */ content = await // renders template with context object and write the result to `path`var /* string */ content = await
Process file with Handlebars and write it
// loads the file, renders it as a template with the context object and writes it to the same pathawait go
Apply Handlebars to multiple files
await go
This will need a little longer description…
Process files from the templates directory and write them to the project directory
We can process all files in the templates directory and write it to project directory
await go
Or we can process all files in the project directory and rewrite it with resulted files
await go
Just like with require()
function in NodeJS it is very important if pattern starts with /
, ./
, ../
or not. In case if it starts with one of listed partials, files will be searched starting from root project directory (where gofile.js
is located), otherwise search will start from templates directory.
In case if cwd
option is passed the behavior will change a little.
Passing search options
To use more out of searching object can be passed as first argument. In this case pattern can be specified as the options.pattern
property (will be set to **
if not given):
await go
To learn more about search options read globby docs and glob docs.
Default options are extended in go-plugin-handlebars
with next object:
gitignore: true // to ignore files mentioned in .gitignore during the search ignore: '.git/**' '**/.git/**' './**/.git/**' // to ignore any .git folder 'node_modules/**' '**/node_modules/**' './**/node_modules/**' // to ignore any node_modules folder
options.cwd
and templates direcory
The same rules about relative paths are applied to cwd
as for search pattern. At the same time, when options.cwd
is used, options.pattern
will be always relative to the value of cwd
option.
// this will look inside of templates directory for fixtures/ folderawait go // but this will look for fixtures/ folder in the root of the project because of './' in `options.cwd`await go
cwd
option plays another important role: it is ignored in filenames when saving them:
// ./fixtures/inner/file will be saved to ./src/fixtures/fileawait go // ./fixtures/inner/file will be saved to ./src/inner/fileawait go
Saving files
There are several options how to save files:
// process files in app/ folder and rewrite them with resulted filesawait go // process files from components/ folder and save resulted files to app/ folderawait go // process files from template/ folder and save them to the path generated by function called for each of the fileawait go // process files from <getTemplatsDir()> and save them to app folderawait go // when destination path is not specified, and the source is from templates directory, files will be saved to project folderawait go // the same behavior as for an example line above: the content of <getTemplatsDir()>/fixtures will be saved to project folderawait go
To see kind of patterns that can be used for searching files follow globbing patterns and make sure to read about expandDirectory feature.
Handlebars partials
Registergo
Handlebars helpers
Registergo
Examples
README.md template
.templates/README.md
# {{ name }} {{ description }} MIT © {{ year }}
gofile.js
var go = go var context = name: 'new repository' description: 'here will be more text soon' year: go
Execute
$ node gofile.js
Process files from project directory
.templates/config/dev.json
gofile.js
var go = go go var context = port: processenvPORT || 8080 host: processenvHOST || 'localhost' go
Execute
$ node gofile.js
app/config/dev.json
Process multiple files at the same time
Read "Apply Handlebars to multiple files" section for the information on go.processTemplates()
More
For more examples on template syntax read Handlebars documentation
License
MIT © Stanislav Termosa