在 PHP 中我们会这样做:
index.php
<?php
include 'inc/head.inc.php'; //DOCTYPE and <head> stuff
include 'inc/header.inc.php'; //NAV or other top-of-page boilerplate
?>
<div id="uniqueDIV">
//Your unique page-specific divs can go here
</div>
<?php include 'inc/footer.inc.php'; ?>
因此,在您的 head.inc.php 文件中,您只有页面文件的常用 DOCTYPE 和 <head> 部分。
在 Ruby 中,等效指令似乎是这样的:
load "inc/head_inc.rb" -- OR --
require_relative "inc/head_inc.rb"
https://practicingruby.com/articles/ways-to-load-code
另一种选择是使用一些 js/jQuery 来填充文档的某些部分。如果您没有 PHP,这是第二个最佳选择。 它不如 PHP 优化,因为 js/jQ 代码将在页面完全呈现后运行,这可能会导致代码出现之前的微小(但明显)延迟。
例如:
html
<div id="navbarDIV"></div>
js/jQuery:
<script type="text/javascript">
$(function(){
$('#navbarDIV').load( 'inc/navbar.inc.html' );
});
</script>
请注意,您需要加载 jQuery 才能使用上述代码。就像<head> 中的这一行一样简单:
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
jsFiddle demo
最后说明:脚本标签可以作为外部文件包含在您的<head> 中,也可以与其余html 一起放在文档中的任何位置。什么都行。但是<head> 作为外部文件,或者正文中的最后一个元素(也作为外部文件)是首选。
最后的注解:“.inc.”不需要命名约定,这只是我自己的做法。包含文件可以命名为head.html 或head.php 等。