How do I create a nested view within a nested view? |
@Dave friend, in the older versions of the CodeIgniter documentation it was
not documented, thanks for clarifying this, guess I need to sit down this weekend and read the whole Users Guide again. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Thanks. I'm trying to implement a strategy from the several good suggestions I've read here. I now have:
Controller and page master view lib_page_view: PHP Code: $data3['tunes']=$this->M_tunes->getAllTuneData(); In sub view file tunes_sv.php: PHP Code: $this->load->view('partials/topper_ssv'); In sub, sub view file partials/topper_ssv: PHP Code: <div class="panel panel-tunes"> I've tried several variations but always get error: Severity: Notice Message: Undefined variable: tune Filename: partials/topper_ssv.php Line Number: 3 Any idea what dumb thing I'm doing wrong? (09-26-2017, 03:54 PM)codeguy Wrote: Thanks for the encouraging reply. But I am confused that I see no data being transferred to your views. Like I'd expect something like . . Sorry I missed that post of yours. Any data is passed to the very first view. Views themselves still check to see if the data exists and if not deals with is smoothly, so there is no chance of errors showing if data is NULL or missing. I recall that I used to pass data to all the views separately under the mistaken belief that the data passed to a view was removed once the view was processed. When I discovered this was not the case I carried on for a while passing view data manually to each view. After a while though it dawned on me what a waste of time this was. Now I simply load all the data into one variable and pass it to the very first view. I probably should load it into the view vars as described earlier by someone, but I have never had a problem passing it all the the header view, and having it all available to all other views. My first view is always the header_view with the HTML, additional css, meta tags etc. The next is usually the main_menu_view with navigation and active states on the current page options etc. My page layouts I do in my own template library and set the page_data variables in my controller with library setters etc. In fact, I now quite like that the view data is not deleted, and am hoping this will not change in CI4. This approach makes changing the site template or design very easy to do, as I am only dealing with HTML layouts and css, never logic or data processing of any sort. Hope that helps, Paul. PS I am very careful though to only pass the data that is required. So I always try to define the fields in my models using select statements rather than the quicker but dirtier method of relying on the fact that the query builder, if not told, will select all by default. Those are called by libraries that format the dates, currencies, etc. Finally any fields that need cleaning are done in the libraries, so the view files are as clean of php as possible, except of course for loops and basic if statements.
Just saw your latest post and I have to say I do not like the approach. It looks like you are loading all your views into vars, and then outputting them in a 'page_master' type view.
What about something like this: Template Library: libraries/Template_library.php PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Controller controllers/Home.php PHP Code: <?php Because the template library loads all the data into the first view, it is available in all subsequent views. So the home view file might load partials/show_blog_articles_view within a loop through that data. (After checking of course that there are some to loop through). No page views are loaded into memory. All view output is done at the end of all the other processing. The controller just collects the data and sets the views. The libraries (in this case blog_library, collects from the database via a blog model, cleans and validates the output (ie is a blog post public etc). All the view data is available to all the views called, however many view partials I want, because all the data is loaded into the first view. There is then no chance of conflicts or overwriting etc. Should I want to change my page layout, I simply alter my template library or any associated views. I hope that helps. PS examples given are simple examples to get the point across. PPS I should just add that CI does not tell you how to do it, so whatever suits you is fine. There is no right answer here. Whatever you are comfortable with. For a long time I just loaded several views at the end of each controller. Part of the beauty of CI is you can do it in whatever way you want. (09-28-2017, 10:50 AM)PaulD Wrote: Just saw your latest post and I have to say I do not like the approach. It looks like you are loading all your views into vars, and then outputting them in a 'page_master' type view.@PaulD Thanks much for this. As soon as I click SEND on this I'm walking to my favorite coffee shop with my tablet and notepad to absorb it and reread your previous replies - to digest them to the point where I can redo my code using these ideas. I'll let you know soon how it goes.
@PaulD Did a lot of reading yesterday. Can't understand why something as conceptually as simple as a template for a view can be so obscure in the CI context. Anyway, this AM I tried to just copy/past code from your posts, adapted to my environment. My home controller looks like this now:
PHP Code: <?php PHP Code: $this->template_library->set_page_data('page_title', 'MZ Library'); Error Message: "Undefined property: C_home::$template_library" The template_library file in aopplication/libraries is: PHP Code: <?php if ( !defined('BASEPATH')) {exit('No direct script access allowed');} Any suggestions?
(09-30-2017, 12:57 PM)dave friend Wrote: Have you loaded template_library? Right. Stands to reason that after creating new files and editing some existing ones to do this new thing - use a template in CI - I also need to load the new template _library file that does the magic. Thank you. Fixed it. It seems to be attempting to load the view now but there's something about a "tune" variable not being defined - which coincidentally was the same problem I had before I started this plea for help. But now I think I might be able to figure it out from here - thanks to the great advice from you guys. Fingers crossed. |
Welcome Guest, Not a member yet? Register Sign In |