Detailed introduction to HTML code reuse practices
Preface
Usually for some pages we make, we can see from the design drawings that there are some similarities. For example: header, bottom, sidebar, etc. If you are a student who makes static pages, you can only copy and paste these repeated parts to a new page. If the number of pages increases and there are areas that need to be repaired in the common parts. However, more than 10 pages use this public html code. Wouldn't it be troublesome to modify it?
For back-end students, they can split it through templates. Doing so can improve the reusability and maintainability of html code. But for students who just make static pages from design drawings, html does not provide a method like template include. But you don’t want to use back-end templates, then the following tools I will introduce may be able to help you.
gulp-file-include
The first thing I want to introduce is a gulp plug-in, which provides a include method to We can separate the public parts just like the backend template. And the include method provided has many configuration items. For details, you can check out gulp-file-include.
Let’s write a small demo to quickly understand. We need to install gulp and gulp-file-include first.
npm install -g gulp mkdir gulp-file-include && cd gulp-file-include npm install gulp --save-dev npm install gulp-file-include
After installation, let’s simply organize the file directory:
|-node_modules|-src // 生产环境的 html 存放文件夹 |-include // 公共部分的 html 存放文件夹 |-*.html |-dist // 编辑后的 html 文件 gulpfile.js
In the newly created gulpfile.js, configure gulp-file-include :
var gulp = require('gulp');var fileinclude = require('gulp-file-include');
gulp.task('fileinclude', function() {
gulp.src('src/**.html')
.pipe(fileinclude({ prefix: '@@', basepath: '@file'
}))
.pipe(gulp.dest('dist'));
});Then create two new html files, namely the header and the bottom:
header.html
<h1>这是 header 的内容</h1>
footer.html
<h1>这是 footer 的内容</h1>
Finally create a new html and add the header and footer## to be used #Giveincludecome in.
layout.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><body>
@@include('include/header.html') <p> 这是 layout 的内容 </p>
@@include('include/footer.html')</body></html>gulp fileinclude:
dist directory and there will be a layout.html file, which is what was finally compiled.
html code you write more maintainable and reusable.
gulp-file-include is simple and easy to use. It is a good small tool for students who do not want to use templates. But for students who are familiar with front-end templates, we can also use templates to achieve html code maintainability and reusability. Then I will use a ejs template that I commonly use to talk about how to separate those public parts of the html files.
ejs. Then delete the node_modules folder and delete all the html files under the dist folder.
ejs template, you need to change the suffix names of the html files in src to .ejs. The tool to compile ejs files into html still uses gulp. Just install gulp-ejs.
npm install gulp --save-dev npm install gulp-ejs --save-dev
gulpflie.js file:
var gulp = require('gulp');var ejs = require('gulp-ejs');
gulp.task('ejs', function() {
gulp.src('src/**.ejs')
.pipe(ejs())
.pipe(gulp.dest('dist'));
});layout.ejs file:
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title></head><body> <%-include include/header %> <p> 这是 layout 的内容 </p> <%-include include/footer %> </body> </html>
gulp ejs in the command line tool, and see the compiled layout.html file in the dist directory. And you're done.
include method, which may seem a bit big but of little use. Interested students can learn more about some methods of templates.
The above is the detailed content of Detailed introduction to HTML code reuse practices. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1378
52
Table Border in HTML
Sep 04, 2024 pm 04:49 PM
Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.
HTML margin-left
Sep 04, 2024 pm 04:48 PM
Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.
Nested Table in HTML
Sep 04, 2024 pm 04:49 PM
This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.
HTML Table Layout
Sep 04, 2024 pm 04:54 PM
Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.
HTML Input Placeholder
Sep 04, 2024 pm 04:54 PM
Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.
HTML Ordered List
Sep 04, 2024 pm 04:43 PM
Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively
Moving Text in HTML
Sep 04, 2024 pm 04:45 PM
Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
HTML onclick Button
Sep 04, 2024 pm 04:49 PM
Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.


