Use javascript gulp tool
and it will be like this :
@@include('./header.html')
<!-- Content -->
<section>
<h1>Hello world</h1>
</section>
@@include('./footer.html')
One of the best ways to have the option of including repeating blocks is using Gulp.js and some packages . gulp is a popular javascript toolkit to automate & enhance your workflow .
for using it first install gulp in your project using yarn or npm :
yarn init
Install the gulp-file-include plugin :
yarn add gulp gulp-file-include -D
create gulpfile to be able to create tasks with Gulp
In linux :
touch gulpfile.js
if you are using windows use this command instead :
type "gulpfile.js"
In the gulpfile.js import gulp and gulp-file-include. you will also create a variable paths to define the path of source and the destination path (where the static html files will be after the build) :
const gulp = require('gulp');
const fileinclude = require('gulp-file-include');
const paths = {
scripts: {
src: './',
dest: './build/'
}
};
In gulpfile.js file , create a task function that will be responsible for including html files and returning static files:
async function includeHTML(){
return gulp.src([
'*.html',
'!header.html', // ignore
'!footer.html' // ignore
])
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(gulp.dest(paths.scripts.dest));
}
For now set function as default :
exports.default = includeHTML;
Add the include tags to index.html:
@@include('./header.html')
<!-- Content -->
<section>
<h1>Hello world</h1>
</section>
@@include('./footer.html')
Run the gulp command :
yarn gulp
The build folder will be created with index.html file inside
Done :)
<iframes>
, but I would never do that. – Beamends<!--#include file="myIncludeFile.html" -->
which you can do stuff like headers and footers and stuff in. – Engadine