T
T
tory_kuzya2018-10-19 10:50:41
Pug
tory_kuzya, 2018-10-19 10:50:41

How to insert php code into pug generated PHP file by gulp builder? Maybe?

The project is built by the gulp builder. There are tasks for different tasks. Initially, .html files were collected from pug files. Then there was a need to simultaneously save the same pages in php format. I use rename to rename html -> php
Here is the html/php build task:

module.exports = function() {
    $.gulp.task('pug', ()=>  {
        return $.gulp.src('./dev/pug/pages/*.pug')
            .pipe($.gp.pug({
                locals : {
                    nav: JSON.parse($.fs.readFileSync('./data/navigation.json', 'utf8')),
                    content: JSON.parse($.fs.readFileSync('./data/content.json', 'utf8')),
                },
                pretty: true
            }))
            .on('error', $.gp.notify.onError(function(error) {
                return {
                    title: 'Pug',
                    message: error.message
                };
            }))
            .pipe($.gulp.dest('./build/'))
            .on('end', $.browserSync.reload);
    });

    $.gulp.task('pugphp', ()=>  {
        return $.gulp.src('./dev/pug/pages/*.pug')
            .pipe($.gp.pug({
                locals : {
                    nav: JSON.parse($.fs.readFileSync('./data/navigation.json', 'utf8')),
                    content: JSON.parse($.fs.readFileSync('./data/content.json', 'utf8')),
                },
                pretty: true
            }))
            .on('error', $.gp.notify.onError(function(error) {
                return {
                    title: 'Pug',
                    message: error.message
                };
            }))
            .pipe($.rename(function (path) {
                path.extname = ".php"
            }))
            .pipe($.gulp.dest('./build/'))
            .on('end', $.browserSync.reload);
    });
};

And here is the gulp file:
global.$ = {
    path: {
        task: require('./gulp/paths/tasks.js')
    },
    gulp: require('gulp'),
    del: require('del'),
    fs: require('fs'),
    browserSync: require('browser-sync').create(),
    gp: require('gulp-load-plugins')(),
    scss: require('gulp-sass'),
    rename: require('gulp-rename')
};

$.path.task.forEach(function(taskPath) {
    require(taskPath)();
});

$.gulp.task('dev', $.gulp.series(
    'clean',
    $.gulp.parallel('favicon', 'styles:dev', 'pug', 'pugphp', 'libsJS:dev', 'js:copy', 'libsCSS:dev', 'libs:dev', 'svg', 'img:dev', 'fonts','svg:copy')));

$.gulp.task('build', $.gulp.series(
    'clean',
    $.gulp.parallel('favicon', 'styles:build', 'pug', 'libsJS:build', 'js:copy', 'libsCSS:build','libs:build', 'svg', 'img:build', 'fonts','svg:copy')));

$.gulp.task('default', $.gulp.series(
    'dev',
    $.gulp.parallel(
        'watch',
        'serve'
    )
));

I would like to add php code to php files automatically, and not manually (in particular, you need to write In two places in each php file. The question is, is this possible?
?v=<?=time()?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Yumshanov, 2019-08-18
@tenzorvec

gulp build has a very nice gulp-rigger builder that embeds chunks of files into a file. the file itself uses the command "//=" and the location of the file. "//= template/time.php"
in gulpfile.js you
declare a variable:
rigger = require('gulp-rigger') , // a module for importing the contents of one file into another
and inserting the task in the place you need
.pipe(rigger ()) // attachment import
that looks at the file on the above source and pastes one file into another...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question