Is there a way to separate PHP Code and HTML? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Is there a way to separate PHP Code and HTML?

Damian99Damian99 USAPosts: 116Member

I need to separate PHP code and HTML. Is there a way to do this?

Tagged:

Comments

  • mariahsharpmariahsharp Posts: 47Member

    To separate your PHP code and HTML as much as possible you need a small template class and a small set of rules to follow.

    Function calls should only be made to render templates or format data like date().
    Only use foreach, no for or while loops.
    if statements should only check a variable for true. All boolean logic should be pre-computed.
    Using else is OK.
    
    If you need to alternate a color, either pass boolean values with your array for each row or use ($i++%2). Then in your view you will use an inline if:
    
    <div class='menu <?=($link['alternate'])?'white':'grey'?>'>
    
    If you need to check for the beginning or end of a list, pass a flag in the associate array that you're currently iterating through.
    

    To make simple or advanced web site you need something that can hold data and a file path. With the below classes you create a new Template object and bind some data to it as it is created.

    $main = new Template('mainView.php', array('title' => 'example page'));

    Now in your view file mainView.php you access the title like this.

    <?= $title; ?> // php 4 version <?= $this->title; ?> // php 5 version

    The reason you use an object instead of just an include file is to encapsulate data. For example:

    $main = new Template('mainView.php', array( 'title' => 'example page', 'leftMenu' => new Template('linkView.php', array('links' => $links)), 'centerContent' => new Template('homeView.php', array('date' => date())), ));

    $main->render();

    mainView.php

    <?= $this->title; ?><title/>

    <? $this->leftMenu->render(); ?>
    <? $this->centerContent->render(); ?>

    Below are minimal template classes for both php 4 and 5.

    // PHP 4 class Template { var $args; var $file;

    function Template($file, $args = array()) {
        $this->file = $file;
        $this->args = $args;
    }
    
    function render() {
        extract($this->args);
        include $this->file;
    }
    

    }

    // PHP 5 class Template { private $args; private $file;

    public function __get($name) {
        return $this->args[$name];
    }
    
    public function __construct($file, $args = array()) {
        $this->file = $file;
        $this->args = $args;
    }
    
    public function render() {
        include $this->file;
    }
    

    }

Sign In or Register to comment.