gulp-file-include
    Overview
    Issues
    Find an Expert
    Code
    Documentation
    Insights & Analysis
    Dependencies
    Career Opportunities
    Alternatives
    Versions
    Playground

gulp-file-include

a gulp plugin for file include

2.1.0  •  Published 1 month ago  •  by haoxin  •  MIT License

NPM version Build status Test coverage License Dependency status Gitter

gulp-file-include

a plugin of gulp for file include

install

npm install gulp-file-include

options

  • options - type: string, just as prefix, default @@, and basepath is default @file
fileinclude('@@')
  • options - type: object

    • prefix: string, default @@
    • suffix: string, default ''
    • basepath: string, default @file, it could be @root, @file, your-basepath
    • filters: object, filters of include content
    • context: object, context of if statement
    • indent: boolean, default false
  • options.basepath - type: string, it could be

    • @root, include file relative to the dir where gulp running in
    • @file, include file relative to the dir where file in example
    • your-basepath include file relative to the basepath you give
fileinclude({
  prefix: '@@',
  basepath: '@file'
})
fileinclude({
  prefix: '@@',
  basepath: '/home/'
})

example

  • @@include options - type: JSON

index.html

<!DOCTYPE html>
<html>
  <body>
  @@include('./view.html')
  @@include('./var.html', {
    "name": "haoxin",
    "age": 12345,
    "socials": {
      "fb": "facebook.com/include",
      "tw": "twitter.com/include"
    }
  })
  </body>
</html>

view.html

<h2>view</h2>

var.html

<label>@@name</label>
<label>@@age</label>
<strong>@@socials.fb</strong>
<strong>@@socials.tw</strong>

gulpfile.js

var fileinclude = require('gulp-file-include'),
  gulp = require('gulp');

gulp.task('fileinclude', function() {
  gulp.src(['index.html'])
    .pipe(fileinclude({
      prefix: '@@',
      basepath: '@file'
    }))
    .pipe(gulp.dest('./'));
});

and the result is:

<!DOCTYPE html>
<html>
  <body>
  <h2>view</h2>
  <label>haoxin</label>
<label>12345</label>
<strong>facebook.com/include</strong>
<strong>twitter.com/include</strong>
  </body>
</html>

filters

<!DOCTYPE html>
<html>
  <body>
  @@include(markdown('view.md'))
  @@include('./var.html', {
    "name": "haoxin",
    "age": 12345
  })
  </body>
</html>

view.md

view
====
var fileinclude = require('gulp-file-include'),
  markdown = require('markdown'),
  gulp = require('gulp');

gulp.task('fileinclude', function() {
  gulp.src(['index.html'])
    .pipe(fileinclude({
      filters: {
        markdown: markdown.parse
      }
    }))
    .pipe(gulp.dest('./'));
});

if statement

fileinclude({
  context: {
    name: 'test'
  }
});
@@include('some.html', { "nav": true })

@@if (name === 'test' && nav === true) {
  @@include('test.html')
}

for statement

fileinclude({
  context: {
    arr: ['test1', 'test2']
  }
});
<ul>
@@for (var i = 0; i < arr.length; i++) {
  <li>`+arr[i]+`</li>
}
</ul>

loop statement

  • index.html
<body>
  @@loop('loop-article.html', [
    { "title": "My post title", "text": "<p>lorem ipsum...</p>" },
    { "title": "Another post", "text": "<p>lorem ipsum...</p>" },
    { "title": "One more post", "text": "<p>lorem ipsum...</p>" }
  ])
</body>
  • loop-article.html
<article>
  <h2>@@title</h2>
  @@text
</article>

loop statement + data.json

data.json

[
  { "title": "My post title", "text": "<p>lorem ipsum...</p>" },
  { "title": "Another post", "text": "<p>lorem ipsum...</p>" },
  { "title": "One more post", "text": "<p>lorem ipsum...</p>" }
]
  • loop-article.html
<body>
  @@loop("loop-article.html", "data.json")
</body>

webRoot built-in context variable

The webRoot field of the context contains the relative path from the source document to the source root (unless the value is already set in the context options).

example

support/contact/index.html

<!DOCTYPE html>
<html>
  <head>
    <link type=stylesheet src=@@webRoot/css/style.css>
  </head>
  <body>
    <h2>Support Contact Info</h2>
    <footer><a href=@@webRoot>Home</a></footer>
  </body>
  </body>
</html>

and the result is:

<!DOCTYPE html>
<html>
  <head>
    <link type=stylesheet src=../../css/style.css>
  </head>
  <body>
    <h2>Support Contact Info</h2>
    <footer><a href=../..>Home</a></footer>
  </body>
  </body>
</html>

License

MIT

Dependencies

balanced-match  ·  concat-stream  ·  extend  ·  flatnest  ·  plugin-error  ·  through2  ·  vinyl  ·  eslint-config-ok  ·  gulp  ·  istanbul  ·  markdown  ·  mocha  ·  should

Tags

gulpplugin
file
include
replace
gulp
plugin
Get an open source license report with a click
Whether it's for legal, customers or investors, upload your package.json files, and get a comprehensive license report page and spreadsheet. No hassle, it just works!
$49 one time fee, money back guarantee

Popularity

Weekly Downloads
6.5K
Stars
561

Issues and PRs

Activity

Last ver 1 month ago
Created 6 years ago
Last commit 1 month ago
20 days between commits

Sustainability

22 contributors

Technology

Node version: 10.15.3
18.5K unpacked

Legal and Compliance

MIT License
OSI Approved
0 vulnerabilities

Top Experts

coderhaoxin
Maintainer, 56 commits, 10 merges, 2 PRs
Works at airwallex
Bogdan Chadkin
Maintainer, 26 commits, 1 merges, 4 PRs
Works at realadvisor
Thomas Vantuycom
2 commits, 1 PRs
Tatu Teräväinen
2 commits
Dem Pilafian
Maintainer, 1 commits, 1 PRs
Works at center-key
Rúnar Berg Baugsson Sigríðarson
1 commits, 1 PRs
Works at NetApp