创建自定义 PHP 模板
我创建了一个自定义 PHP 模板系统,但我构建它的方式似乎效率很低。我的模板的三个主要目标是:
- 从 template.tpl include 中提取所有站点范围的 HTML 元素。
- 能够动态分配 template.tpl 中的内容(例如
</code> 或 <code><script></code>) - 尽可能高效且可扩展。
最后,我的模板系统看起来像这样:
randomPage.php
<?php
// declare any page specific resources
$pageTitle = "Random Sub-Title";
$pageResources = "/css/someRandomCSS.css"
$pageContent = "/randomPage.tpl"
// include the generic page template
include dirname($_SERVER['DOCUMENT_ROOT']).'/includes/template.tpl'
?>
randomPage.tpl
<h1><?=$pageTitle?></h1>
<p>Some random page's content</p>
template.tpl
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Site -- <?=$pageTitle?></title>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<link href="<?=pageResources?>" rel="stylesheet" type="text/css">
</head>
<body>
<? include $pageContent ?>
</body>
</html>
这个系统的主要问题是对于每个网页,我需要管理两个文件:一个用于逻辑/数据,另一个用于页面模板。对我来说,这似乎效率很低,而且似乎不是一种可扩展的方法。
最近,我遇到了 smarty 框架,它允许我将我的系统从 randomPage.php 和 randomPage.tpl 整合为类似:
randomSmartyPage.php
{extends file="template.tpl"}
{block name=pageTitle}My Page Title{/block}
{block name=pageResources}
<link href="/css/someRandomCSS.css" rel="stylesheet" text="text/css">
{/block}
{block name=pageContent}My HTML Page Body goes here{/block}
看到这种方法给我提出了三个主要问题
- :我处理模板系统的方式有什么根本缺陷吗?
- 我的原始 php 代码是否可以重构,这样我就不必为每个网页创建两个文件?
- 在这种情况下,使用 smarty(或者可能是替代框架)是一个好主意吗?
I created a custom PHP templating system and the way I built it seems, well, inefficient. The three main goals for my template was to:
- Pull all site-wide HTML elements from a template.tpl include.
- Be able to dynamically assign content in the template.tpl (like
<title>
or<script>
) - Be as efficient and scalable as possible.
In the end, my template system looked something like this:
randomPage.php
<?php
// declare any page specific resources
$pageTitle = "Random Sub-Title";
$pageResources = "/css/someRandomCSS.css"
$pageContent = "/randomPage.tpl"
// include the generic page template
include dirname($_SERVER['DOCUMENT_ROOT']).'/includes/template.tpl'
?>
randomPage.tpl
<h1><?=$pageTitle?></h1>
<p>Some random page's content</p>
template.tpl
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Site -- <?=$pageTitle?></title>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
<link href="<?=pageResources?>" rel="stylesheet" type="text/css">
</head>
<body>
<? include $pageContent ?>
</body>
</html>
The main problem with this system is that for every web page, I need to manage two files: one for logic/data and the other for the page template. This seems largely inefficient to me, and doesn't seem like a very scalable approach.
Recently, I come across the smarty framework, which would allow me to consolidate my system from randomPage.php and randomPage.tpl into something like:
randomSmartyPage.php
{extends file="template.tpl"}
{block name=pageTitle}My Page Title{/block}
{block name=pageResources}
<link href="/css/someRandomCSS.css" rel="stylesheet" text="text/css">
{/block}
{block name=pageContent}My HTML Page Body goes here{/block}
Seeing this approach raised three major questions for me:
- Are there any fundamental flaws with how I am approaching my templating system?
- Can my original php code be refactored so I don't have to create two files for every web page?
- Would using the smarty (or perhaps an alternative framework) be a good idea in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个示例,未经测试。它将封装所有变量,这样您就不会污染全局名称空间。
index.php
视图/home.php
Here is an example, untested. It will encapsulate all the variables so you don't pollute the global namespace.
index.php
views/home.php
您描述的方法是 MVC 设计模式。分离应用程序的不同方面。
您似乎已经了解 PHP 是一个模板系统本身就像在您之前的许多其他人。
看看这个 流行模板系统粗略比较的基准。
更新 2022.08.29
更新了存档版本的断开链接。
注意:如果您正在尝试学习该语言,答案仍然有效。但对于任何严肃的事情,请考虑使用框架。
The approach you are describing is part of MVC design pattern. Separating the different aspects of your application.
What you seem to have already understood is that PHP is a templating system in itself as have many others before you.
Take a look at this benchmark for a rough comparison of popular template systems.
Update 2022.08.29
Updated broken links to archived versions.
Note: The answer remains valid if you're trying to learn the language. But for anything serious, consider using a framework.