创建自定义 PHP 模板

发布于 2024-10-31 20:13:11 字数 1846 浏览 1 评论 0原文

我创建了一个自定义 PHP 模板系统,但我构建它的方式似乎效率很低。我的模板的三个主要目标是:

  1. 从 template.tpl include 中提取所有站点范围的 HTML 元素。
  2. 能够动态分配 template.tpl 中的内容(例如 </code> 或 <code><script></code>)
  3. 尽可能高效且可扩展。

最后,我的模板系统看起来像这样:

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}

看到这种方法给我提出了三个主要问题

  1. :我处理模板系统的方式有什么根本缺陷吗?
  2. 我的原始 php 代码是否可以重构,这样我就不必为每个网页创建两个文件?
  3. 在这种情况下,使用 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:

  1. Pull all site-wide HTML elements from a template.tpl include.
  2. Be able to dynamically assign content in the template.tpl (like <title> or <script>)
  3. 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:

  1. Are there any fundamental flaws with how I am approaching my templating system?
  2. Can my original php code be refactored so I don't have to create two files for every web page?
  3. Would using the smarty (or perhaps an alternative framework) be a good idea in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两相知 2024-11-07 20:13:11
  1. 除了 PHP 本身之外,您的代码实际上并未使用任何模板引擎,这很好。我发现的一个缺陷是您的模板将有权访问所有变量,并且您为其创建的变量都是全局变量。
  2. 两个文件是一个很好的系统,一个用于更改预处理并传递给视图的内容,另一个用于视图本身,包含 HTML 或其他内容。这使您可以轻松交换视图,例如,标准浏览的视图和移动浏览器的移动视图。
  3. 这可能是一个好主意,但我坚信使用 PHP 就足够好了。

这是一个示例,未经测试。它将封装所有变量,这样您就不会污染全局名称空间。

index.php

function view($file, $vars) {
    ob_start();
    extract($vars);
    include dirname(__FILE__) . '/views/' . $file . '.php';
    $buffer = ob_get_contents();
    ob_end_clean();
    return $buffer;
}

echo view('home', array('content' => Home::getContent()));

视图/home.php

<h1>Home</h1>
<?php echo $content; ?>
  1. Your code isn't really using any templating engine besides PHP itself, which is fine. A flaw I can see is your template will have access to all variables, and the ones you create for it are all global.
  2. Two files is a good system, one to change what is preprocessed and passed to the view, and one for the view itself, containing HTML or whatever. This allows you to easily swap views, for example, a view for standard browsing and a mobile view for mobile browsers.
  3. It can be a good idea, but I'm a firm believer that using PHP is good enough.

Here is an example, untested. It will encapsulate all the variables so you don't pollute the global namespace.

index.php

function view($file, $vars) {
    ob_start();
    extract($vars);
    include dirname(__FILE__) . '/views/' . $file . '.php';
    $buffer = ob_get_contents();
    ob_end_clean();
    return $buffer;
}

echo view('home', array('content' => Home::getContent()));

views/home.php

<h1>Home</h1>
<?php echo $content; ?>
苦笑流年记忆 2024-11-07 20:13:11

您描述的方法是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文