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


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

2 answers


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

  • 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() {
        .src('./' + desarrollo + '/pug/*.pug', {
          base: './' + desarrollo + '/pug/'
        // -------------------------------------
          locals: {},
          pretty: true
        // ------------------------------------- 
          extname: ".php"
        // ------------------------------------- 
      head title php in my pug 
        // 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

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


    Example of use:

    var jade = require('gulp-jade-php');
    gulp.task('templates', function() {
            locals: {
            title: 'OMG THIS IS THE TITLE'
    answered by 14.10.2016 в 06:54