DooPHP - fastest MVC based PHP framework

Learn More

DooPHP View

The definitive guide to DooPHP

Discussions

Have a question? Post it at the forum.

DooPHP View

A view is simply a web page or output such as RSS and Atom feeds. In DooPHP view files are located in SITE_PATH/protected/view and SITE_PATH/protected/viewc.

View files are never called directly, they must be loaded by a Controller. In MVC, we should have view to display data in a nice format while having Controller to handle the application logic.


Using Template Engine

If you would like to use the built-in template engine to write your views, simply mockup a Html and put it in the view folder. The template file needs to be end with the extension name .html

To load a template file from the Controller, use render():

HTML template (mytemplate.html):

<html<html>
<head>
    <title>{{title}}<title>
</head>

<body>
    <p>{{content}}</p>
</body>
</html>

Load the HTML template in Controller:

class MyController extends DooController{
    function test(){
        $data['title'] = 'Hi This is Title!';
        $data['content'] = 'Some content here...';
        
        //Both syntax can be used:
        //$this->view()->render('mytemplate', $data);

        $this->render('mytemplate', $data);
    }
}

Using PHP for View

If you don't like template engines, you can write native PHP as view files. The PHP template file should be put at viewc instead of view folder.

To load a PHP view from the Controller, use renderc():

The PHP template (mytemplate.php):

<html>
<head>
    <title><?php echo $this->data['title']; ?><title>
</head>

<body>
    <p><?php echo $this->data['content']; ?></p>
    <p>If controller access enable: <br/>
       <?php echo $this->siteName; ?>
       <?php $this->getStuff(); ?>
    </p>
</body>
</html>


Load the native PHP template in Controller:

class MyController extends DooController{
    
    public $siteName = 'My Super Site';

    function getStuff(){
        echo 'Super stuffs!';
    }
    
    function test(){
        $data['title'] = 'Hi This is Title!';
        $data['content'] = 'Some content here...';

        //Simply load and render the template
        $this->renderc('mytemplate', $data);        
        
        //If you want View to access Controller properties and method
        $this->renderc('mytemplate', $data, true);
    }
}

View the Template engine demo's Example page for a list of syntax.