10

私の bottom_index.ejs は次のようになります。

<div>The bottom section</div>

私のコードでは、ejs を宣言します。

ejs = require('ejs');

次に、関数をコンパイルします。

var botom_index_ejs =
ejs.compile(fs.readFileSync(__dirname + "/../views/bottom_index.ejs", 'utf8'));

それを呼び出して、レンダリングされた html を取得します。

botom_index_ejs()

それはうまくいきます!

ここで、テンプレートを次のように変更したいと思います。

<div><%= bottom_text %></div>

パラメータ(bottom_text)をbottom_index.ejsに渡すことができます

どのようにパラメータを渡す必要がありますか?

ありがとう!

4

1 に答える 1

23

パラメータは、JS プレーン オブジェクトとして EJS テンプレートに渡されます。あなたの例では、次のようにする必要があります。

botom_index_ejs({ bottom_text : 'The bottom section' });

アップデート:

test.js

var fs = require('fs');
var ejs = require('ejs');
var compiled = ejs.compile(fs.readFileSync(__dirname + '/test.ejs', 'utf8'));
var html = compiled({ title : 'EJS', text : 'Hello, World!' });
console.log(html);

test.ejs

<html>
    <head>
        <title><%= title %></title>
    </head>
    <body>
        <p><%= text %></p>
    </body>
</html>
于 2013-03-04T14:48:14.233 に答える