使用PHP创建HTML模板 [英] Creating html templates using PHP
问题描述
通过环顾和研究现有框架,我了解了很多有关MVC框架如何工作的知识。似乎我看到的每个框架都有一个布局,其中每个控制器中的每个方法都有自己的模板文件。所以会有一个登录模板,一个注销模板,注册等等。
Im learning a lot about how MVC frameworks work by looking around and studying existing ones. It seems that every framework I see has a layout where each method in each controller has its own template file. So there will be a login template, a logout template, register, so on and so on.
我的问题是,如何以及为什么要为整个模板创建模板页面全部放在一个文件中。假设您要在多个页面上显示登录表单,是否不必为要显示在其上的每个模板创建登录表单?
My question is, how and why would you create a template for the entire page all within one file. Lets say you wanted to show the login form on more than one page, wouldn't you have to create the login form for each template that you want it to display on? Doesn't that go against the don't repeat yourself rule (DRY)?
到目前为止,我做事的方式是创建小块模板,然后将它们组合以创建每个页面。因此,不要做这样的事情,
The way i've been doing things so far is I've been creating liitle chunks of templates and then combining them to create each page. So instead of doing something like this,
$title = 'Blah Blah Blah';
$user = 'Jon Miller';
include 'index.phtml';
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h3><?php echo $user; ?></h3>
<form>login form</form>
</body>
</html>
我一直在这样做
$title = 'Blah Blah Blah';
include 'header.phtml';
$user = 'Jon Miller';
include 'user.phtml';
include 'login_form.phtml';
include 'footer.phtml';
header.phtml
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
user.phtml
<h3><?php echo $user; ?></h3>
login_form.phtml
<form>login form</form>
footer.phtml
</body>
</html>
总而言之,我只想知道正确的方法,以及如何以及为什么...这似乎违反了DRY规则。
As alway, I would just like to know the proper way to do it, along with how and why...It just seems to go against the DRY rule.
谢谢
推荐答案
一个词:组织。分开页面的每个部分将允许分别查看/编辑它们。这个简单的概念非常有益。例如,团队中任何想要处理登录过程的人都可以轻松地知道他们必须编辑 login_form.phtml
,并且可以确定编辑 login_form.phtml
不太可能无意间干扰其他功能。
One word: Organization. Separating each part of the page will allow each of them to be viewed/edited separately. This simple concept is very beneficial. For example, anyone in the team that want to handle login process can easily figure out that they have to edit login_form.phtml
and they can be sure that editing login_form.phtml
will less likely to unintentionally interfere with other functionalities.
按照最佳做法,这是我的操作方式(不是
As of the best practice, here is how I do it (not exactly but similar).
$Title = 'Blah Blah Blah';
$User = 'Jon Miller';
$ThemeName = "MyGreenPage";
$Contents = array("User", "Login_Form");
function Include($FileName) {
if (file_exists($FileName))
include $FileName;
}
MyGreenPage.phtml
:
MyGreenPage.phtml
:
<html>
<head>
<title><?php echo $title; ?></title>
<?php
foreach($Contents as $Content)
Include("$Content.pcss");
?>
<?php
foreach($Contents as $Content)
Include("$Content.pjs");
?>
</head>
<body>
<?php
foreach($Contents as $Content)
Include("$Content.phtml");
?>
</body>
</html>
User.pcss
:
User.pcss
:
/* Some styles needed by User */
User.pjs
:
User.pjs
:
/* Some script needed by User */
User.phtml
:
User.phtml
:
<h3><?php echo $user; ?></h3>
Login_Form.pcss
:
Login_Form.pcss
:
/* Some styles needed by Login_Form */
Login_Form.pjs
:
Login_Form.pjs
:
/* Some script needed by Login_Form */
Login_Form.phtml
:
Login_Form.phtml
:
<form>login form</form>
让我再次提醒您,这并不是我所做的(我使用OOP所做的事情)因此它可能无法完全按原样运行,您可能需要对其进行编辑。
Let me remind you again that this is not that exactly what I do (what I do use OOP) so this may not exactly run as is and you may need to edit it.
这篇关于使用PHP创建HTML模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!