如何在Node.js中使用模板(把手或任何替代方案)而不使用框架(ex = express)?

时间:2023-01-21 19:09:26

For example, I have this JSON document "foo.json":

例如,我有这个JSON文档“foo.json”:

{
    "foo": [
        {
            "bar": "Hello World!"
        },
        {
            "bar": "The End"
        }
    ]
}

In Node.js, I would like to use templating (handlebars or any) to generate a string from the JSON document, such as:

在Node.js中,我想使用模板(把手或任何)来从JSON文档生成一个字符串,例如:

<p>Hello World!</p><p>The End</p>

... And then assign that string value to a variable in Node.js. Finally, I'll concatenate more values to the variable and output the final variable value as an html document.

...然后将该字符串值分配给Node.js中的变量。最后,我将更多值连接到变量,并将最终变量值输出为html文档。

Can this be done without using a framework like Express?

这可以在不使用像Express这样的框架的情况下完成吗?

1 个解决方案

#1


30  

If you want to use handlebars, just grab the npm module:

如果你想使用把手,只需抓住npm模块:

npm install handlebars

Then in your script, you can use handlebars to render your output based on a simple template that iterates over the array foo and creates a <p> for each item, containing the text of the bar property:

然后在您的脚本中,您可以使用把手基于一个简单的模板来渲染输出,该模板迭代数组foo并为每个项创建一个

,其中包含bar属性的文本:

var handlebars = require('handlebars');

// get your data into a variable
var fooJson = require('foo.json');

// set up your handlebars template
var source = '{{#each foo}}<p>{{this.bar}}</p>{{/each}}';

// compile the template
var template = handlebars.compile(source);

// call template as a function, passing in your data as the context
var outputString = template(fooJson);

--EDIT--

- 编辑 -

If you want to use a .hbs template file instead of a string source you can use the fs module to read the file with fs.readFile, call toString() on the returned buffer, and use that to call a rendering function. Try this:

如果要使用.hbs模板文件而不是字符串源,可以使用fs模块使用fs.readFile读取文件,在返回的缓冲区上调用toString(),并使用它来调用渲染函数。尝试这个:

var handlebars = require('handlebars');
var fs = require('fs');

// get your data into a variable
var fooJson = require('path/to/foo.json');

// read the file and use the callback to render
fs.readFile('path/to/source.hbs', function(err, data){
  if (!err) {
    // make the buffer into a string
    var source = data.toString();
    // call the render function
    renderToString(source, fooJson);
  } else {
    // handle file read error
  }
});

// this will be called after the file is read
function renderToString(source, data) {
  var template = handlebars.compile(source);
  var outputString = template(data);
  return outputString;
}

#1


30  

If you want to use handlebars, just grab the npm module:

如果你想使用把手,只需抓住npm模块:

npm install handlebars

Then in your script, you can use handlebars to render your output based on a simple template that iterates over the array foo and creates a <p> for each item, containing the text of the bar property:

然后在您的脚本中,您可以使用把手基于一个简单的模板来渲染输出,该模板迭代数组foo并为每个项创建一个

,其中包含bar属性的文本:

var handlebars = require('handlebars');

// get your data into a variable
var fooJson = require('foo.json');

// set up your handlebars template
var source = '{{#each foo}}<p>{{this.bar}}</p>{{/each}}';

// compile the template
var template = handlebars.compile(source);

// call template as a function, passing in your data as the context
var outputString = template(fooJson);

--EDIT--

- 编辑 -

If you want to use a .hbs template file instead of a string source you can use the fs module to read the file with fs.readFile, call toString() on the returned buffer, and use that to call a rendering function. Try this:

如果要使用.hbs模板文件而不是字符串源,可以使用fs模块使用fs.readFile读取文件,在返回的缓冲区上调用toString(),并使用它来调用渲染函数。尝试这个:

var handlebars = require('handlebars');
var fs = require('fs');

// get your data into a variable
var fooJson = require('path/to/foo.json');

// read the file and use the callback to render
fs.readFile('path/to/source.hbs', function(err, data){
  if (!err) {
    // make the buffer into a string
    var source = data.toString();
    // call the render function
    renderToString(source, fooJson);
  } else {
    // handle file read error
  }
});

// this will be called after the file is read
function renderToString(source, data) {
  var template = handlebars.compile(source);
  var outputString = template(data);
  return outputString;
}