node.js - 以类似于 PHP 的方式加载 html [英] node.js - Loading html in a similar way to PHP

查看:26
本文介绍了node.js - 以类似于 PHP 的方式加载 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习用于网站开发的 Node.js.
以前我可以使用 PHP 在每个页面上包含类似导航栏的内容,使用 <?php include('blah.php') ?>.这与我使用页面上的实际代码的方式相同.
我的问题是:有什么方法可以使用 JS/Node.js 将 HTML 元素/页面从一个页面导入到另一个页面?
请记住,PHP 直接与同一文件中的 HTML 集成,而 Node.js 没有如此无缝地集成,所以我所问的可能是不可能的.

I am learning Node.js for website development.
Previously I could use PHP to include something like a navigation bar on every page, using <?php include('blah.php') ?>. This works in the same way of me just using the actual code on the page.
My question is: is there any way to import HTML elements / pages from one page to another with JS / Node.js?
Bearing in mind, PHP integrates directly with HTML in the same file, and Node.js does not integrate so seamlessly, so what I'm asking might be impossible.

推荐答案

使用 EJS - 嵌入式 JavaScript 模板.

use EJS - Embedded JavaScript templating.

http://ejs.co/

设置分隔符:ejs.delimiter = '?';

 <?- include('header'); -?>
<h1>
  Title
</h1>
<p>
  My page
</p>
<?- include('footer'); -?>

这篇关于node.js - 以类似于 PHP 的方式加载 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆