Coder Social home page Coder Social logo

Comments (8)

haoxins avatar haoxins commented on May 28, 2024

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.

euro avatar euro commented on May 28, 2024

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.

haoxins avatar haoxins commented on May 28, 2024

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.

euro avatar euro commented on May 28, 2024

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.

haoxins avatar haoxins commented on May 28, 2024

yes, it's hard to work with others by absolute path.

from gulp-file-include.

euro avatar euro commented on May 28, 2024

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.

haoxins avatar haoxins commented on May 28, 2024

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.

euro avatar euro commented on May 28, 2024

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.

  1. Pull each *.tmpl file no matter where it is in my nested folders
  2. strip the extension
  3. name the extension.html
  4. 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.