简体   繁体   English

为什么模板包含文件不起作用

[英]Why templates include file doesn't work

I'm learning how to use templatesjs from: https://www.npmjs.com/package/templatesjs 我正在从以下网址学习如何使用templatejs: https//www.npmjs.com/package/templatesjs

They have an example of using include html file in other html file (using <%include%> tag) 他们有一个在其他html文件中使用include html文件的示例(使用<%include%>标记)

When I'm trying to build my own example, it doesn't work (The screen is empty, with no errors): 当我尝试构建自己的示例时,它不起作用(屏幕为空,没有错误):

var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
app.use(bodyParser.json())
var templatesjs = require('templatesjs');

// FILES
var MAIN_FILE = '/main.html';


/* 
 *  Home page
 */
app.get('/', function (req, res) {

   fs.readFile(__dirname + MAIN_FILE, function(err,data){   
        if(err) throw err;

        templatesjs.set(data, function(err,data){
            if(err) throw err;
            res.send();               
        });
    });  

})


/* 
 *  Startup
 */
var server = app.listen(8082, function () {
   var host = server.address().address
   var port = server.address().port

   // start
   console.log("App listening at http://%s:%s", host, port)
})

the main html.file looks: html.file的主要外观如下:

<html>
<title> Tutorial -> Templates Js Server </title>
<head>

</head>
<body>

    <div> 
        <%include Top.html%>
    </div>
    <div> 
    </div>
</body>
</html>

and Top.html file looks: 并且Top.html文件的外观如下:

<p>TOP</p>

(I have tried to add <html> tag into Top.html, but same results); (我试图将<html>标记添加到Top.html中,但结果相同);

The problem is that the web screen I'm getting is empty (with no errors at Node.js) 问题是我得到的网页屏幕是空的(Node.js上没有错误)

What am I doing wrong ? 我究竟做错了什么 ?

It's because you doesn't send back any data to incoming request! 这是因为您没有将任何数据发送回传入请求! your res.send() is empty. 您的res.send()为空。 you should send something back if you really want to show it. 如果您确实想展示它,则应该将其发送回去。 for example: res.send('hello world') . 例如: res.send('hello world')

If you want to render your template with your data, you could use templatesjs.renderAll() method to populate your html template with desired data as follows: 如果要用数据呈现模板,则可以使用templatesjs.renderAll()方法用所需数据填充html模板,如下所示:

// set default directory for html partials
templatesjs.dir = "./public/partials/";

app.get('/', function(req, res) {

  fs.readFile(__dirname + MAIN_FILE, function(err, data) {
    if (err) throw err;


    templatesjs.set(data, function(err, data) {
      if (err) throw err;

      var list = { // this is your data
        name: 'your name'
      }; 

      templatesjs.renderAll(list, function(err, data) {
        if (err) throw err;
        res.send(data);
      });
    });
  });

})

Top.html: 的top.html:

<p>Hello, my name is <%name%></p>

and this file should reside in ./public/partials/ directory as we set default include directory to this path; 该文件应位于./public/partials/目录中,因为我们将此路径设置为默认包含目录;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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