节点js模块和引擎 [英] Node js hbs module and engine

查看:100
本文介绍了节点js模块和引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的节点js,遇到了这个hbs模块,并在这部分代码中看到它:例如:

  app.set('view engine','html'); 
app.engine('html',require('hbs').__ express);

任何人都可以解释什么是hbs(句柄 - 但是它有什么作用)?
,为什么第二行是必要的,如果第一行已经说过这些文件将以html打开



谢谢!


hbs 是一个 express.js包装器,用于 handlebars.js javascript模板引擎 / em>的。 Handlebars.js 是一个模板引擎,可以使编写HTML代码变得更容易,如果感兴趣,可以查看这里。但 handlebars.js 用于客户端的复制(浏览器编译模板),因此您需要像 hbs 这样的包装器

>

包装器使得在express.js中使用客户端库成为可能,这就是 hbs 的作用。这有点简化,但它解释了原则。



在第二个问题中,为什么需要第二行。这是因为如果你使用标准的行:

  app.set('view engine','hbs'); 

express.js查找名为 hbs 视图引擎 / em>,但在你的例子中:

  app.set('view engine','html'); 
app.engine('html',require('hbs').__ express);

express.js dosent知道如何查找视图引擎定义为 html ,你必须在第二行定义这个视图引擎,所以express.js知道要查找什么。如果您看此处,您可以看到它说 Express加载它在内部


I'm new to node js and came across this hbs module and saw it in this part code for example :

app.set('view engine', 'html');
app.engine('html', require('hbs').__express);

can anyone please explain what is hbs (handlebars - but what does it do)? and why the second line is needed if the first already says that the files will be opened as html

Thank you!

解决方案

hbs is a express.js wrapper for the handlebars.js javascript template engine. Handlebars.js is a template engine to make writing html code easier, if intrested you can look here. But handlebars.js is meant for client-side copilation(the browser compiles the templates) so you need a wrapper like hbs.

A wrapper makes it possible to use for example a client-side library in express.js and that is what hbs does. This was a little simplify, but it explains the principle.

Over to your second question, why the second line is needed. and that is because if you use the standard line:

app.set('view engine', 'hbs');

express.js looks for the view engine named hbs, but in your example:

app.set('view engine', 'html');
app.engine('html', require('hbs').__express);

express.js dosent know what to look for in case of the view engine defined as html and you have to define this view engine in the second line, so express.js knows what to look for. If you look here, you can see that it says, Express loads it internally.

这篇关于节点js模块和引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆