node.js Jade - 从不同目录加载模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11675453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:05:26  来源:igfitidea点击:

Jade - Loading templates from different directories

templatesnode.jsexpresspug

提问by StevenNunez

I'm trying to work on Peepcode's Node.js Full Stack videos and it seems they're using an older version of express/jade. No mention of using block/extends to render layouts.

我正在尝试处理 Peepcode 的 Node.js Full Stack 视频,而且他们似乎使用的是较旧版本的 express/jade。没有提到使用块/扩展来渲染布局。

The setup used in the app is to have a /views/layout.jade file that loads for all sub-apps. The sub-apps' views are located in /apps//views.

应用程序中使用的设置是具有为所有子应用程序加载的 /views/layout.jade 文件。子应用程序的视图位于 /apps//views 中。

My server.js seems pretty standard. Express is version 3.0.0rc1

我的 server.js 看起来很标准。Express 是 3.0.0rc1 版本

require('coffee-script');

var express = require('express')
  , http = require('http')
  , path = require('path');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

require('./apps/authentication/routes')(app)

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

My routes file for the sub-app is in /apps/authentication/routes

我的子应用程序的路由文件在 /apps/authentication/routes 中

routes.coffee

路线.咖啡

routes = (app) ->

  app.get "/login", (req,res) ->
    res.render "#{__dirname}/views/login",
      title: "Login"
      stylesheet: 'login'

module.exports = routes

The view I intend on rendering for this.

我打算为此渲染的视图。

login.jade

登录.jade

extends layout

block content
  form(action='/sessions', method='post')
    label
      | Username
      input(type='text', name='user')
    label
      | Password 
      input(type='password', name='password)
    input(type='submit', name='Submit')

And Finally the layout.

最后是布局。

doctype 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/#{stylesheet}.css')
  body
    block content

Going to localhost:3000/login renders this:

转到 localhost:3000/login 会显示以下内容:

Express 500 Error: /Users/StevenNunez/code/HotPie/apps/authentication/views/login.jade:1 > 1| extends layout 2| 3| block content 4| form(action='/sessions', method='post') ENOENT, no such file or directory '/Users/StevenNunez/code/HotPie/apps/authentication/views/layout.jade'

Express 500 错误:/Users/StevenNunez/code/HotPie/apps/authentication/views/login.jade:1 > 1| 扩展布局 2| 3| 块内容 4| form(action='/sessions', method='post') ENOENT,没有这样的文件或目录'/Users/StevenNunez/code/HotPie/apps/authentication/views/layout.jade'

My folder structure:

我的文件夹结构:

.
├── '
├── apps
│?? └── authentication
│??     ├── routes.coffee
│??     └── views
│??         └── login.jade
├── package.json
├── public
├── server.js
└── views
    ├── index.jade
    └── layout.jade

Thank you for your time.

感谢您的时间。

采纳答案by StevenNunez

It looks like I had to just give the relative path in the extendscall.

看起来我只需要在extends调用中给出相对路径。

extends ../../../views/layout

block content
  form(action='/sessions', method='post')
    label
      | Username
      input(type='text', name='user')
    label
      | Password
      input(type='password', name='password')
    input(type='submit', name='Submit')

I didn't have to set app.set('view options',{layout:false});

我不必设置 app.set('view options',{layout:false});

回答by IvanM

You can use variable __dirnameto connect views from other directories.

您可以使用变量__dirname连接来自其他目录的视图。

Example:

例子:

app.get('/otherurl/' , function(req, res){ 
  res.render(__dirname + '/../other_project/views/index') 
});

回答by andrescabana86

You are trying to use two methods of rendering... The first layout that extends the layout and the second is block content. When using a layout you are extending automatically the layout file in the folder of login.jade.

您正在尝试使用两种渲染方法...第一个布局扩展布局,第二个是块内容。使用布局时,您会自动扩展 .zip 文件夹中的布局文件login.jade

It isn't necessary to use extendsand you can't use blocksentence, but if you want to use extendsyou have to disable layout.

没有必要使用extends,也不能使用block句子,但如果要使用extends,则必须禁用布局。

Add this to the app:

将此添加到应用程序:

app.set('view options',{layout:false});

after

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

and extend the files like layout extends:

并扩展文件,如布局扩展:

layout.jade

layout.jade

doctype 5
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/#{stylesheet}.css')
  body
    block content

login.jade

登录.jade

extends layout

block content
  form(action='/sessions', method='post')
    label
      | Username
      input(type='text', name='user')
    label
      | Password 
      input(type='password', name='password)
    input(type='submit', name='Submit')

The login file has to be in the same dir. If you want to call other layout you can use the dir like this:

登录文件必须在同一个目录中。如果你想调用其他布局,你可以像这样使用目录:

appDirectory
  views
    layout
  otherViews
    login

login.jade

登录.jade

extends ../views/layout    
block content
  form(action='/sessions', method='post')
    label
      | Username
      input(type='text', name='user')
    label
      | Password 
      input(type='password', name='password)
    input(type='submit', name='Submit')