Issue with include |
Hi,
I have an issue with an include() in my views. Here is my project tree : In my News controller I have : PHP Code: ... $this->load->view('news/add'); And in my view news/add I have : Code: <?php include "../template/core/template_top.php" ?> And i got this error : Code: Message: include(../template/core/template_top.php): failed to open stream: No such file or directory Why ? My path is right, no ? I'm lost. Can anyone help please ? Thank you !
You can load another view inside a view.
View: news/add.php PHP Code: <?php $this->load->view('template/header');?> This way, though, you get a lot of repeating code. Because every view must embed a header and footer. An alternative way is make a function in MY_Controller (that extends the CI_Controller): PHP Code: public function _render_page($view, $data) { Now, from your controller, you can do this: PHP Code: $this->_render_page('news/add', $data); //data is an array with variables you want to use in the view
Thanks that's look great !
And how to do if I want to add a different <title> and different css links in my pages ? Can we do that with this way ?
I have done this :
MY_Controller : PHP Code: class My_Controller extends CI_Controller Controller News : PHP Code: class News extends MY_Controller { My template folder in views : - header - head_elements - footer header : Code: <!DOCTYPE html> head_elements : Code: <?php footer : Code: <footer> Explanation: 1) $headElements in MY_Controller has an index 'elements' because I want to call it in my head_elements template to test if it's avoid 2) I have separated header and head_elements in the template because it's rare to add css links or meta so I didn't want to pollute all my render function in my controllers with an array complicated like array( 'pageTitle' => 'Title', 'elements' => NULL) I dont think it's the best solution, how can I improve it ? Thanks for your help !
I have built my own Assets library. It's autoloaded in autoload.php, so it's available for all pages.
The library his public arrays like $stylesheets and $javascripts. And methods like add_stylesheet($path), and add_javascript($path). But also: output_stylesheets(), which just returns a string with all stylesheets in the appropriate format. In my controller, I do this: PHP Code: $this->assets->add_stylesheet( base_url() . 'assets/css/example.css'); In my header view I have this: PHP Code: <head> The method output_stylesheets in the library is like this: PHP Code: public function output_stylesheets() For me, this is as flexible as I need it.
05-17-2016, 05:41 PM
(This post was last modified: 05-17-2016, 05:52 PM by John_Betong. Edit Reason: Added condition )
I use many includes within my views also depending on passed parameters different HTML headers are loaded (a special one for AmpProject compatible pages.)
Recently CodeIgniter simplified includes by declaring a VIEWPATH constant in index.php. // view file common to many web-pages if( isset( $ampOk ) ): require VIEWPATH ."template/core/_header-amp.php"; else: // load non-amp version endif; Tediously tapped on a tablet ![]() |
Welcome Guest, Not a member yet? Register Sign In |