问题
What is the difference between view and render in codeigniter?
回答1:
some template libraries use $this->template->render();
to output the rendered content based on your template. (you must have the template library installed obviously)
function index()
{
$this->template->set_template('main_template');
$data['content'] = 'hello this is my content';
$this->template->write_view('content', $data);
$this->template->render();
}
is in effect the same as
function index()
{
$data['content'] = 'hello this is my content';
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');
}
template libraries save the need to load each partial view every time.
回答2:
Render is not an out-of-the-box Codeigniter function for loading view files. Render is primarily used by Codeigniter template libraries like Collin William's Template Library or Phil Sturgeon's Template Library.
The following two methods for view files are supported by Codeigniter right in the core code without using third party libraries or core extensions.
$this->load->view()
The standard and most used way of loading a view file. Does not support any fancy syntax except alternative control syntax structured PHP code or standard PHP code.
$this->parser->parse()
Using the in-built Codeigniter parser which supports a Smarty like syntax only not as powerful. Also allows you to code view files using standard PHP and HTML as well.
来源:https://stackoverflow.com/questions/4758978/what-is-the-difference-between-view-and-render