java文件引入html文件,gulp-html-import,在html中引入外部html文件

写于 2016.06.14css

项目地址:html

曾经学习PHP的时候,深深以为include语法很是好用,后接触了ejs,发现里面也有相似的语法,可以方便地引入公共html文件;在学习了vue,react等框架之后,“组件化思想”更是在我脑海根深蒂固,再也没法忍受每一个页面重复大量代码的原始方法。可是,在最最普通的静态html开发过程当中,我实在懒得用框架,只想用最基本的方式写几个静态页面出来,这时候才想起,没有include语法,每一个页面的公共部分都要手动复制粘贴一次,实在不科学……node

早上看了张鑫旭老师的文章《JS通常般的网页重构可使用Node.js作些什么》,深受启发,因而立刻蹦起床尝试着把当中内容实现一遍,并尝试着搭配gulp,制做一个简单好用的插件,实现相似PHPinclude语法可以引入静态html文件的功能。react

由于喜欢less语法,因此使用了相似less的@import 'xxx.less';做为引入方式。git

下面直接粘贴项目readme的内容github

gulp-html-import

A gulp plugin which can import .html files into .html filesnpm

Usage

First, install gulp-html-import as a devDependency:gulp

npm install gulp-html-import --save-dev

复制代码

Then add it to the gulpfile.js:bash

var htmlImport = require('gulp-html-import');

gulp.task('import', function () {

gulp.src('./demo/index.html')

.pipe(gulpImport('./demo/components/'))

.pipe(gulp.dest('dist'));

})

复制代码

Example

Here is the files tree:

|

-- demo

| |

| -- components

| | |

| | -- header.html

| | |

| | -- footer.html

| |

| -- index.html

|

-- gulpfile.js

复制代码

Html files:

Gulp-html-import Example

@import "header.html"

Hello World

@import "footer.html"

复制代码

In your index.html, you should use

@import "XXX.html"

复制代码

to import your components.

I am the header

复制代码

I am the footer

复制代码

When you get into the root directory(where your gulpfile.js is) and type

gulp import

复制代码

you could see a html file in /dist like this:

Gulp-html-import Example

I am the header

Hello World

I am the footer

复制代码

Everything is OK.

API

htmlImport(string)

string

Type: String

The url of your components

Copyright © 2016 Jrain Lau

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值