How to compile pug (before jade) to php with gulp?

2

I am working on a project that is necessary to work with .php, I have always worked with .html, but now I have to work with .php, I work with pug before called jade and I compile it by means of tasks in gulp, but by default compiled in html, but I want to know if there is a way to pass it to php with gulp.

    
asked by MichaelCardoza 14.10.2016 в 06:40
source

2 answers

1

This problem presented itself to me when having the requirement of the programmer to use php declarations and thus to manage the sessions in the site without using sessionStorage, because we had problems with cache, and not wanting to separate the layout that it had in .pug.

The solution to this problem was very simple.

  • Check if .pug did not have conflicts with the tag, this I did in codepen.io

  • Then find a module in npm that can handle the extension change, the solution I found with this module: gulp-rename.

  • That already modifies my gulp task:

  • var // Modulos de desarrollo
      gulp = require('gulp'),
      pug = require('gulp-pug'),
      rename = require("gulp-rename");
    
    
    gulp.task('views', function() {
      gulp
        .src('./' + desarrollo + '/pug/*.pug', {
          base: './' + desarrollo + '/pug/'
        })
        // -------------------------------------
        .pipe(pug({
          locals: {},
          pretty: true
        }))
        // ------------------------------------- 
        .pipe(rename({
          extname: ".php"
        }))
        // ------------------------------------- 
        .pipe(gulp.dest('./'));
    });
    html
      head title php in my pug 
      body 
        main
        // Declaración php
        <?php echo 'I'm a php'?>
        h1 Hello #[br] #[small &lt;?php echo 'pug' ?&gt;]
        
    answered by 22.05.2017 в 16:28
    0

    You can use a npm package called gulp-jade-php in addition to pug

    jade-php

    Example of use:

    var jade = require('gulp-jade-php');
    
    gulp.task('templates', function() {
    gulp.src('./views/**/*.jade')
        .pipe(jade({
            locals: {
            title: 'OMG THIS IS THE TITLE'
            }
        }))
        .pipe(gulp.dest('./dist'));
    });
    
        
    answered by 14.10.2016 в 06:54