Welcome Guest, Not a member yet? Register   Sign In
What actually goes in YOUR header?
#1

I understand the concept of having a header to stop duplicate code. What I have so far though, I'm not sure what will be duplicated in a header. The title of the page will be different each time, the js files and css files will be different each time. So what do you reuse? And how to you deal with things like css and js files that vary?

Also, somewhat sort of related. Is it ok to load a view in another view like so

Code:
<!DOCTYPE html>
   <html>
       <head>
           <meta charset="UTF-8">
           <title>User Home</title>
       </head>
       <body>
           <?php $this->load->view('nav_view');?>
       </body>
   </html>
Reply
#2

Why not try put this in your controller, $this->load->view('header'); $this->load->view('body'); $this->load->view('footer'); You can't $this->load->view('blabla'); in another view.
Keep calm.
Reply
#3

(01-27-2016, 11:15 AM)arma7x Wrote: Why not try put this in your controller, $this->load->view('header'); $this->load->view('body'); $this->load->view('footer'); You can't $this->load->view('blabla'); in another view.

Not that I've tried it (I use Smarty for this kind of thing) but I think he can do this kind of logic as long as he gets the CI super object instance with get_instance() function, somewhere on the top section of his view.

I didn't test that though, and probably is not that simple. But the logic is that. Just saying... \_(-.-)_/
Best regards,
José Postiga
Senior Backend Developer
Reply
#4

Why getting get_instance() in view, it already has CI super object.
Keep calm.
Reply
#5

Codeigniter Template example
http://forum.codeigniter.com/thread-6379...#pid326426

same example that is also calling a model in the template for dynamic headers
http://stackoverflow.com/questions/34909...4#34911214
Reply
#6

"Why not try put this in your controller, $this->load->view('header'); $this->load->view('body'); $this->load->view('footer'); You can't $this->load->view('blabla'); in another view."

Because I don't want to be calling 3 or 4 views from my controller over and over again. And you can load a view from another view. At least it works when I try it. I just wasn't sure if it's a good idea.

Thanks for the links Cartalot.
Reply
#7

(This post was last modified: 01-28-2016, 09:14 AM by RobertSF.)

(01-28-2016, 01:43 AM)DreamOfSleeping Wrote: "Why not try put this in your controller, $this->load->view('header'); $this->load->view('body'); $this->load->view('footer'); You can't $this->load->view('blabla'); in another view."

Because I don't want to be calling 3 or 4 views from my controller over and over again. And you can load a view from another view. At least it works when I try it. I just wasn't sure if it's a good idea.

It's perfectly fine to load a view from another view. It works, and the documentation shows an example, if I remember right. I happen to use the same header. If something in it is variable, I just pass the value to it in a variable. So every view of mine goes like this.
PHP Code:
$this->load->view('header');

<!-- 
html code -->
// some php code

$this->load->view('footer'); 

I can even have subviews. For example, you might have some kind of table, and below it a form that can be an add, edit, or delete form. I put the table in the view, and from that view, I load the desired subview, which is the form that adds, edits, or deletes.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#8

Thanks Robert. I think your example is what I'm going to go with. Looking up a lot of articles about this subject, and some people seem to frown upon the loading the header inside another view each time. Mainly because they class it as dupilicate code. But I think repeating one line of code at the top and bottom of a view is hardly anything to worry about.

Today I wasted my time making my own way of templating. This is what I came up with. I'm not going to use it though.

Code:
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Loader extends CI_Loader {

   private $templates = array(
                             'standard' => array('common/header_view', 'common/nav_view', '?', 'common/footer_view'),
                             'no_nav' => array('common/header_view', '?', 'common/footer_view')
                             );
   
   public function template($body, $data = array(), $template_name = 'standard', $return = FALSE) {
       $template = $this->templates[$template_name];
       $content = "";
     
       foreach($template as $view_path) {
           if ($view_path == '?') {
               $view_path = $body;
           }
           
           if ($return) {
               $content .= $this->view($view_path, $data, true);
           } else {
               $this->view($view_path, $data);
           }
       }
       
       return $content;
   }
}
Reply
#9

(This post was last modified: 01-28-2016, 04:44 PM by RobertSF.)

(01-28-2016, 12:51 PM)DreamOfSleeping Wrote: Thanks Robert. I think your example is what I'm going to go with. Looking up a lot of articles about this subject, and some people seem to frown upon the loading the header inside another view each time. Mainly because they class it as dupilicate code. But I think repeating one line of code at the top and bottom of a view is hardly anything to worry about.
You bet, man, and you're right. DRY -- don't repeat yourself -- is a good idea but not a religious commandment. Besides, let's look at the different ways of doing this (just the headers and footers views).

You could include the header and footer code in each view. Result? Massive code duplication.
 
You could load header and footer from the controller.
PHP Code:
$this->load->view('header');
$this->load->view('body');
$this->load->view('footer'); 
Result? Less code duplication, but you have to repeat this code in every controller.

You could load header and footer from the body view itself. Result? The least code duplication of all three options. You can forget entirely about the header and footer once you put it in your views. Is there some code duplication? Of course. It's not possible to eliminate code duplication entirely.

I haven't paid attention to templating, but here's how I do it. In the controller,
PHP Code:
public function index()
{
    
$this->form_validation->set_rules('search[name]''Search Name''required');
    
$this->form_validation->set_rules('search[site_id]''Site''required');
    
$this->form_validation->set_rules('search[url]''Search URL''required');
    if (
$this->form_validation->run())
    {
        
$data $this->input->post('search'true);
        
$search_id $this->searches_model->add_search($data);
        
redirect();
    }
    
$data = array(
        
'subview' => 'search_add_view',
        
'sites' => $this->sites,
        
'searches' => $this->searches
    
);
    
$this->load->view('searches_view'$data);

Ok, so this is an application that lets you save web searches and execute them later. The main view shows an HTML table of saved searches, and below it, by default, you can add another search to the saved searches. So I assign 'search_add_view' to $data['subview'] and send that to the main view ('searches_view').

Over in the view searches_view, we have this (full code),
PHP Code:
<?php $this->load->view('header'); ?>

  <div id="content">

    <h2>Saved Searches</h2>

<?php
    
if (count($searches) == 0)
    {
        echo 
'<p>There are no saved searches.</p>';
    }
    else
    {
        
$this->table->set_heading('Search Name''Site''Search URL''Edit''Delete''Execute');
        foreach (
$searches as $search)
        {
            
$this->table->add_row(
                
$search['name'], $search['site_name'], $search['url'],
                
anchor('searches/edit/' $search['id'], 'Edit'), 
                
anchor('searches/delete/' $search['id'], 'Delete'),
                
anchor('searches/execute/' $search['id'], 'Execute')
            );
        }
        echo 
$this->table->generate();
    }
?>

<?php $this->load->view($subview); ?>

    </div><!-- content -->

<?php $this->load->view('footer'); ?>
This view receives the associative $data array and converts its elements into variables, a neat little trick I've not seen in other languages, so now $data['subview'] becomes $subview, which the view loads after the table.

If you'd like to see the entire application, which is not complex but which uses a few advanced CodeIgniter features, like driver libraries, download the whole thing at https://github.com/RobertHallsey/search-scraper
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#10

Thanks. I will check out your application. It will be useful to see a full working example like that.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB