0

I recently switched to Node/Express, and I have a PHP file that was made before this decision. Is it possible to use two different view engines? I tried including php-express, but I get a failed to lookup view error.

I figured it was because I can't have these lines consecutively, because express would just overwrite one...

app.set('view engine', 'php');

app.set('view engine', 'pug');

I tried setting the view engine just before the GET request instead like this:

 app.get('/avail', function(req, res, next){
   app.set('view engine', 'php');
   res.render('availability.php');
 })

However, this just gave me this error:

Error: Command failed: php H:\Node\node_modules\php-express\lib\PHPExpress/../../page_runner.php H:\Node\views H:\Node\views\availability.php 'php' is not recognized as an internal or external command, operable program or batch file.

My only conclusion is that I just have to rewrite the file with pug, but I wanted to avoid it if there were a way to just render this php file.

Jan Sršeň
  • 1,045
  • 3
  • 23
  • 46
user3768258
  • 125
  • 1
  • 10
  • NEVER set your view engine in a request handler – Robert Mennell Sep 20 '18 at 16:02
  • can you explain why? (I'm just curious b/c I've read the documentation and watched/read several tutorials and nobody mentions anything). so is my only soln to rewrite/translate the php to pug? – user3768258 Sep 20 '18 at 16:04
  • Because setting an app level setting in an asynchronous way will lead to an inconsistent state. But no it's not your only choice. You instead need to manually call the rendering methods and pipe the result to the client – Robert Mennell Sep 20 '18 at 16:05
  • sorry but can you be more specific on how to "manually call the rendering methods and pipe the result to the client"? I just tried to search for this and got nothing relevant – user3768258 Sep 20 '18 at 16:12
  • https://pugjs.org/api/reference.html https://github.com/fnobi/php-express/blob/master/lib/PHPExpress/engine.js Those two links should tell you how you can manually render using those engines. Wrapping those in a DRY module and importing it into routers or routes that need to render and using it there should make them single use and setup, and consistent. Then you just tell it when and where to render and send that result to the res.send function. – Robert Mennell Sep 20 '18 at 16:34
  • another option is explicit extensions: https://stackoverflow.com/questions/15063262/is-there-any-way-to-use-multiple-view-engines-with-express-node-js https://expressjs.com/en/advanced/developing-template-engines.html You could easily create your own wrapper that will check extension and default if not found. Another option is the consolidate package in the answer. Have fun! – Robert Mennell Sep 20 '18 at 16:35
  • I saw that SO answer earlier and it doesn't seem as if PHP is an option for consolidate? Re the pug documentation, it looks as if it's only for .pug files? – user3768258 Sep 20 '18 at 21:00

0 Answers0