Comments (8)
sure. I use path.resolve
to get full path.
so
var path = require('path');
path.resolve('/a/b', './c.html');
// '/a/b/c.html'
path.resolve('/a/b', '/c.html');
// '/c.html'
from gulp-file-include.
What I have so far:
HTML template files that are named with .tmpl extension in many different folder levels all over the site
HTML partial files are named with *.inc extension and are all stored in the following location (/snippets/.inc)
What I want to do is not have to keep track of all my host templates' location in the site - messing with (../.inc) (../../../.inc)
The following code works but is relative based:
HTML files currently have this in them:
@@include('../../snippets/_head.inc')
I'd like all the HTML files to have the same include statement regardless of where its located in the file structure inside the site
@@include('/snippets/_head.inc')
This is the gulp file that runs it...
var gulp = require('gulp'),
include = require('gulp-file-include'),
rename = require('gulp-rename'),
chmod = require('gulp-chmod');
gulp.task('include', function() {
gulp.src(['**/*.tmpl'])
.pipe(include({prefix: '@@',basepath: '@file'}))
.pipe(rename({extname: ""}))
.pipe(rename({extname: ".html"}))
.pipe(chmod(444))
.pipe(gulp.dest('./'))
});
so I guess I need to add the following into my gulp file somewhere:
var path = require('path');
path.resolve('/a/b', './c.html');
// '/a/b/c.html'
path.resolve('/a/b', '/c.html');
// '/c.html'
I tried this but it's not working:
var gulp = require('gulp'),
include = require('gulp-file-include'),
rename = require('gulp-rename'),
chmod = require('gulp-chmod');
path = require('path'); // DO I HAVE TO INSTALL THIS OR IS IT ALREADY IN NODE.JS? I' can't find gulp-path?
gulp.task('include', function() {
path.resolve('/snippets', './*.inc');
// '/snippets/*.inc'
path.resolve('/snippets', '/*.inc');
// '/*.inc'
gulp.src(['**/*.tmpl'])
.pipe(include({prefix: '@@',basepath: '@file'}))
.pipe(rename({extname: ""}))
.pipe(rename({extname: ".html"}))
.pipe(chmod(444))
.pipe(gulp.dest('./'))
.pipe(notify({ message: 'include task complete' }));
});
from gulp-file-include.
so I guess I need to add the following into my gulp file somewhere:
no, you needn't.
path = require('path'); // DO I HAVE TO INSTALL THIS OR IS IT ALREADY IN NODE.JS
path
is in node, see doc
You just need absolute path
in include expression, such as
@@include('/Users/hx/a.html')
give a demo here.
/Users/hx/gulpfile.js
var gulp = require('gulp'),
fileinclude = require('gulp-file-include');
gulp.task('include', function() {
gulp.src(['a.html'])
.pipe(fileinclude({
prefix: '@@'
}))
.pipe(gulp.dest('./result/'));
});
/Users/hx/a.html
@@include('/Users/hx/b.html')
/Users/hx/b.html
test file: b.html
from gulp-file-include.
So if I have to go all the way back to the root of the hard drive that probably won't work since Im in a collaborative environment and other folks on my team are even on (gasp) windows machines. When they check out of svn they will have my file paths in all the HTML templates. Maybe I can use a variable that they can manage in their local gulpfile?
from gulp-file-include.
yes, it's hard to work with others by absolute path
.
from gulp-file-include.
It would be awesome if I could use a variable in the include path thats in the HTML template and store the variable in the gulp file.
HTML template
@@include('%fullpath%/b.html')
Gulpfile.js
var.fullpath: '/something/something/'
from gulp-file-include.
just try
var gulp = require('gulp'),
fileinclude = require('gulp-file-include');
gulp.task('include', function() {
gulp.src(['a.html'])
.pipe(fileinclude({
prefix: '@@',
basepath: '/something/something/'
}))
.pipe(gulp.dest('./result/'));
});
see readme
options.basepath
- type:string
, it could be
your-basepath
include file relative to the basepath you give
from gulp-file-include.
That works!, now each @@include just references the actual filename. I only have to manage the full path to the folder where the included files live in the gulp file. Thats easy to manage in a collaborative environment.
- Pull each *.tmpl file no matter where it is in my nested folders
- strip the extension
- name the extension.html
- save it in the same directory
(I'm also doing some other stuff in my gulp task that I stripped out of this response like locking the *.html files with chmod to 444 to prevent someone from forgetting and editing a *.html file.)
gulp.task('include', function() {
return gulp.src('**/*.tmpl')
.pipe(include({prefix: '@@',basepath: '/Users/euro/WebServer/sitexxx/snippets/'}))
.pipe(rename({extname: ""}))
.pipe(rename({extname: ".html"}))
.pipe(gulp.dest('./'))
.pipe(notify({ message: 'include task complete' }));
});
from gulp-file-include.
Related Issues (20)
- Postfix
- Support recursive include? HOT 1
- Multiline in variable and json error HOT 1
- Can I use different styles for different source files?
- More error info
- How to loop array of objects if it is inside array of objects in the same JSON file?
- could i config custom `@@webRoot` or add `@@urlRoot` ? HOT 6
- Filters documentation
- Is not difined in IF statement HOT 2
- Question: Extension support in vscode
- Include with part of the JSON HOT 1
- nested for loop, inner variable is not defined HOT 1
- Can html page files be generated in batches? HOT 1
- @@webRoot documentation HOT 4
- Invalid character error HOT 1
- Build only the files that will be changed
- Detect which file have an error
- Fix : invalid characters in JSON5 HOT 2
- json not work in loop HOT 1
- Pass variables to child include
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gulp-file-include.