简体   繁体   中英

How can I have my index.htm file include other html files

using node.

Here is how I serve one from my server:

app.get('/', function(req, res) {
    res.sendfile('./views/index.html');
});

With in this index.html file I have two files I need served:

head.htm and body.htm .

In PHP I would just use includes. How is this done in Node?

There's many solutions to the situation here... it comes down to personal preference on which tool you gravitate towards.

One such tool that I have used is EJS. You can read all about it here:

https://code.google.com/archive/p/embeddedjavascript/wikis/Templates.wiki

Edit: An example of such would be having a header and footer template, with an index.ejs page that includes them. (Although you can use include these files at any point in the index page that gets rendered).

Index.ejs (ejs is just the extension used, it's the same as html with rendering tags inside of it):

<% include templates/header %>
<h1> Index page!</h1>
<% include templates/footer %>

Header.ejs:

<html>
  <head>
  </head>
  <body>

Footer.ejs:

  </body>
</html>

Inside routes configuration:

app.get("/", function(req, res){
  res.render("index");
}

There's obviously configuration requirements that you will need to do, I'm also assuming you're using express, which EJS works pretty easily with.

Pick a template library , any template library. I've had success with nunjucks .

Then you can do something like:

var nunjucks = require("nunjunks");

var app = express();

nunjucks.configure('views', {
    autoescape: true,
    express: app
});

app.get('/', function(req, res) {
    res.render('index.html');
});

and in index.html :

{% include "item.html" %}

It would help if you mentioned the templating engine you are using. By default, it should be Pug(or Jade) (if you used the express generator I think).

For Jade:

app.js:

var express = require('express');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// define routes
app.get('/', function(req, res) {
res.render('index.html');
});

Now, by default the views folder will be used to serve files. A good practice is to create a master layout that defines the general structure of your HTML files, and then add the specific contents in the extended files.

master.pug:

doctype html
html
  head
    title
        block title // title block

    link(rel='stylesheet', href='/stylesheets/default.css') //some default styles
    block styles // block for more styles

  body
    include header.pug //include header file

    block content // block to insert contents

    script(type='text/javascript',src='/javascripts/faculty-index.js') // default scripts


    block scripts // block to insert scripts

    include footer.pug // include footer file

Block defines a space where you can enter your content once you extend the file. Include basically just includes the code from the file in that space. Now you're index.pug can be something like this

index.pug

extends master.pug // extend the base template

block title
    | Index Page // adds the content in the title block

block styles
    link(rel='stylesheet', href='/stylesheets/index.css') // specific styles for index

block content
    h1 This is the index  // adds the index content which goes in the body tag where content is defined

Here the index file uses everything from the master file, and adds its own content in title, body and styles.

Look at the pug documentation for more

Similar behavior can be replicated with any templating engine. Another one I've used is Handlebars which I like more because its syntax feels more like writing html. But, you'll have to set it up first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM