如何在Express框架中使用nunjucks-没有玉 [英] How to use HTML in Express framework with nunjucks- no jade

查看:157
本文介绍了如何在Express框架中使用nunjucks-没有玉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用sendFile方法在Express项目中呈现Html。
我想使用部分与我的项目。而且,不要切换到玉石。

I have been using sendFile method to render Html in Express project. I would like to use partials with my project. And, not switch to jade.

有没有办法在Express 3.x中使用传统的HTML与partials。
我已经尝试过ejs,但是完全不了解它。

Is there a way to use traditional HTML with partials in Express 3.x. I have tried ejs, but dont understand it completely.

推荐答案

更多HTML样的模板引擎将是 nunjucks (其语法类似于Jinja2,您有经验)。

A more 'HTML-like' templating engine would be nunjucks (whose syntax is similar to Jinja2, which you have experience with).

这是一个简单的设置。这假定Express和Nunjucks都已安装,如果没有安装:

Here's a simple setup. This assumes both Express and Nunjucks are installed, if not:

npm install express
npm install nunjucks

- app.js

var nunjucks  = require('nunjucks');
var express   = require('express');
var app       = express();

app.listen(3012);

nunjucks.configure('views', {
  autoescape: true,
  express   : app
});

app.get('/', function(req, res) {
  res.render('index.html', {
    title : 'My First Nunjucks Page',
    items : [
      { name : 'item #1' },
      { name : 'item #2' },
      { name : 'item #3' },
      { name : 'item #4' },
    ]
  });
});

- views / index.html

<!doctype html>
<html>
  <head>
    <title>welcome to {{ title }}</title>
  </head>
  <body>
    <ul>
      {% for item in items %}
        {% include "item.html" %}
      {% endfor %}
    </ul>
  </body>
</html>

- views / item.html

<li>{{ item.name }}</li>

这篇关于如何在Express框架中使用nunjucks-没有玉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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