【问题标题】:Call Multiple Files on Express - EJS在 Express 上调用多个文件 - EJS
【发布时间】:2017-06-21 13:44:05
【问题描述】:

我正在为我的网络应用程序使用 express 4 - ejs 组合,我想知道如何根据被调用的路由/url 渲染多个文件。

假设我有 header.ejs 我想在每个页面上呈现,header.ejs 代码是:

<div class="wrapper">
  <div class="header-area">Header IS Gonna Be Here</div>
  <div class="content-area">Content Area Gonna Be Different on Every Page Depend on the Router</div>
</div>

我现有的调用 header.ejs 文件的方法是using res.render(),但我无法弄清楚同时渲染多个文件。谢谢你的回答,如果你觉得我的问题已经被问到了,可以给我链接吗,我去看看。

【问题讨论】:

    标签: node.js express ejs


    【解决方案1】:

    这个概念被称为模板化布局,基本上是创建像页眉页脚这样保持静态的部分。在 res.render() 上,您将使用您需要的数据渲染视图并添加到页眉和页脚:

    app.get('/', function(req, res){ 
      res.render('index',{user:"John Smith"}) 
    // pass main ejs file name, pass any data variables 
    }); 
    

    要渲染名为 index.ejs 的文件,请使用 include 语句与其他 ejs 文件组合。请确保 index.ejs 在您最初设置的视图目录中

    <!DOCTYPE html>
    < html lang="en">
      < head>
        <% include ../partials/head %>
      </ head>
      < body class="container">
        < header>
          <% include ../partials/header %>
        </ header>
        < main>
          < div class="jumbotron">
            < h1> This is great </ h1>
            < p> Welcome to templating using EJS </ p>
            < p> welcome <%= user%>;< /p>
          </ div>
        </ main>
        < footer>
          <% include ../partials/footer %>
        </ footer>
      </ body>
    </ html>
    

    Check for tutorial

    【讨论】:

    • 以及如何区分哪个文件包含在不同的路由上?
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2013-06-10
    • 2017-08-23
    • 2013-06-05
    • 2020-08-12
    • 2019-11-25
    • 1970-01-01
    • 2021-02-04
    相关资源
    最近更新 更多