1

So I need the PHP include functionality for markup in NodeJS/Express. I searched around for a bit and stumbled across this question: Nodejs Include Other Views?

I need about the same thing but I don't really want to include/learn a full template engine like jade though and basically, I just need this specific functionality. Is there a cooler method than just read the split files by using fs.read and returning a concatenated string for express to deliver?

I'd preferably call the files in the html with some kind of placeholder than building them together in my JS. Also, if there is an easy way to do this with 'vanilla' nodejs functionality, I'd choose this over some module since I'm still learning.

Thanks in advance!

PHP

<?php include('header.php'); ?>
<body>
    //content
</body>
<?php include('footer.php'); ?>
Community
  • 1
  • 1
Tom M
  • 2,815
  • 2
  • 20
  • 47
  • 2
    When you do that with PHP, you're technically using PHP *as your templating language.* So the only way to get that kind of functionality is with a templating engine or writing your own way of inserting code which basically means you're writing your own templating engine. – Mike Cluck Apr 14 '16 at 22:46
  • So I'll have to, like, set placeholders and regex/replace those after reading the files? – Tom M Apr 14 '16 at 22:47
  • 2
    That'd be one way to do it, yes. – Mike Cluck Apr 14 '16 at 22:48
  • alright, thank you then. You don't sound too convinced of this idea though. Got a better approach? – Tom M Apr 14 '16 at 22:52
  • 1
    Personally, I prefer using a proper templating language but that's just my preference ;) Just make sure you don't get caught in the trap of [attempting to parse irregular languages using regular expressions.](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) But for a quick-and-dirty, simple code injection approach, regex with internal identifiers will work just fine. – Mike Cluck Apr 14 '16 at 22:54
  • 1
    I died at "The
    cannot hold it is too late.". Thanks for the lulz and your helpful response :)
    – Tom M Apr 14 '16 at 23:08
  • You might try changing your template engine to something with less learning curve like [handlebars-express](https://github.com/ericf/express-handlebars): simple to learn and you don't have to re-write all your existing HTML. – Michael Tallino Apr 14 '16 at 23:09
  • I am afraid, template is the easiest way. Try this...https://github.com/ericf/express-handlebars – FRizal Apr 14 '16 at 23:31

1 Answers1

1

Using ejs should bring it close to php. Its in the node stack and should be close. Here is the link to the github https://github.com/tj/ejs/blob/master/Readme.md