Skip to main content
added 1 characters in body
Source Link
mheinzerling
  • 2.7k
  • 15
  • 17

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><?=$pageTitle=$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.

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><?=$pageTitle?></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.

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.

Source Link
mheinzerling
  • 2.7k
  • 15
  • 17

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><?=$pageTitle?></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.