0

php.json

{
    "data" : "<?php echo $data; ?>"
}

home.jade

script.
                var data = !{data};

templates.js

gulp.task('templates-in-one-folder', () => {
  let data = dddata('ru');
  return gulp.src('app/**/*.jade')
    .pipe(plumber({errorHandler: errorHandler('Error in \'templates\' task')}))
    .pipe(cached('jade'))
    .pipe(gulpif(global.watch, inheritance({basedir: 'app'})))
    .pipe(filter(file => /app[\\\/]pages/.test(file.path)))
    .pipe(jade({basedir: 'app', data}))
    .pipe(prettify({
      braceStyle: 'expand',
      indentWithTabs: true,
      indentInnerHtml: true,
      preserveNewlines: true,
      endWithNewline: true,
      wrapLineLength: 120,
      maxPreserveNewlines: 50,
      wrapAttributesIndentSize: 1,
      unformatted: ['use']
    }))
    .pipe(staticHash({asset: 'dist', exts: ['js', 'css']}))
    .pipe(rename({dirname: '.'}))
    .pipe(gulp.dest('dist'))
});

My output

</div>
        <script>
            var data = < ? php echo $data; ? > ;

        </script>
    </body>

Between '<' '?' spaces are inserted and php code does not work out. How can I fix it? I try use htmlmin with 'ignoreCustomFragments' with regexp, but it is not works too...

Graham
  • 7,431
  • 18
  • 59
  • 84
  • as a general note: you should **never** mix php and js. They're different languages that get executed differently. Use AJAX or hidden elements with data-* tags to pass values between the client and server – treyBake Oct 23 '19 at 11:11
  • I wouldn’t be happy to mix in this way, it is not my code. I would just like to insert a crutch. Otherwise, when building, I get a non-working code. – Adam Balkoev Oct 23 '19 at 11:40

1 Answers1

0

There are some concerns here, as treyBake mentions, you should never mix PHP & JS.

While there are many ways to do this, you're first problem is that the .json file is not a .php file. Apache (or whatever webserver you're using) will not process this script, but rather just return plain text.

You may wish to try:

  • creating the json file from a php script, either on demand or via a scheculed event such as a cron.

  • using ajax (to run a php script that will create the json file) to load the data into the page

The point of SO is to point you in the right direction, rather than provide you with a complete solution.

I suggest you try using an ajax script, come back and tell us what happened. Then we can provide more guidance/assistance

Good luck

D