【问题标题】:Render a view in PHP在 PHP 中渲染视图
【发布时间】:2012-12-18 02:35:19
【问题描述】:

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

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

这就是我现在的做法。这是一个正确的方法吗?

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

}

【问题讨论】:

  • 如果我不知道其他人algorithm 做某事,那么它是最好的。
  • 对我来说似乎很好。如果文件存在,您可以检查方法 content(),但如果您是唯一使用该脚本的人,则可以将其省略。

标签: php model-view-controller view


【解决方案1】:

简单视图类的示例。与您和 David Ericsson 的非常相似。

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

?>

类中定义的函数可以在视图中访问,如下所示:

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

【讨论】:

    【解决方案2】:

    这是我如何做到的一个例子:

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

    我使用控制器中的 assign 函数来分配变量,并在析构函数中提取该数组以使其成为视图中的局部变量。

    如果你愿意,可以随意使用它,我希望它能让你知道如何做到这一点

    这是一个完整的例子:

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

    在你的视图文件中:

    <?php echo $variablename; ?>
    

    【讨论】:

    • 使用对象析构函数来触发渲染对于使用您的框架的其他开发人员来说会非常混乱。通常析构函数会清理资源,而不是创建更多资源。
    • 你说的完全正确,但是这是我个人的框架,仅供我自己使用,它可能不是最好的解决方案,但它确实有效:p
    • 是的:)。我个人做的是输出缓冲并返回一个字符串。我会用视图类发布答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 2014-04-27
    • 2015-02-04
    相关资源
    最近更新 更多