模板语言 vs. 直接 PHP [英] Template language vs. straight PHP

查看:21
本文介绍了模板语言 vs. 直接 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算编写一个 CMS,但现在我正在写下我的所有想法,并在开始之前尝试弄清楚我的所有概念.我被撕裂的一件事是是否使用模板语言并解析网站的页面,并用内容项替换模板标签,或者只是使用直接的 PHP 开发网站并让 CMS 生成有帮助的数据结构.例如:

I'm going to write a CMS, but right now I'm writing down all my ideas and trying to get all of my concepts straight before I start. One of the things I'm torn on is whether to use a template language and parse the pages of the website, and replace template tags with content items, or just develop the site with straight PHP and have the CMS generate data structures which help. For example:

{navigation: products}

对比

foreach($cms_label['products'] as $product) {

    echo '<li class="product_nav">'.
         '<a href="products/{$product.id}">{$product.name}</a>'.
         "</li>\n";

}

前者更简洁,但需要发明一种语言,并在显示前解析每个页面.后者不太干净,但我认为如果 CMS 只为所有代码提供数据,它可以工作得非常好.但是,这会被认为是将逻辑与演示混合在一起吗?我考虑过的另一种替代方法是使用类似于模板标签的 PHP 函数:

The former is cleaner but it would involve inventing a language, plus parsing every page before display. The latter is less clean but I think it could work really great if the CMS just provided data for all the code. But, would this be considered mixing logic with presentation? Another alternative I've considered is using PHP functions that are similar to the template tags:

<?php navigation('products'); ?>

你有什么想法?

请记住,我不需要做任何比在某个地方包含一个页面或写出一个无序列表更复杂的事情;其余的由 CSS 处理.

Keep in mind I don't have to do anything more complicated than including a page at a certain place, or writing out an unordered list; the rest shall be handled by CSS.

推荐答案

PHP 的模板语言是一种名为内部平台效应."Smarty 是 PHP 模板框架的一个例子,但即使是一本关于 Smarty 的书的作者 Hasin Hayder 也说 Smarty 已经死了,没有必要再使用它了.

Template languages for PHP are an example of an anti-pattern called "Inner-Platform Effect." Smarty is an example of a template framework for PHP, but even Hasin Hayder, author of a book on Smarty says that Smarty is dead and there's no need to use it anymore.

开发模板语言可能有充分的理由,例如,如果您的非编码设计师或内容编辑人员正在使用您的 CMS,并且您不想用 PHP 的复杂性压倒他们(或允许他们编写可能会破坏您网站的代码).

There can be good reasons to develop a template language, for example if you are having non-coder designers or content editors using your CMS and you don't want to overwhelm them with the complexity of PHP (or allow them to write code that could break your website).

但是您没有将其描述为目标,因此我认为在这种情况下最好使用 PHP 作为页面模板语言.由于您不必开发自己的新语言,因此工作量会减少,而且它会为您需要特定类型的动态内容的罕见情况提供更大的灵活性.

But you haven't described that as a goal, so I'd assume using PHP as your page template language is best in this case. It'll be less work because you don't have to develop your own new language, and it'll provide greater flexibility for uncommon cases where you need a specific kind of dynamic content.

不要编写 PHP 函数来封装 HTML 输出块.相反,使用 include() 来拉入 HTML 片段.这种技术有时被称为部分".

Don't write PHP functions to encapsulate blocks of HTML output. Instead, use include() to pull in fragments of HTML. This technique is sometimes called "partials."

您还可以使用 MVC 框架(例如 Symfony、Kohana、Solar、CodeIgniter 或 Zend Framework)来帮助您遵守将 PHP 模板代码与应用程序的其余部分代码分开的纪律.

You can also use an MVC framework such as Symfony, Kohana, Solar, CodeIgniter, or Zend Framework to help you keep discipline about separating your PHP template code from the rest of your application code.

这篇关于模板语言 vs. 直接 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆