Created
December 4, 2017 11:47
-
-
Save Shwartz/7aa933ec22c02ff3436311ee13757de3 to your computer and use it in GitHub Desktop.
Mini handlebar with a Gulp. Works similar like php include(), can pass variable as well
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Task will run through /src/html/ folder and will loop through each html file | |
When it finds {{head}}, it will be replaced with a content from /src/inc/head.html | |
Example of src/inc/breadcrumb.html | |
<ul> | |
<li><a href="link/page">Home</a></li> | |
<li>{{content}}</li> | |
</ul> | |
Example of any page where breadcrumb is needed: | |
<div class="content"> | |
{{breadcrumb="Some Link"}} | |
</div> | |
We passing string "Some Link" | |
gulp will replace {{breadcrumb="..."}} and will do inside replacement for breadcrumb.html {{content}}, | |
that way we can use same breadcrumb variable and get different content based on a page (or for meta description etc) | |
*/ | |
gulp.task('html', () => { | |
const getIncludes = (path) => fs.readFileSync(path, "utf8"); | |
gulp.src(['src/html/*.html']) | |
.pipe(replace('{{head}}', getIncludes("src/inc/head.html"))) | |
.pipe(replace(/{{breadcrumb=".*?"}}/, function (match){ | |
/* a way to pass content through declared "handlebar" | |
* {{content}} is replaced with a string between "" quotes. | |
* */ | |
const breadcrumb = getIncludes("src/inc/widget/breadcrumb.html"); | |
return breadcrumb.replace(/{{content}}/, match.match( /"(.*?)"/ )[1]); | |
})) | |
.pipe(htmlmin({collapseWhitespace: true})) | |
.pipe(gulp.dest('public/')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment