Home >Web Front-end >HTML Tutorial >HTML code reuse practice_html/css_WEB-ITnose

HTML code reuse practice_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:39:431663browse

Foreword

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 a static page, 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?

Take SF for example, their header and bottom are the same on every page (some pages are different):

↑Header↑
↓Bottom↓

For back-end students, they can split it through templates. Doing this 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 one I want to introduce is a gulp plug-in. It provides an include method that allows us to separate the public parts like a back-end template. go out. 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 gulpmkdir gulp-file-include && cd gulp-file-includenpm install  gulp --save-devnpm 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, one for the header and one for the bottom:

header.html

<h1>这是 header 的内容</h1>

footer.html

<h1>这是 footer 的内容</h1>

Finally, create a new html and include the header and footer to be used.

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>

Finally go back to the command line tool and execute gulp fileinclude:

See after the compilation is completed , go to the dist directory and there is a layout.html file, which is what was finally compiled.

Okay, now that you understand the above small example. It may be able to greatly improve productivity in future work, making the HTML code you write more maintainable and reusable.

Front-end template

As mentioned above, 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 maintainability and reusability of HTML code. Then I will use a commonly used ejs template to talk about how to separate those public parts of html files.

Copy the entire folder of the previous example to a new location, and then change the name to ejs. Then delete the node_modules folder and delete all the html files in the dist folder.

If you use the ejs template, you need to change the suffix 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-devnpm install gulp-ejs --save-dev 

Then modify the 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'));});

Then modify the 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>

Finally, use the command line tool Run gulp ejs inside and see the compiled layout.html file in the dist directory. And you're done.

In fact, templates have many powerful methods, and the above example mainly demonstrates the include method, which may seem a bit big and small. Interested students can learn more about some methods of templates.

Summary

With the tools and templates mentioned above, even those who cut pictures and create static pages can greatly improve their development efficiency, and no longer have to worry about Frustrated by modifying public parts. In this way, we can spend the rest of the time picking up girls (escaping.

If you have any good tools or suggestions, I hope you can communicate with me. Let’s encourage everyone in SF.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn