2

I can't seem to figure out how to include a file using Node.js. All of the other questions seem to discuss including other JS files, but I'm looking for the equivalent of a PHP include (files that can contain HTML content).

Usually in PHP, my view files like this:

...stuff

<?php include 'header.php'; ?>
<?php include 'nav.php'; ?>

... more stuff

<?php include 'footer.php'; ?>

And for example in nav.php I have:

<nav role=navigation>

    <ul>
        <!-- links -->
    </ul>

</nav>

Is there similar functionality in Node or do I have to change my way of developing web applications? I am using the Express framework if that helps.

A short, working example would be highly appreciated.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156

4 Answers4

2

No, that isn't the way nodeJS is working. If you like to compare, compare nodejs to ruby or ASP.NET. There is jade, as dystroy said, but if you are familiar with HTML syntax prefer ectjs for example :

stuff...
<div id="content">stuff</div>
<% include 'footer.ect' %>

Of course, if there is code to include use require

Zulu
  • 8,765
  • 9
  • 49
  • 56
Vinz243
  • 9,654
  • 10
  • 42
  • 86
1

There's nothing similar directly into nodejs.

But many people using nodejs also use a framework like express and template engines.

For example Jade lets you do includes :

index.jade file :

doctype html
html
  include includes/head
body
  h1 My Site
  p Welcome to my super lame site.
  include includes/foot

includes/head.jade file :

head
  title My Site
  script(src='/javascripts/jquery.js')
  script(src='/javascripts/app.js')

includes/foot.jade file :

#footer
  p Copyright (c) foobar

It should be noted that in the age of ajax, single page applications and javascript, template includes are much rarely needed that in the old times of PHP. You might want to first play a little with templates (for example using Jade) and only after decide if you need includes.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

You can user vash engine. Take for example this layout page:

<body>
    <!-- Body content -->
    <div>@html.block("body")</div>


    <!-- Render Page Specific Scripts Here -->
    @html.block("scripts")
</body>

Now in your other views, you can do smth like this:

@html.extend("layout", function(model){

     <!-- main content of the page -->
     @html.block("body",function(model){
         <h1>This will be the login page</h1>
     })

     <!-- page specific scripts -->
     @html.block("scripts",function(model){
          <scrip></script>
     })
 })

Hope this helps!

marinvirdol
  • 1,263
  • 1
  • 12
  • 18
0

let's assume we have to include 2 files inside index.html

the simplest way

html index.html

<%include filename.extension%>
<%include anotherfile.html%>

node.js

var temp = require('templatesjs');

// ... ... ...

fs.readFile("./index.html"){
    if(err) throw err;
    var output = temp.set(data);
    res.write(output);
    res.end();
});

those two file will get included inside index.html automatically when you use set()

i have used module templatesjs

$ npm install templatesjs

a good documentation here
github : templatesjs