Skip to content

Instantly share code, notes, and snippets.

@stryju
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stryju/0dbdcaa99c4540b64841 to your computer and use it in GitHub Desktop.
Save stryju/0dbdcaa99c4540b64841 to your computer and use it in GitHub Desktop.
sample partials
gulp.task( 'templates', function () {
// this is the partials.js
var wrapper = require( './partials' );
return gulp.src( 'assets/partials/**/*.html' )
.pipe( $.plumber() )
.pipe( wrapper({
// this will be added to the file path (template id)
root : '',
// this will be stripped away from the file path (template id)
base : 'assets/templates',
}))
.pipe( $.util.buffer( function ( err, files ) {
var partials = files.map( function ( file ) {
return file.contents.toString();
}).join( '\n ' );
return gulp.src( 'assets/templates/index.html' )
.pipe( $.template({
url : config.urls,
partials : partials
}))
.pipe( gulp.dest( 'build' ) );
}));
});
// jshint node:true
var gutil = require( 'gulp-util' );
var through = require( 'through2' );
var path = require( 'path' );
function wrap( file, url ) {
'use strict';
var template = '<script type="text/ng-template" id="${ url }">${ contents }</script>';
return gutil.template( template, {
url : url,
contents : file.contents.toString(),
file : file
});
}
function partial( options ) {
'use strict';
options = options || {};
var root = options.root || '';
var base = options.base || '';
if ( base && base.substr( -1 ) !== path.sep ) {
base += path.sep;
}
return through.obj( function ( file, enc, cb ) {
if ( file.isNull() ) {
this.push( file );
return cb();
}
if ( file.isStream() ) {
this.emit( 'error', new gutil.PluginError( 'gulp-partial', 'Streaming not supported' ));
return cb();
}
var url = path.join( root, file.path.replace( path.resolve( base || file.base ), '' ) );
if ( process.platform === 'win32' ) {
url = url.replace( /\\/g, '/' );
}
try {
file.contents = new Buffer( wrap( file, url ));
} catch ( err ) {
this.emit( 'error', new gutil.PluginError( 'gulp-partial', err ));
}
this.push( file );
cb();
});
}
module.exports = partial;
@stryju
Copy link
Author

stryju commented Oct 10, 2014

if few ppl like it, i'll turn it into a proper gulp plugin :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment