EJS - pass variable when include

前端 未结 3 1522
醉话见心
醉话见心 2021-02-19 18:33

I\'m using ejs in backend with nodejs. I\'d like to pass variable when include. When include the header, pass the page title.

index.ejs:

<% include he         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-19 18:56

    you can pass my_tytle directly to the index.ejs and if there is a partial view for header, my_tytle should be accessible to the header.

    for example: index.ejs:

    <% include header %>
     . . . 
    <% include footer%>
    

    header.ejs:

    
    
        <%- my_tytle %>
    
    

    now from node server if you pass the value for my_tytle to index.ejs, like this:

    res.render('template_file.js', {
            my_tytle : "Value for your title"
        });
    

    then your partial view (i.e header in your case) would be also able to access that variable.

提交回复
热议问题