Welcome Guest, Not a member yet? Register   Sign In
Issue with include
#4

I have done this : 

MY_Controller : 
PHP Code:
class My_Controller extends CI_Controller
{
 
   public function render($view$pageTitle NULL$data NULL$headElements = ['elements' => NULL] )
 
   {
 
       $this->load->view('template/header'$pageTitle);
 
       $this->load->view('template/head_elements'$headElements);
 
       $this->load->view($view$data);
 
       $this->load->view('template/footer');
 
   }


Controller News : 
PHP Code:
class News extends MY_Controller {

 
   public function index()
 
   {
 
       $elements = [
 
           'elements' => [
 
               '<link rel="stylesheet" href="www.google.ch">'
 
           ]
 
       ];

 
       $this->render('news/index', ['pageTitle' => 'Hello'], ['texte' => 'blablabla'], $elements);
 
   }


My template folder in views : 
- header
- head_elements
- footer
header : 
Code:
<!DOCTYPE html>
<html>
   <head>
       <meta charset="utf-8">
       <title><?php echo $pageTitle ?></title>

head_elements :
Code:
<?php
   if( $elements != NULL)
   {
       foreach ( $elements as $element)
       {
           echo $element;
       }
   }
?>
</head>
<body>
<header>
   Header de mon site
</header>

footer : 
Code:
   <footer>
       Footer de mon site
   </footer>
</body>
</html>



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 !
Reply


Messages In This Thread
Issue with include - by kikk - 05-12-2016, 06:25 AM
RE: Issue with include - by Wouter60 - 05-12-2016, 09:55 AM
RE: Issue with include - by kikk - 05-13-2016, 12:12 AM
RE: Issue with include - by kikk - 05-13-2016, 06:34 AM
RE: Issue with include - by kikk - 05-17-2016, 12:40 AM
RE: Issue with include - by Wouter60 - 05-17-2016, 12:36 PM
RE: Issue with include - by John_Betong - 05-17-2016, 05:41 PM



Theme © iAndrew 2016 - Forum software by © MyBB