HTML 模板——php?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13071784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:44:36  来源:igfitidea点击:

HTML Templates -- php?

phphtmltemplatestemplating

提问by Code Monkey

So, I'm making a site about WWI as a school assignment, and I want this to appear in every document:

所以,我正在制作一个关于第一次世界大战的网站作为学校作业,我希望它出现在每个文件中:

<!DOCTYPE html>

<html>
<head>
    <title>1914</title>
    <script src="modernizr-1.5.js"></script>
    <link href="styles.css" type="text/css" rel="stylesheet" />
    <meta charset="utf-8" />

</head>
<body>
  <div id="container">
    <header>
    <img src="images/banner.png" alt="World War I" style="border: none"/>
    <nav>
        <ul>
        <a href="index.htm"><li><span>Home</span></li></a>
        <a href="1914.htm"><li><span>1914</span></li></a>
        <a href="1915.htm"><li><span>1915</span></li></a>
        <a href="1916.htm"><li><span>1916</span></li></a>
        <a href="1917.htm"><li><span>1917</span></li></a>
        <a href="1918.htm"><li><span>1918</span></li></a>
        </ul>
    </nav>
    </header>
    <section>
    <article>
    <br style="clear: both" />
    </article>
    <aside>
    </aside>
    </section>
    <footer style="font-weight: bold; letter-spacing: .1em">
    <a href="citations.htm">Citations</a> &bull;
    <a href="about.htm">About</a>
    </footer>
  </div>
</body>
</html>

I think it's kind of stupid to copy-paste all this into each document, and painstakingly go in and change each page separately if I just want to change one word or tag. Is there a way I can put this in template.htm (or something similar) and have php or javascript code take this and insert everything from the requested file inside of the <article>tag? I don't know a lot of php, so this is probably a piece of cake for you gurus, but I would appreciate the help.

我认为将所有这些复制粘贴到每个文档中,如果我只想更改一个单词或标签,那么煞费苦心地单独更改每个页面是一种愚蠢的做法。有没有办法我可以把它放在 template.htm (或类似的东西)中,并让 php 或 javascript 代码把它取出来并插入<article>标签内请求文件中的所有内容?我不懂很多 php,所以这对你们大师来说可能是小菜一碟,但我会很感激你的帮助。

回答by Rick Calder

Using php includes:

使用 php 包括:

Save this as top.php

将其另存为 top.php

<html>
<head>
<title><?php echo $title; ?></title>
<script src="modernizr-1.5.js"></script>
<link href="styles.css" type="text/css" rel="stylesheet" />
<meta charset="utf-8" />

</head>
<body>
<div id="container">
<header>
<img src="images/banner.png" alt="World War I" style="border: none"/>
<nav>
    <ul>
    <a href="index.htm"><li><span>Home</span></li></a>
    <a href="1914.htm"><li><span>1914</span></li></a>
    <a href="1915.htm"><li><span>1915</span></li></a>
    <a href="1916.htm"><li><span>1916</span></li></a>
    <a href="1917.htm"><li><span>1917</span></li></a>
    <a href="1918.htm"><li><span>1918</span></li></a>
    </ul>
</nav>
</header>
<section>

Save this as bottom.php

将此保存为bottom.php

<aside>
</aside>
</section>
<footer style="font-weight: bold; letter-spacing: .1em">
<a href="citations.htm">Citations</a> &bull;
<a href="about.htm">About</a>
</footer>
</div>
</body>
</html>

Then your individual pages would be like this:

那么您的个人页面将是这样的:

<?php $title = '1914'; include("top.php");?>
//This would be where you would make the changes that need to be made on each page.

<article>
<br style="clear: both" />
</article>

<?php include("bottom.php");?>

回答by John Slegers

You could consider one of these popular template engines :

您可以考虑以下流行的模板引擎之一:

  • Smarty(becoming outdated)

  • Latte(used mostly by the Nettecommunity)

  • Twig(used mostly by the Symfonycommunity)

  • Mustache(official implementations of this templating engine exist in more than two dozen proramming/scripting languages)

  • Smarty(变得过时)

  • 拿铁(主要由Nette社区使用)

  • Twig(主要由Symfony社区使用)

  • Mustache(这个模板引擎的官方实现存在于两种以上的编程/脚本语言中)

I tend to favor Mustache, mostly because it has an official JS version, an official Ruby version, an official Java version, etc. It allows you to use the same templates frontend and backend, which is very useful for widgets that are first rendered in background and rerendered in foreground at updates.

我倾向于使用Mustache,主要是因为它有官方 JS 版本官方 Ruby 版本官方 Java 版本等。它允许您使用相同的模板前端和后端,这对于首先呈现在背景并在更新时在前台重新渲染。

回答by soundswaste

Put the content in a file abc.php

将内容放在文件abc.php 中

and then add this to each page you want the desired content in :

然后将此添加到您想要所需内容的每个页面:

<?php
include("abc.php");
?>

So if your code is :

所以如果你的代码是:

<nav>
        <ul>
        <a href="index.htm"><li><span>Home</span></li></a>
        <a href="1914.htm"><li><span>1914</span></li></a>
        <a href="1915.htm"><li><span>1915</span></li></a>
        <a href="1916.htm"><li><span>1916</span></li></a>
        <a href="1917.htm"><li><span>1917</span></li></a>
        <a href="1918.htm"><li><span>1918</span></li></a>
        </ul>
    </nav>
    </header>
    <section>
    <article>
    <br style="clear: both" />
    </article>
    <aside>
    </aside>
    </section>

And you want the part inside <nav>to be repeated in each page, you can put the content between <nav>and </nav>(including the tags) inside abc.phpand include abc.phpin your file like this :

并且您希望<nav>在每个页面中重复里面的部分,您可以将<nav></nav>(包括标签)之间的内容放在abc.php 中,并将abc.php包含在您的文件中,如下所示:

<?php
    include("abc.php");
?>
    </header>
    <section>
    <article>
    <br style="clear: both" />
    </article>
    <aside>
    </aside>
    </section>

回答by Spudley

Create the template file as a single file, exactly you have it already, but printing PHP variables in the places where you want content.

将模板文件创建为单个文件,您已经拥有它,但在您需要内容的地方打印 PHP 变量。

eg:

例如:

....
<article>
<?php print $mainContent; ?>
</article>
....

Then write a PHP function as follows:

然后写一个PHP函数如下:

<?php
function insertContentIntoTemplate($mainContent) {
    require_once('/path/to/template.php');
}
?>

Now you can load your page content into the template simply by calling the function and passing the content into it for each page.

现在,您只需调用该函数并将内容传递给每个页面,即可将页面内容加载到模板中。

So, for example, 1914.php could look like this:

因此,例如,1914.php 可能如下所示:

<?php
require_once('/path/to/insertFunction.php');

//the text could be loaded from a DB, or from another file, or just as a plain string like this:
$text = "The year was 1914, and the war was just starting.";

insertContentIntoTemplate($text);

?>

Congratulations. You now have a working (albeit very simple) template system. Add more variables / placeholders to your template as required.

恭喜。您现在有一个有效的(虽然非常简单)模板系统。根据需要向模板添加更多变量/占位符。