如何在一个节点中使用ejs和jade快递项目? [英] how to use both ejs and jade in one nodejs & express project?

查看:119
本文介绍了如何在一个节点中使用ejs和jade快递项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ejs 部分,并使用 jade 单个页面,如何在一个nodejs&快递项目?

I want to use ejs for partials and use jade for individual pages, how to use both in one nodejs & express project?

推荐答案

很容易找到 expressjs api docs mergeate.js github

It 's easy to find the way in expressjs api docs and consolidate.js at github

参考下面的express.js文档片段,请

Reference the express.js doc fragment below, please

app.engine(ext,callback)

将给定的模板引擎回调注册为ext默认情况下将根据文件扩展名来要求()引擎。例如,如果您尝试呈现foo.jade文件,Express将在内部调用以下内容,并在随后的调用中缓存require()以提高性能。

Register the given template engine callback as ext By default will require() the engine based on the file extension. For example if you try to render a "foo.jade" file Express will invoke the following internally, and cache the require() on subsequent calls to increase performance.

app.engine('jade', require('jade').__express);

对于不提供.__表示开箱即用的引擎,或者如果您希望映射您可以使用此方法对模板引擎的不同扩展名。例如将EJS模板引擎映射到.html文件:

For engines that do not provide .__express out of the box - or if you wish to "map" a different extension to the template engine you may use this method. For example mapping the EJS template engine to ".html" files:

app.engine('html', require('ejs').renderFile);

在这种情况下,EJS提供与Express所期望的相同签名的.renderFile()方法:(path ,选项,回调),虽然注意到它将此方法作为ejs .__在内部进行别名,因此如果您使用.ejs扩展名,则不需要执行任何操作。

In this case EJS provides a .renderFile() method with the same signature that Express expects: (path, options, callback), though note that it aliases this method as ejs.__express internally so if you're using ".ejs" extensions you dont need to do anything.

一些模板引擎不遵循这个惯例,已经创建了mergeate.js库来映射所有节点的流行模板引擎,以遵循这个约定,从而允许他们在Express中无缝工作。

Some template engines do not follow this convention, the consolidate.js library was created to map all of node's popular template engines to follow this convention, thus allowing them to work seemlessly within Express.

var engines = require('consolidate');
app.engine('haml', engines.haml);
app.engine('html', engines.hogan);

这篇关于如何在一个节点中使用ejs和jade快递项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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