How can I pass variable to ejs.compile

前端 未结 1 524
南旧
南旧 2021-02-08 13:20

My bottom_index.ejs looks like that:

The bottom section

In my code I declare ejs:

ejs = require(\'ejs\')         


        
1条回答
  •  后悔当初
    2021-02-08 14:10

    Parameters are passed to EJS template as a JS plain object. For your example it sholud be:

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

    Update:

    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

    
        
            <%= title %>
        
        
            

    <%= text %>

    0 讨论(0)
提交回复
热议问题