Destramic Posted August 17, 2018 Share Posted August 17, 2018 Hey guys I include my header to my pages by adding this to the top of the page $this->render ('myheader.html'); The problem is I want to pass results from my db to the navigation menu, which would require me to assign vars in each controller view that includes the header. What is the best way to do this please? Another way I can think off is assigning the results to my view in my bootstrap ready for the controllers...but I'm sure there is a more logical way. Thank you Quote Link to comment Share on other sites More sharing options...
requinix Posted August 18, 2018 Share Posted August 18, 2018 Depends on your framework. An easy solution is to allow for an array as a second argument to render(), perhaps $args, which the file could read from. Quote Link to comment Share on other sites More sharing options...
Destramic Posted August 18, 2018 Author Share Posted August 18, 2018 That is possible with my view when rendering but still I would need to pass vars on each page which seems unlogical and long winded Preloading vars in my bootstrap may be the best way to do this then in my case. Thank you Quote Link to comment Share on other sites More sharing options...
requinix Posted August 18, 2018 Share Posted August 18, 2018 What kinds of "vars" are you talking about? When do you know their values? When do they change? Quote Link to comment Share on other sites More sharing options...
maxxd Posted August 18, 2018 Share Posted August 18, 2018 What framework are you using? Quote Link to comment Share on other sites More sharing options...
Destramic Posted August 20, 2018 Author Share Posted August 20, 2018 I'm using my own framework that I built...I think it's pattern issues that I have. OK so in a typical page I'll load the header.html and footer.html with my content in between...now my header has a menu and I want to add result counts from my database. My header is also rendered on every page. Ie. Tasks (5) Just don't see a clear way of how this should be done. How is this typically done in the real world? Thank you Quote Link to comment Share on other sites More sharing options...
requinix Posted August 21, 2018 Share Posted August 21, 2018 Separate logic from presentation. Do all your work before you output the header. That's when you get your result count. Then you render the header, passing values into it such as the page title (which you aren't already doing?) which the page can format however it wants. $this->render('myheader.html', ['Title' => "Tasks ({$count})"]); Your framework passes that second argument to myheader.html, which will probably have to turn into a PHP script, and it grabs the Title value. Quote Link to comment Share on other sites More sharing options...
Destramic Posted August 23, 2018 Author Share Posted August 23, 2018 sorry requinix i think i may have no explained myself correcly, although you may understand what ive been trying to say. but i'll explain fully heres my layout on the left navigation you will see a count of 5 for the number of tasks. how my pages are put together <?php echo $this->render('header.html'); ?> login page <?php echo $this->render('footer.html'); ?> i could do what you said like so: <?php echo $this->render('header.html' array('task_count' => $user_tasks_count)); ?> login page <?php echo $this->render('footer.html'); ?> but this would require me to do this in every controller not just login public function login($task_id) { $user = $this->get_user(); $this->render(array( 'user_tasks_count' => $this->tasks->get_tasks_count($user->user_id()); )); } if this is how it should be done then so be it, just dont seem too practical...sorry to keep on. ps. feel free to tell me to shut up and move on thank you Quote Link to comment Share on other sites More sharing options...
requinix Posted August 23, 2018 Share Posted August 23, 2018 If you want it on every page then you should do it in the header... If the problem is bridging the divide between the header (some sort of view) and the task count (which apparently requires a controller) then that's a framework question. What sorts of things can you do with it? What can you not do with it? The way I would address this is by asking what the best way to do this should be, then do the work required to make that actually possible. Quote Link to comment Share on other sites More sharing options...
Destramic Posted August 23, 2018 Author Share Posted August 23, 2018 a controller for the header sounds good, but leaves me with the question how would i run two controllers or load the header controller into other controllers. what would be the best pattern/way please requinix? thank you for your patience Quote Link to comment Share on other sites More sharing options...
ignace Posted August 24, 2018 Share Posted August 24, 2018 1) Let the view do a sub-request to the header controller to load the menu. 2) Create a view helper that loads the menu. Both will require the current route to determine which hierarchy to load/show. Quote Link to comment Share on other sites More sharing options...
Destramic Posted August 25, 2018 Author Share Posted August 25, 2018 i like the idea of loading a controller view inside of the view it's self. so here it is... view method public function load($controller, $variables = array()) { $dispatcher = new Dispatcher($this->get_request(), $this->get_response()); if (!$dispatcher->is_dispatchable($controller)) { throw new Exception(sprintf('View: Unable to dispatch %s.', $controller)); } if (!empty($variables)) { self::$variables = array_merge(self::$variables, $this->escape_values($variables)); } return $dispatcher->dispatch()->get_view()->get_contents(); } then in my pages echo $this->load('template:common:header', array( 'heading' => 'Task', 'heading_description' => 'This is a quick view of your current task' )); thank you both for your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.