1
\$\begingroup\$

In order to separate my views (markup) and code, I elected to write my views like this:

    <!DOCTYPE HTML>
    <html>
    <head>
        <title>%PAGE_TITLE%</title>
        <meta charset="UTF-8">
        <link href='PotatoDocs/views/stylesheets/docs2013.css' type='text/css' rel='stylesheet'>
        <link rel="shortcut icon" href="favicon.ico" />

        <link rel="stylesheet" href="highlight.js/styles/default.css">
        <script src="highlight.js/highlight.pack.js"></script>
        <script>hljs.initHighlightingOnLoad();</script>
    </head>
    <body>
        <section id='wrapper'>
            <section id='content'>
                <h1>%PAGE_TITLE%</h1>
                %CONTENT%
            </section>

            <section id='sidebar'>
                %SIDEBAR%
            </section>  

            <div class='clear'>&nbsp;</div>

        </section>


        <section id='footer'>
            Potato Seed 2013
        </section>
    </body>
    </html>

A method then replaces the substitution area with parts of the website, like so:

    private function generatePageSource($page_title)
    {
        $page_template = file_get_contents(REGISTRY_CODE_PATH . "views/templates/" .
            $this->template_dir . "/main.html");

        $page_content = str_replace("%PAGE_TITLE%", pdisplay($page_title), $page_template);
        $page_content = str_replace("%CONTENT%", $this->pagecontent_content, $page_content);
        $page_content = str_replace("%SIDEBAR%", $this->sidebar_content, $page_content);

        $this->page_content = $page_content;
    }

The $this->page_content is later echoed.

Not too relevant to my question, but for the sake of completion, this is the pdisplay() function.

function pdisplay($pdisplay)
{
    $pdisplay = htmlentities(stripslashes($pdisplay), ENT_QUOTES);
    return $pdisplay;
}

My question is this (though I accept any code criticism at all): is this a sensible way to render pages?

I used to implant php tags with echoes onto a page -- but this meant that code (even a little bit) was on the views, so I thought this would be a better solution.

\$\endgroup\$
  • \$\begingroup\$ This is a sensible way to render pages, but why not use Twig, or Smarty, or even Savant? \$\endgroup\$ – jsanc623 Aug 15 '13 at 19:33
  • \$\begingroup\$ I considered a template engine -- but for what I needed, this simple solution seemed to work best. \$\endgroup\$ – Racktash Aug 15 '13 at 20:42
  • \$\begingroup\$ It's fine but a bit limited. Will you ever want to share the same head template between multiple pages? Or perhaps the side bar? Once you move past one template per web page then it can quickly get complicated. \$\endgroup\$ – Cerad Aug 18 '13 at 20:27
1
\$\begingroup\$

According your comment you don't want to use a template engine, but are actually creating a new one. At some point you will need loops or ifs and it starts becoming ugly.

PHP is already a template engine. Actually it was designed for exactly this purpose. There is a braceless/echoless syntax for all control structures.

Your template/view:

<!DOCTYPE HTML>
<html>
<head>
    <title><?=$page_title?></title>
    ...
</head>
<body>
    <section id='wrapper'>
        <section id='content'>
            <h1><?=$page_title?></h1>
            <?=$content?>
        </section>
     ...
</body>
</html>

Your controller:

private function generatePageSource($page_title)
{
    $page_title=pdisplay($page_title));
    $content = $this->pagecontent_content;

    ob_start();
    $template=REGISTRY_CODE_PATH . "views/templates/" .
        $this->template_dir . "/main.html";
    include $template;
    $this->page_content=ob_get_clean();
}

Of course you could wrap this in a View class to DRY.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the reply. Currently, my loops reside in the controllers -- which then use more templates to build up the inner contents of the page. The sidebar has another template to describe how the navigation links will look. This template becomes the string for %SIDEBAR%. \$\endgroup\$ – Racktash Aug 16 '13 at 7:14
  • \$\begingroup\$ I tried this approach some year ago, but as soon as the structure gets a little more complex you have a lot of templates. So it will be really hard to change your layout afterward as you also have to change your controller in most cases. Separation of logic and representation does not mean that there is not something like a representation logic (displaying a list, conditional style classes, etc.) which belongs to the representation. \$\endgroup\$ – mheinzerling Aug 16 '13 at 7:43
  • \$\begingroup\$ +1 for pointing out that php is in fact a template engine and a really good one. I would however opt for one of the realy good engines out there. Personally I favor twig since it is part of Symfony \$\endgroup\$ – Pinoniq Aug 19 '13 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.