Query in CI Tutorial |
I am studying CI documents wherein I have a query as below in the Tutorial:
public function view($page = 'home') { if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php')) { // Whoops, we don't have a page for that! show_404(); } $data['title'] = ucfirst($page); // Capitalize the first letter $this->load->view('templates/header', $data); $this->load->view('pages/'.$page, $data); $this->load->view('templates/footer', $data); } In last 3 lines $data variable – how come same variable has 3 different values, that of content in header.php, footer.php & content of page.php file? Confusion in syntax/ usage of array $data[‘title’] & just $data.
For 3 different values, I assume you mean 'templates/header', 'pages/'.$page and 'templates/footer'?
This is because first attribute points to template file, so these three lines pass the same data to three different template files. For $data, views expect second parameter as array, so inside view file you can use array index as variable name to access that value: $data['title'] = 'aaa' becomes <?= $title ?> in view file. Or $data['myvalue'] could be accessed using $myvalue.
@Sovani Shreeniwas
Did you know that you could also do this... $this->load->view('templates/header', $data); $this->load->view('pages/'.$page); $this->load->view('templates/footer'); ...and it would still work?
(01-07-2019, 05:57 PM)php_rocs Wrote: @Sovani Shreeniwas Or, because in this example we do not ask "view" to return a string it could be written PHP Code: $this->load $data only needs to be passed to "view" once because "view" will save a copy of any data it is given.
(01-08-2019, 01:29 PM)dave friend Wrote:(01-07-2019, 05:57 PM)php_rocs Wrote: @Sovani Shreeniwas Thanks all for the replies. Trying to understand the inputs from you. |
Welcome Guest, Not a member yet? Register Sign In |