A
A
AlpineMilk2020-04-22 14:06:34
Twig
AlpineMilk, 2020-04-22 14:06:34

How to make a path alias for Gulp?

Hello! I want to track twig files through gulp so that when the file changes, the page is automatically updated.

In twig, files are inherited through such an entry

{% extends '@VendorBundle/base_layout.html.twig' %}
because of this, errors in the gallop fly out, he does not understand what kind of path this is.

Is it possible to add an alias for the gallp so that when such a phrase is found @VendorBundle, it substitutes the path that I will indicate to it, for example vendor/bundle/base.html.twig?

gulpfile.js
/*global require*/
"use strict";
var gulp = require('gulp'),
    twig = require('gulp-twig'), 
    plumber = require('gulp-plumber'),
    browserSync = require('browser-sync'),
    replace = require('gulp-replace');
/*
 * Directories here
 */
var paths = {
    build: './public/build/',
};


gulp.task('twig', function () {
    return gulp.src(['templates/*.twig'])
        .pipe(replace('@VendorBundle', 'vendor/vendor-bundle/Resources/views/'))
        // Stay live and reload on error
        .pipe(plumber({
            handleError: function (err) {
                console.log(err);
                this.emit('end');
            }
        }))
        .pipe(twig())
        .on('error', function (err) {
            process.stderr.write(err.message + '\n');
            this.emit('end');
        })
        .pipe(gulp.dest(paths.build));
});

gulp.task('rebuild', ['twig'], function () {
    // BrowserSync Reload
    browserSync.reload();
});


gulp.task('browser-sync', ['twig'], function () {
    browserSync({
        server: {
            baseDir: paths.build
        },
        notify: false,
    });
});


gulp.task('watch', function () {
    gulp.watch([
            'templates/**/*.twig',
        ],
        {cwd:'./'},
        ['rebuild']);
});

gulp.task('build', ['twig']);
gulp.task('default', ['browser-sync', 'watch']);


Mistake:
Unable to find template file /home/user/PhpstormProjects/application/templates/@VendorBundle/layout.html.twig. Error: ENOENT: no such file or directory, stat '/home/user/PhpstormProjects/application/templates/@VendorBundle/layout.html.twig'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xdevelx, 2020-04-22
@AlpineMilk

@VendorBundle is the namespace https://github.com/twigjs/twig.js/wiki#user-conten...
There is a corresponding option in gulp-twig https://github.com/zimmen/gulp-twig/blob/ 56f060de3...

.pipe(twig({
  namespaces: { 
    'VendorBundle': 'vendor/vendor-bundle/Resources/views/' 
  }
}))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question