简体   繁体   中英

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

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.

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.

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

There are several ways of achieving separation between logic and representation (it's formally known as part of MVC - Model-View-Controller methodology).

One direction I would like to point you in is 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 ).

Templating in PHP is a religious debate - one I would rather not get dragged into. Suffice to say I'm strongly against Smarty - it's bulky and does nothing that standard PHP does not already do.

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 .
But whati is the content of %_TPL_ and where it written? In the same old business logic, I suppose. So, where is your desired separation then?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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