Węzeł.js-EJS-w tym częściowy

Próbuję użyć wbudowanego renderera Javascript dla węzła: https://github.com/visionmedia/ejs

Chciałbym wiedzieć, jak mogę dołączyć inny plik widoku (częściowy) wewnątrz a .plik widoku ejs.

 49
Author: jeffreyveon, 2011-03-23

5 answers

Z Express 3.0:

<%- include myview.ejs %>

Ścieżka jest względna od wywołującego, który zawiera plik, a nie od katalogu views ustawionego przez app.set("views", "path/to/views").

EJS zawiera

 99
Author: pkyeck,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-01 07:31:20

W Expressie {[5] } użyłem następującego do załadowania ejs:

  var path = require('path');

  // Set the default templating engine to ejs
  app.set('view engine', 'ejs');
  app.set('views', path.join(__dirname, 'views'));

  // The views/index.ejs exists in the app directory
  app.get('/hello', function (req, res) {
    res.render('index', {title: 'title'});
  });

Wtedy potrzebujesz tylko dwóch plików, aby to działało - views/index.ejs:

<%- include partials/navigation.ejs %>

I views/partials/navigation.ejs:

<ul><li class="active">...</li>...</ul>

Możesz również powiedzieć Express, aby używał ejs dla szablonów html:

var path = require('path');
var EJS  = require('ejs');

app.engine('html', EJS.renderFile);

// Set the default templating engine to ejs
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));

// The views/index.html exists in the app directory
app.get('/hello', function (req, res) {
  res.render('index.html', {title: 'title'});
});

Wreszcie możesz również użyć modułu ejs layout:

var EJSLayout = require('express-ejs-layouts');
app.use(EJSLayout);

To użyje views/layout.ejs jako układu.

 12
Author: czerasz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-19 12:26:50

Od Express 4.x

App.js

// above is all your node requires

// view engine setup
app.set('views', path.join(__dirname, 'views')); <-- ./views has all your .ejs files
app.set('view engine', 'ejs');

Błąd.ejs

<!-- because ejs knows your root directory for views, you can navigate to the ./base directory and select the header.ejs file and include it -->

<% include ./base/header %> 
<h1> Other mark up here </h1>
<% include ./base/footer %>
 8
Author: Daniel Ram,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-11 22:36:45

Express 3.x nie obsługuje już częściowego. Zgodnie z postem ejs 'partial is not defined' , możesz użyć słowa kluczowego "include" W EJS, aby zastąpić usuniętą częściową funkcjonalność.

 3
Author: VCD,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:33:26

EJS sam w sobie obecnie nie pozwala na wyświetlanie częściowych. Express tak.

 -3
Author: jeffreyveon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-11 03:40:22