gulp-inject-partials
A recursive injection of partials based on their path name for gulp.
Gulp-inject-partials parses target file, located defined placeholders and injects file contents based on their relative path. See Basic usage and More examples below.
Gulp-inject-partials is based/inspired by gulp-inject
.
Note: NodeJs v4 or above is required.
Installation
Install gulp-inject-partials
as a development dependancy:
npm install --save-dev gulp-inject-partials
Basic usage
Each pair of comments are the injection placeholders (aka. tags, see options.start
and options.end
).
index.html
My index <!-- partial:partial/_mypartial.html --> <!-- partial -->
partial/_mypartial.html
This text is in partial
gulpfile.js
var gulp = ;var injectPartials = ; gulp;
Results in
My index <!-- partial:views/_mypartial.html --> This text is in partial <!-- partial -->
More examples
Injecting nested partials
Nesting partials works same way as single level injection. When injecting partials, gulp-inject-partials
will parse parent file in search for partials to inject. Once it finds a partial will then recursively parse child partial.
index.html
My index <!-- partial:views/_mypartial.html --> <!-- partial -->
views/_mypartial.html
This is in partial <!-- partial:_mypartial2.html --> <!-- partial --> <!-- partial:_mypartial3.html --> <!-- partial -->
views/_mypartial2.html
This text is in partial 2
views/_mypartial3.html
This text is in partial 3
gulpfile.js
var gulp = ;var injectPartials = ; gulp;
Results in
My index <!-- partial:views/_mypartial.html --> This is in partial <!-- partial:_mypartial2.html --> This text is in partial 2 <!-- partial --> <!-- partial:_mypartial3.html --> This text is in partial 3 <!-- partial --> <!-- partial -->
start
and/or end
tag
Setting the custom It's possible to change start and end tag by setting option.start
and options.end
respectivelly.
index.html
My index <## partial/_mypartial.html> </##>
partial/_mypartial.html
This text is in partial
gulpfile.js
var gulp = ;var injectPartials = ; gulp;
Results in
My index <!-- partial:views/_mypartial.html --> This text is in partial <!-- partial -->
Remove tags after insertion
For production purposes we would like inject tags to be removed and have a clean html. This is possible with options.removeTags
.
index.html
My index <!-- partial:partial/_mypartial.html --> <!-- partial -->
partial/_mypartial.html
This text is in partial
gulpfile.js
var gulp = ;var injectPartials = ; gulp;
Results in
My index This text is in partial
API
inject(options)
options.start
Type: string
Param (optional): path
- relative path to source file
Default: <!-- partial:{{path}} -->
Used to dynamically set starting placeholder tag, which might contain relative path
to source file. Even thou this parameter is optional, whithout it no file would be injected.
options.end
Type: string
Param (optional): path
- relative path to source file
Default: <!-- partial -->
Used to dynamically set ending placeholder tag, which might contain relative path
to source file.
options.removeTags
Type: boolean
Default: false
When true
the start and end tags will be removed when injecting files.
options.quiet
Type: boolean
Default: false
When true
gulp task will not render any information to console.
options.prefix
Type: string
Default: (Empty string)
Prefix path to prepend to every route processed e.g. relative/path/to/partials/
. Note that full route is still relative.
options.ignoreError
Type: 'boolean'
Default: false
When true
ignores missing files during the injection and shows just info message