简体   繁体   English

Node.js 没有模板引擎

[英]Node.js without a template engine

I'm new into Node.js and trying to learn.我是 Node.js 的新手,正在努力学习。 From what I have understood it's common to use a template engine (eg. Jade), even for CSS (eg. Stylus).据我了解,使用模板引擎(例如 Jade)是很常见的,即使对于 CSS(例如 Stylus)也是如此。 To be honest, all the tutorials I have seen out there involve a template engine when it comes to the layout.老实说,我看到的所有教程都涉及到模板引擎的布局。

The thing is that I don't want to use a template engine because I think it's unnecessarily complex.问题是我不想使用模板引擎,因为我认为它不必要地复杂。

Here is a link to a boilerplate for Node (unfortunately it doesn't work by some reason), but Option 1 shouldn't be using any template engine for the layout.是指向 Node 样板的链接(不幸的是,由于某种原因它不起作用),但选项 1 不应使用任何模板引擎进行布局。

So, what is the easiest way to combine Node.js and Mongodb with "normal" HTML(5)?那么,将 Node.js 和 Mongodb 与“普通”HTML(5) 结合起来的最简单方法是什么? Is it possible for example to use HMTL5 Boilerplate with Node?例如,是否可以将 HMTL5 Boilerplate 与 Node 一起使用?

If you are using static html, so you wont need templating on the server side.如果您使用的是 static html,那么您不需要在服务器端进行模板化。 You can easily serve your html files with Express/Connect static middleware , for example:您可以使用Express/Connect static 中间件轻松地为您的 html 文件提供服务,例如:

app.use(express.static(__dirname + '/public'));

then put an index.html to your public folder.然后将index.html放入您的公用文件夹。

Also I think you can copy and paste the whole h5bp to your public folder and it should work.此外,我认为您可以将整个 h5bp 复制并粘贴到您的公共文件夹中,它应该可以工作。

Here are my thoughts on this.这是我对此的看法。

If you are serving only static html, it's obvious that you don't need any template engine, since you can just buffer the html in the response, or use the Connect static middleware.如果您只提供static html,很明显您不需要任何模板引擎,因为您可以在响应中缓冲 html,或者使用Connect static 中间件。

However things get interesting when you have to deal with dynamic content.然而,当您必须处理动态内容时,事情就会变得有趣起来。

This is where template engines are good at, since they provide ways to integrate your data with the html. If you are going to replace the template engine, you need a library that can do HTML and DOM manipulation.这是模板引擎擅长的地方,因为它们提供了将您的数据与 html 集成的方法。如果您要替换模板引擎,您需要一个可以执行 HTML 和 DOM 操作的库。 I can think of two alternatives:我可以想到两种选择:

  • jsdom , and libraries that are build on it (such as fill.js ). jsdom以及基于它构建的库(例如fill.js )。
    With jsdom you can use server-side jQuery to build your views, or even YUI .使用 jsdom,您可以使用服务器端 jQuery 来构建您的视图,甚至是YUI
    However it has some drawbacks:但是它有一些缺点:
    • it's slow and cumbersome又慢又麻烦
    • it's a pain to install on Windows since it depends on native modules在 Windows 上安装很痛苦,因为它依赖于本机模块
    • i couldn't get it to parse html fragments or incomplete html (maybe someone knows a way around this)我无法让它解析 html 片段或不完整的 html(也许有人知道解决这个问题的方法)

  • The second alternative would be to use some lightweight libraries that handle html, without the full DOM.第二种选择是使用一些轻量级库来处理 html,但没有完整的 DOM。 So far I've found two libs that are good at this:到目前为止,我发现了两个擅长于此的库:
    • cheerio - a small library that relies on jQuery -like selectors cheerio - 一个依赖于 jQuery 类选择器的小型库
    • plates - a library that binds data to markup plates - 一个将数据绑定到标记的库

Both are very neat in my opinion, and a good starting point in getting rid of templates:)在我看来,两者都非常简洁,并且是摆脱模板的良好起点:)
There may be others that I'm not aware of, but you get the idea.可能还有其他我不知道的,但你明白了。

Using express, you would just send the html5 in the response:使用 express,您只需在响应中发送 html5:

app.get('/', function(req, res){
  res.send('<header>Hello World</header>');
});

However, I would say that in most cases a templating engine doesn't add complexity.但是,我会说在大多数情况下模板引擎不会增加复杂性。 The separation of concerns makes things simpler if you are rendering dynamic content.如果您要呈现动态内容,关注点分离会使事情变得更简单。

First time answering my own question.第一次回答我自己的问题。 I just want to share that I found a converter from html to jade (template engine).我只想分享一下,我找到了一个从 html 到 jade(模板引擎)的转换器。 This is definitely a good thing that's removing a lot of complexity at least for me, even if it still involves a template engine.这绝对是一件好事,至少对我来说,它消除了很多复杂性,即使它仍然涉及模板引擎。

http://html2jade.aaron-powell.com/ http://html2jade.aaron-powell.com/

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

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