简体   繁体   English

PHP中的HTML标记模板

[英]HTML markup templates in PHP

When I write PHP code for websites, I don't like mixing business logic with the presentation layer and as such I tend to create markup templates. 当我为网站编写PHP代码时,我不喜欢将业务逻辑与表示层混合使用,因此,我倾向于创建标记模板。 I've written a very lightweight template engine to facilitate this, since I really don't want to move to a fully-fledged template framework like Smarty. 我已经编写了一个非常轻量级的模板引擎来简化此操作,因为我真的不想转向像Smarty这样成熟的模板框架。

Here's a simplified example of what I do: 这是我所做的简化示例:

function renderTemplatePage($page, $params)
{
    $page = readTemplateFile("templates/{$page}");
    $tokens = getTemplateTokens($page);
    foreach($tokens as $token)
    {
        if(substr($token, 0, 6) == "%_TPL_")
        {
            $subPage = renderTemplatePage(tokenToPageName($token), $params);
            $page = str_replace($token, $subPage, $page);
        }
        else
        {
            $page = str_replace($token, $params[$token], $page);
        }
    }
    return $page;
}

Sample page: 样本页面:

<html>
    <head><title>%_PageTitle_%</title></head>
    <body>
        <div id="header">%_TPL_Header_%</div>
        <div id="content">%_TPL_Homepage_%</div>
        <div id="footer">%_TPL_Footer_%</div>
    </body>
</html>

A call to renderTemplatePage("index", array("PageTitle" => "Home")) would produce a page entitled "Home", with content from the Header, Homepage and Footer templates. 调用renderTemplatePage("index", array("PageTitle" => "Home"))会产生一个标题为“ Home”的页面,其内容来自页眉,主页和页脚模板。

I do all of my logic (including db queries, etc) before calling the rendering, so I can mass up a large $params array and just do a single call to render it all. 在调用渲染之前,我已经做了所有的逻辑工作(包括数据库查询等),因此我可以处理一个大的$params数组,只需要执行一次调用就可以渲染全部。

Are there any flaws in this methodology? 这种方法是否有缺陷? Is there a more standard way to do this? 有更标准的方法可以做到这一点吗?

Its flawed. 它的缺点。 How do you handle template specific logic . 您如何处理模板特定的逻辑。 Just out of curiosity how would you handle ifs or loops 出于好奇,您将如何处理ifsloops

There are several ways of achieving separation between logic and representation (it's formally known as part of MVC - Model-View-Controller methodology). 有几种方法可以实现逻辑和表示之间的分离(正式称为MVC的一部分-模型-视图-控制器方法)。

One direction I would like to point you in is XML+XSLT. 我想指出的一个方向是XML + XSLT。 The idea is to assemble all the information you need as an XML string (eg have a look at the output of the following URL: http://www.whiteoctober.co.uk/?dumpXML ) , and then perform an XSLT transform on it (see http://www.w3schools.com/xsl/xsl_transformation.asp ). 这个想法是将您需要的所有信息组装为XML字符串(例如,查看以下URL的输出: http : //www.whiteoctober.co.uk/?dumpXML ),然后在上执行XSLT转换它(请参阅http://www.w3schools.com/xsl/xsl_transformation.asp )。

Templating in PHP is a religious debate - one I would rather not get dragged into. 用PHP进行模板是一种宗教辩论-我不希望被卷入其中。 Suffice to say I'm strongly against Smarty - it's bulky and does nothing that standard PHP does not already do. 可以说我强烈反对Smarty-它体积庞大,并且没有标准PHP尚未做的任何事情。

The problem with all these homebrew templates is lack of real life testing. 所有这些自制程序模板的问题在于缺乏实际测试。
Instead of doing such a test you are coming here to ask other people. 而不是进行这种测试,您是来这里问其他人的。
It is not so wise strategy in general, as your own experience is indispensable, while there are not so much pro's hangs around having enough time to write you an extended answer. 一般来讲,这不是明智的策略,因为您自己的经验是必不可少的,而没有太多的专业人士会花时间为您写一个扩展答案。 While there are always lots of inexperienced users ready to answer. 虽然总是有很多没有经验的用户准备回答。

Are there any flaws in this methodology? 这种方法是否有缺陷?

yes, of course. 当然是。

You are declaring that you don't like mixing business logic with the presentation layer . 您在声明您don't like mixing business logic with the presentation layer
But whati is the content of %_TPL_ and where it written? 但是%_TPL_的内容是%_TPL_以及它写在哪里? In the same old business logic, I suppose. 我想以相同的旧业务逻辑。 So, where is your desired separation then? 那么,您想要的分离位置在哪里?

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

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