What does it mean to “render a view”? What the pro

2019-07-19 04:11发布

问题:

I can't understand what is "rendering a view". I'm reading Zend Framework manual and there are a lot of usage of this term.

Automatically rendering views:

This helper takes care of injecting the view object into the controller, as well as automatically rendering views.

Disable rendering for a view:

You can also simply disable rendering for an individual view

render() renders a view script.

render() renders a view script.

Render that template in the subdirectory

Passing a value for $action will render that template in the /[controller]/ subdirectory.

And so on.

I found the render() method. I fact many component classes of Zend_Controller have render() method. But all of them at long last anyway call only the one from the Zend_View_Abstract class. And the only thing I could see in its realization is that it seems it just puts the html-script into the output buffer:

public function render($name) {

        // find the script file name using the parent private method
        $this->_file = $this->_script($name);
        unset($name); // remove $name from local scope

        ob_start();
        $this->_run($this->_file);

        return $this->_filter(ob_get_clean()); // filter output
    }

Unfortunately there is no realization of the _run() method and I don't know what it has to do, how to include script in a scope. Is this what is meant by include - include_once("/myScritp.php")?

/**
 * Use to include the view script in a scope that only allows public
 * members.
 *
 * @return mixed
 */
 abstract protected function _run();

So what does it mean to "render a view"? Just to include the script and put it into the output buffer and get it in some variable (return ob_get_clean()) then?

回答1:

Zend Framework is based on MVC design pattern which stands for Model View Controller. The View is presentation part including html, javascript, css or any other aesthetics.

Rendering a view means showing up a View eg html part to user or browser. Let's say you have a controller for About page of your site, now from your controller you would render the About view which means show the that page in browser for users to see otherwise if you don't users will see just blank page :)

Think of it echoing html/css/js to browser.



回答2:

A View is part of an MVC framework, which is what Zend is. Think of the View as the output to the user. The Controller handles actions and routing, in a sense, and the model handles mostly database interactions. The View sets up variables and sends them to the HTML output when the page is rendered.

So by rendering a view, the MVC framework has handled the data in the controller and done the backend work in the model, and then sends that data to the View to be output to the user.



回答3:

render just means to emit. To print. To echo. To write to some source (probably stdout).



回答4:

Say, we are talking about HTML:

Rendering is the process of filling a template with living data from your application, replacing placeholders and simple logic (like if-else statements or loops) resulting in a valid html file.