在 PHP 中渲染视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14143865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:44:53  来源:igfitidea点击:

Render a view in PHP

phpmodel-view-controllerview

提问by prometheus

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.

我正在编写我自己的 MVC 框架并且已经来到了视图渲染器。我将控制器中的 vars 设置为 View 对象,然后通过 echo $this->myvar 在 .phtml 脚本中访问 vars。

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

在我的 default.phtml 中,我调用方法 $this->content() 来输出视图脚本。

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' ;
    }

}

回答by Martin Samson

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'); ?>

回答by David Ericsson

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; ?>