86

ノードにEmbeddedJavascriptレンダラーを使用しようとしています: https ://github.com/visionmedia/ejs

.ejsビューファイル内に別のビューファイル(部分的)を含める方法を知りたいのですが。

4

12 に答える 12

172

Express 3.0の場合:

<%- include myview.ejs %>

パスは、で設定されたビューディレクトリからではなく、ファイルを含む呼び出し元からの相対パスですapp.set("views", "path/to/views")

EJSv1に含まれる
ものEJSv2に含まれるもの

(更新:ejs v3.0.1の最新の構文は<%- include('myview.ejs') %>

于 2011-07-07T10:24:24.110 に答える
22

Express 4.xで動作します:

これに従ってテンプレートにパーシャルを含める正しい方法は、次のとおりです。

<%- include('partials/youFileName.ejs') %>

使用しているもの:

<% include partials/yourFileName.ejs %>

これは非推奨です。

于 2020-04-06T17:23:11.930 に答える
21

Express4.xでは、以下を使用してロードしました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'});
  });

次に、それを機能させるために2つのファイルが必要です- views/index.ejs

<%- include partials/navigation.ejs %>

そしてviews/partials/navigation.ejs

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

Expressにejshtmlテンプレートに使用するように指示することもできます。

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'});
});

最後に、ejsレイアウトモジュールを使用することもできます。

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

これは、views/layout.ejsをレイアウトとして使用します。

于 2015-09-19T12:26:50.883 に答える
10

Express4.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');

error.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 %>
于 2016-01-11T22:36:45.910 に答える
4

Express3.xはパーシャルをサポートしなくなりました。post ejs'partial is not defined'によると、EJSで "include"キーワードを使用して、削除された部分機能を置き換えることができます。

于 2012-12-05T02:59:09.640 に答える
3

app.js追加

app.set('view engine','ejs')

ビュー/部分ファイルに部分ファイル(ejs)を追加します

index.ejs

<%- include('partials/header.ejs') %>
于 2020-06-15T06:15:13.467 に答える
2

公式ドキュメントhttps://github.com/mde/ejs#includesshowには、次のような作品が含まれています。

<%- include('../partials/head') %>
于 2020-01-29T17:11:58.067 に答える
1

また

<%- include('partials/header.ejs',{paramName: paramValue}) %>
于 2021-06-23T23:52:47.827 に答える
0

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">

 <form method="post" class="mt-3">
        <div class="form-group col-md-4">
          <input type="text" class="form-control form-control-lg" id="plantName" name="plantName" placeholder="plantName">
        </div>
        <div class="form-group col-md-4">
          <input type="text" class="form-control form-control-lg" id="price" name="price" placeholder="price">
        </div>
        <div class="form-group col-md-4">
            <input type="text" class="form-control form-control-lg" id="harvestTime" name="harvestTime" placeholder="time to harvest">
          </div>
        <button type="submit" class="btn btn-primary btn-lg col-md-4">Submit</button>
      </form>

<form method="post">
        <table class="table table-striped table-responsive-md">
            <thead>
            <tr>
                <th scope="col">Id</th>
                <th scope="col">FarmName</th>
                <th scope="col">Player Name</th>
                <th scope="col">Birthday Date</th>
                <th scope="col">Money</th>
                <th scope="col">Day Played</th>
                <th scope="col">Actions</th>
            </tr>
            </thead>
            <tbody>
            <%for (let i = 0; i < farms.length; i++) {%>
                 <tr>
                    <td><%= farms[i]['id'] %></td>
                    <td><%= farms[i]['farmName'] %></td>
                    <td><%= farms[i]['playerName'] %></td>
                    <td><%= farms[i]['birthDayDate'] %></td>
                    <td><%= farms[i]['money'] %></td>
                    <td><%= farms[i]['dayPlayed'] %></td>
                    <td><a href="<%=`/farms/${farms[i]['id']}`%>">Look at Farm</a></td>
                </tr>
            <%}%>
        </table>
    </form>

于 2019-12-18T14:16:58.437 に答える
0

ejsファイルを含めるには、以下を使用する必要があります。

<%- include("file-name") %>

注:.ejsファイル名に使用する必要はありません。

于 2021-09-25T12:26:22.717 に答える
-1

ejsファイルを含めるには、以下を使用する必要があります。

<%- include("relative path of ejs ") -%>

于 2021-06-21T05:35:21.370 に答える
-4

EJS自体は、現在、ビューパーシャルを許可していません。Expressはそうします。

于 2011-05-11T03:40:22.697 に答える