【问题标题】:Including files with HTML in Node.js在 Node.js 中包含带有 HTML 的文件
【发布时间】:2014-07-17 20:57:58
【问题描述】:

我似乎无法弄清楚如何使用 Node.js 包含文件。所有其他问题似乎都在讨论包括其他 JS 文件,但我正在寻找相当于 PHP 包含(可以包含 HTML 内容的文件)。

通常在 PHP 中,我的视图文件是这样的:

...stuff

<?php include 'header.php'; ?>
<?php include 'nav.php'; ?>

... more stuff

<?php include 'footer.php'; ?>

例如在nav.php 我有:

<nav role=navigation>

    <ul>
        <!-- links -->
    </ul>

</nav>

Node 中是否有类似的功能,还是我必须改变开发 Web 应用程序的方式?如果有帮助,我正在使用 Express 框架。

非常感谢一个简短的工作示例。

【问题讨论】:

    标签: html node.js express include server-side-includes


    【解决方案1】:

    在 nodejs 中没有类似的东西。

    但许多使用 nodejs 的人也使用 express 和模板引擎之类的框架。

    例如Jade lets you do includes

    index.jade 文件:

    doctype html
    html
      include includes/head
    body
      h1 My Site
      p Welcome to my super lame site.
      include includes/foot
    

    includes/head.jade 文件:

    head
      title My Site
      script(src='/javascripts/jquery.js')
      script(src='/javascripts/app.js')
    

    包含/foot.jade 文件:

    #footer
      p Copyright (c) foobar
    

    需要注意的是,在 ajax、单页应用程序和 javascript 的时代,模板包含在 PHP 的旧时代很少需要。您可能想先玩一些模板(例如使用 Jade),然后再决定是否需要包含。

    【讨论】:

      【解决方案2】:

      不,这不是 nodeJS 的工作方式。如果您喜欢比较,请将 nodejs 与 ruby​​ 或 ASP.NET 进行比较。有玉,正如dystroy所说,但如果你熟悉HTML语法,更喜欢ectjs,例如:

      stuff...
      <div id="content">stuff</div>
      <% include 'footer.ect' %>
      

      当然,如果有代码要包含使用require

      【讨论】:

        【解决方案3】:

        您可以使用 vash 引擎。以这个布局页面为例:

        <body>
            <!-- Body content -->
            <div>@html.block("body")</div>
        
        
            <!-- Render Page Specific Scripts Here -->
            @html.block("scripts")
        </body>
        

        现在在你的其他视图中,你可以这样做:

        @html.extend("layout", function(model){
        
             <!-- main content of the page -->
             @html.block("body",function(model){
                 <h1>This will be the login page</h1>
             })
        
             <!-- page specific scripts -->
             @html.block("scripts",function(model){
                  <scrip></script>
             })
         })
        

        希望这会有所帮助!

        【讨论】:

          【解决方案4】:

          假设我们必须在 index.html 中包含 2 个文件

          最简单的方法

          html index.html

          <%include filename.extension%>
          <%include anotherfile.html%>
          

          node.js

          var temp = require('templatesjs');
          
          // ... ... ...
          
          fs.readFile("./index.html"){
              if(err) throw err;
              var output = temp.set(data);
              res.write(output);
              res.end();
          });
          

          当您使用set() 时,这两个文件将自动包含在 index.html 中

          我用过模块templatesjs

          $ npm install templatesjs
          

          一个很好的文档here
          github:templatesjs

          【讨论】:

            猜你喜欢
            • 2013-01-10
            • 2015-10-09
            • 1970-01-01
            • 1970-01-01
            • 2011-06-19
            • 2016-10-07
            • 2016-04-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多