Render a view in PHP

2019-04-07 10:42发布

问题:

I am writing my own MVC framework and has come to the view renderer. I am setting vars in my controller to a View object and then access vars by echo $this->myvar in the .phtml script.

In my default.phtml I call the method $this->content() to output the viewscript.

This is the way I do it now. Is this a proper way to do that?

class View extends Object {

    protected $_front;

    public function __construct(Front $front) {
        $this->_front = $front;
    }

    public function render() {                
        ob_start();
        require APPLICATION_PATH . '/layouts/default.phtml' ;            
        ob_end_flush();
    }

    public function content() {
        require APPLICATION_PATH . '/views/' . $this->_front->getControllerName() . '/' . $this->_front->getActionName() . '.phtml' ;
    }

}

回答1:

Here's an example of how i did it :

<?php


class View
{
private $data = array();

private $render = FALSE;

public function __construct($template)
{
    try {
        $file = ROOT . '/templates/' . strtolower($template) . '.php';

        if (file_exists($file)) {
            $this->render = $file;
        } else {
            throw new customException('Template ' . $template . ' not found!');
        }
    }
    catch (customException $e) {
        echo $e->errorMessage();
    }
}

public function assign($variable, $value)
{
    $this->data[$variable] = $value;
}

public function __destruct()
{
    extract($this->data);
    include($this->render);

}
}
?>

I use the assign function from out my controller to assign variables, and in the destructor i extract that array to make them local variables in the view.

Feel free to use this if you want, i hope it gives you an idea on how you can do it

Here's a full example :

class Something extends Controller 
{
    public function index ()
    {
    $view = new view('templatefile');
    $view->assign('variablename', 'variable content');
    }
}

And in your view file :

<?php echo $variablename; ?>


回答2:

Example of a simple view class. Really similar to yours and David Ericsson's.

<?php

/**
 * View-specific wrapper.
 * Limits the accessible scope available to templates.
 */
class View{
    /**
     * Template being rendered.
     */
    protected $template = null;


    /**
     * Initialize a new view context.
     */
    public function __construct($template) {
        $this->template = $template;
    }

    /**
     * Safely escape/encode the provided data.
     */
    public function h($data) {
        return htmlspecialchars((string) $data, ENT_QUOTES, 'UTF-8');
    }

    /**
     * Render the template, returning it's content.
     * @param array $data Data made available to the view.
     * @return string The rendered template.
     */
    public function render(Array $data) {
        extract($data);

        ob_start();
        include( APP_PATH . DIRECTORY_SEPARATOR . $this->template);
        $content = ob_get_contents();
        ob_end_clean();
        return $content;
    }
}

?>

Functions defined in the class will be accessible within the view like this:

<?php echo $this->h('Hello World'); ?>