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

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" ?>
...
<?php include "../template/core/template_bottom.php" ?>

And i got this error : 
Code:
Message: include(../template/core/template_top.php): failed to open stream: No such file or directory
...
Message: include(): Failed opening '../template/core/template_top.php' for inclusion (include_path='.:/Applications/XAMPP/xamppfiles/lib/php')

Why ? My path is right, no ? 
I'm lost. 
Can anyone help please ?
Thank you !
Reply
#2

(This post was last modified: 05-12-2016, 09:56 AM by Wouter60.)

You can load another view inside a view.

View: news/add.php
PHP Code:
<?php $this->load->view('template/header');?>
...
...
<?php $this->load->view('template/footer');?>

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) {
$this->load->view('template/header');
$this->load->view($view,$data);
$this->load->view('template/footer');


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 
Reply
#3

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 ?
Reply
#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
#5

An advice ?
Reply
#6

(This post was last modified: 05-17-2016, 12:37 PM by Wouter60.)

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_stylesheetbase_url() . 'assets/css/example.css');
$this->assets->add_javascriptbase_url() . 'assets/js/example.js'); 

In my header view I have this:
PHP Code:
<head>
<?= 
$this->assets->output_stylesheets();?>
<?= $this
->assets->output_javascripts();?>
</head> 

The method output_stylesheets in the library is like this:
PHP Code:
public function output_stylesheets()
{
    if (empty(
$this->stylesheets)) return NULL;
    
$str '';
    foreach(
$this->stylesheets AS $item){
        
$str .= '<link rel="stylesheet" href="' $item '" type="text/css" />' "\n";
    }    
    return 
$str;


For me, this is as flexible as I need it.
Reply
#7

(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 Sad
Reply




Theme © iAndrew 2016 - Forum software by © MyBB