PHP Pattern to Decouple UI Markup from Static Content

那年仲夏 提交于 2020-01-05 05:58:17

问题


I deal with a website that is composed of a lot of static content. One of the things that bothers me about the site is the amount of duplicate/one-off markup and that every time I have to change or update the UI, I have to modify a lot of different files and then make sure I didn't break anything. I was considering moving the website content to a PHP server and reorganizing the site contents to make my job easier.

/* This is also something of an exercise for me. I wanted to get my feet wet with PHP. Because of this, I chose not to go with an off the shelf framework. */

Is the following pattern sane to use for a static website? That is, assuming I want a simple framework for managing my website and I want to decouple my website UI from my content, is this a reasonable way to get it done? Have I overlooked anything?

Assume that I want to declare my content for each page on the website like this:

/* begin contentStub.php */
<?php $headerUtil->setTitle("Hola Mundo!"); ?>
<p>Hello world</p>
/* end contentStub.php */

Also assume that I want to use a simple template for my site like this:

/* begin UITemplate.php */
<!DOCTYPE html>
<html>
    <head>
        <title><?php echo($headerUtil->getTitle()); ?></title>
    </head>
    <body>
        <?php echo($content); ?> 
    </body>
</html>
/* end UITemplate.php */

Now assume I set the PHP directive for auto_prepend_file to use the following file:

/* begin file: prepend.php */
<?php
    class HeaderUtil
    {
        private $title;
        public function __construct()
        {
            $this->title = "new page";
        }
        public function getTitle(){return $this->title;}
        public function setTitle($title){$this->title = $title;}

        //TODO: Add methods/collections to deal with css/js/keywords
    }

    $headerUtil = new HeaderUtil();

    ob_start();
        include($_SERVER['SCRIPT_FILENAME']);
    $content = ob_get_contents();
    ob_end_clean();

    include("UITemplate.php");

    die();
?>
/*  end file: prepend.php */

This example seems to run as I expect it to. Is this a reasonable way to decouple my website UI from my content using PHP? Much more specifically, assuming my requirements are unchanging, do I face any obvious and/or significant security, stability, or resource exhaustion risks using my approach?

Motivation for asking: I have a bit of experience with Coldfusion and have previously worked with Java ServerFaces 2. For those technologies, threading/scope was important and, in any language, file access time is critical. Am I missing anything here?

来源:https://stackoverflow.com/questions/16123285/php-pattern-to-decouple-ui-markup-from-static-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!