Home » Angularjs » How to fix template paths in templateCache? (gulp-angular-templatecache)

How to fix template paths in templateCache? (gulp-angular-templatecache)

Posted by: admin November 30, 2017 Leave a comment


I’m using gulp-angular-templatecache to generate a templateCache.js file which combines all my HTML template files into 1. (my full gulpfile)

After injecting that new module into my app, my Directives will automatically pick up the templates and I won’t need to add the partial .html files into my build folder.

The problem is that the leading folder path is getting cut off, see my example below:

The paths in my Directives:

templateUrl : "panels/tags/tagsPanel.html"...
templateUrl : "header/platform_header/platformHeader.html"...

The paths in my produced templateCache file:


^ panels and header are getting lost.

enter image description here

I’m trying to write a function that will fix that in my Gulpfile.

The config section of my Gulpfile:

var config = {
    destPartials: 'app/templates/'

My html-templates gulp.task

gulp.task('html-templates', function() {
    return gulp.src(config.srcPartials)
    .pipe(templateCache('templateCache.js', {
        root: updateRoot(config.srcPaths)
    { module:'templateCache', standalone:true })

function updateRoot(paths) {
    for (var i = 0; i < paths.length; i++) {
        // console.log(paths);
        return paths[i];

^ The above is working, in that it uses the root option in gulp-angular-templatecache to append a new string in front of the template paths.

Problem is my code above returns once and updates all the paths to the first item in the paths Array which is beta/.

How would you write this so that it correctly replaces the path for each file?


Figured it out! I should not have been using exact folder names, but globs ** in my config

var config = {
    destPartials: 'app/templates/'

The updated gulp.task:

gulp.task('html-templates', function() {
    return gulp.src(config.srcTemplates)
    .pipe(templateCache('templateCache.js', { module:'templateCache', standalone:true })

Now the output is correct:



I ran into a similar issue, but in my case, it wasn’t possible to use the same directory for all the gulp.src entries.

There is a solution that will work all those folders

return gulp.src(['public/assets/app1/**/*.tpl.html', 'public/assets/common_app/**/*.tpl.html'])
        root: "/",
        base: __dirname + "/public",
        module: "App",
        filename: "templates.js"

That presumes the gulpfile is one directory up from the public directory. It will remove the base string from the full path of the files from src.
Base can also be a function that will be passed the file object which could be helpful in more complicated situations. It’s all around line 60 of the index.js in gulp-angular-templatecache


You can also use this gulp plugin which can read your routes, directives and replace the templateUrl with the template referenced in the templateUrl.

This will remove all headache regarding handling templateUrl in your application. This uses relative url to directive js files instead of absolute url.



angular.module('test').directive('helloWorld', function () {
    return {
        restrict: 'E',
        // relative path to template
        templateUrl: 'hello-world-template.html'


    Hello world!


var gulp = require('gulp');
var embedTemplates = require('gulp-angular-embed-templates');

gulp.task('js:build', function () {

gulp-angular-embed-templates will generate the following file:

angular.module('test').directive('helloWorld', function () {
    return {
        restrict: 'E',
        template:'<strong>Hello world!</strong>'