简体   繁体   English

使用PHP创建html模板

[英]Creating html templates using PHP

Im learning a lot about how MVC frameworks work by looking around and studying existing ones. 我通过浏览和研究现有框架来了解MVC框架的工作原理。 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)? 这不违反不重复自己的规则(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. 到目前为止,我一直在做的事情是,我一直在创建liitle模板块,然后将它们组合起来创建每个页面。 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>

I've been doing this 我一直这样做

$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>

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. 总而言之,我只想知道正确的方法,以及如何以及为什么......这似乎违反了DRY规则。

Thanks 谢谢

You should check out the concepts of ' layouts ' and ' view helpers '. 您应该查看“ 布局 ”和“ 查看助手 ”的概念。 While I've linked to the Zend Framework version of those concepts, other MVC frameworks (and the MVC concept) should have them as well. 虽然我已经链接到这些概念的Zend Framework版本,但其他MVC框架(以及MVC概念)也应该有它们。

The basic idea is that your page 'view' - for example the login form - is included into your site 'layout' - the general template that is used throughout your site. 基本的想法是,您的网页“查看” - 例如登录表单 - 包含在您的网站“布局”中 - 整个网站中使用的常规模板。 When you request a different controller, with a different view - for example a user profile - that view is also included in the same layout. 当您请求具有不同视图的不同控制器(例如用户配置文件)时,该视图也包含在同一布局中。

To include something like a login form on all pages, a view helper can be used. 要在所有页面上包含类似登录表单的内容,可以使用视图助手。 That view helper could display the current user, or display a login form, depending on the login status. 该视图助手可以显示当前用户,或显示登录表单,具体取决于登录状态。 View helpers may be included in the layout, or included by the specific controller (as long as MVC framework allows some kind of named render segments). 视图助手可以包含在布局中,也可以包含在特定控制器中(只要MVC框架允许某种命名的渲染段)。

The two step 'include' method works better than linear inclusion of parts (including header, then content, then footer - what you're doing now) because your templates do not have to split HTML tags. 两步“包含”方法比零件的线性包含(包括标题,内容,然后页脚 - 您现在正在做的)更好,因为您的模板不必拆分HTML标记。 The Zend Guide has a good visual example of view templates in a layout. Zend Guide在布局中有一个很好的视图模板可视化示例

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. 例如,团队中任何想要处理登录过程的人都可以很容易地发现他们必须编辑login_form.phtml ,他们可以确保编辑login_form.phtml不太可能无意中干扰其他功能。

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>

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. 让我再次提醒你,这不完全是我所做的(我使用的是OOP),所以这可能不完全按原样运行,你可能需要编辑它。

The most common way to do HTML templating with PHP, is to use one of these popular templating engines : 使用PHP进行HTML模板化的最常用方法是使用以下常用模板引擎之一:


Alternatively, you can just put placeholders in your HTML that look something like <% variablename %> . 或者,您可以在HTML中放置类似<% variablename %>占位符。 Just load your HTML, do a regex do find all placeholders and replace them with the corresponding variables. 只需加载你的HTML,做一个正则表达式找到所有占位符并用相应的变量替换它们。


Alternatively, you can load your HTML, parse it as a DOM document and then modify your DOM. 或者,您可以加载HTML,将其解析为DOM文档 ,然后修改DOM。 I created the library DOM Query to be able to do this with a jQuery-like syntax, but you could use vanilla PHP or one of several other libraries as well. 我创建了库DOM Query ,以便能够使用类似jQuery的语法执行此操作,但您也可以使用vanilla PHP或其他几个库中的一个。


Alternatively, you can do some HTML templating in frontend with JavaScript. 或者,您可以使用JavaScript在前端进行一些HTML模板化。 If you want to do HTML templating both on frontend and backend, you might want to consider using Mustache , because Mustache templates can be used both in frontend (with JavaScript) and in backend (with PHP). 如果要在前端和后端进行HTML模板化,可能需要考虑使用Mustache ,因为Mustache模板可以在前端(使用JavaScript)和后端(使用PHP)中使用。

For this project I am working on, all views are XSL templates. 对于我正在研究的这个项目,所有视图都是XSL模板。 Instead of passing variables to the view, I generate all the XML in the controller and transform it with the view. 我不是将变量传递给视图,而是在控制器中生成所有XML并将其转换为视图。

I like this structure because I can keep all my templates in one file, and arrange them anyway I want. 我喜欢这种结构,因为我可以将所有模板保存在一个文件中,并按照我想要的方式安排它们。 It's also a standard template language which is very flexible and has tons of documentation. 它也是一种标准模板语言,非常灵活,有大量文档。 This has been working out really well so far. 到目前为止,这一直很顺利。

A really good example of this structure is the WoW Armory website (view the source). 这个结构的一个很好的例子是WoW Armory网站(查看源代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM