Welcome Guest, Not a member yet? Register   Sign In
Redundancy on HTML
#1

[eluser]Unknown[/eluser]
Dear members of this community,

Since i`m new to CI, i`m not sure whether im approaching my project right. But before i start, i would like to point out that i enjoy working with CI!

Anyway, i have a question regarding semi static HTML output. For instance is it possible to have a static HTML document which loads automatically?

Because, obviously have some static html pages which have to be loaded every time. And since i have to run $this->load->view(); on every function, i wondered if there is a way to 'bypass' this.

My doctype, my meta and css styles dont have to called everytime right? I was wondering if there could be some sort of MAIN controller which could load everytime and within this main controller, you can give some parameters regarding the page you need. This would reduce code and im pretty sure it`s a feature somewhere within CI, or otherwise a certain approach to it, but i can`t figure it out.

So if any of you could help me understand how it works, i would really appreciate it!

With kind regards,
James Lewis
#2

[eluser]kyleect[/eluser]
http://www.google.com/search?client=safa...8&oe=UTF-8

Create "MY_Controller" and put the code in there. Now, here is the way I did it because I needed a way to dynamically load css and JS. All you have to do is set $this->views['content'] with name of the view you want to load. This should give you the basic jist and a jumping off point.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* MY_Controller
*
* Consider this the application controller.
*
* @package Firestarter
* @subpackage Core Extension
* @author Kylee Tilley <[email protected]>
*/

// ------------------------------------------------------------------------

/**
* @package Firestarter
* @subpackage Core Extension
**/

/* TODO:

    I need to create an assest management library so clean up the code in $this->embed_assests
    Remove all HTML related code and move it to MY_Front_End_Controller

*/

/*

    NOTE: Using this controller will change how you are using views.
    You will need to load a view in to a variable called $this->views['content']

*/

class MY_Controller extends Controller
{
    
    protected $views; //This is where the views are loaded. They are stored as strings.
    protected $view_data; //This is data that the view will use directly.
    protected $assests;
    
    public function __construct()
    {
        parent::Controller();
        
        $this->config->load('application', TRUE);
        
        
        
        //SET DEFAULT VIEW VARIABLES
        $this->view_data['page_title'] = 'Home';
        $this->view_data['application'] = $this->config->item('application');
            //To help standardize page specific CSS, we are setting the body id of the page to "controller__method"            
        $this->view_data['body_id'] = ($this->router->method == 'index') ? ($this->router->class) : ($this->router->class).'__'.(($this->router->method));


        
        //SET DEFAULT ASSESTS
        
        /* ---- Stylesheets ----
        
            In order to include a stylesheet on to the page, you must define it in the $this->assests['css'] variable.
            
            By default, you will set just a single string. You can set an array if you need to set options like media type.
            
            EXAMPLE:
            
                $this->assests['css'][] = array('print', media="print");
        
        */
        $this->assests['css'] = array('main');
        $this->assests['javascript'] = array();



        //LOAD VIEWS
        //The header and footer views have to be loaded in to variables so they can be outputted on the controllers destruction
        $this->views['header'] = $this->load->view('header', $this->view_data, TRUE);
        $this->views['footer'] = $this->load->view('footer', $this->view_data, TRUE);
        $this->views['content'] = NULL;    
    }
    
    private function embed_assests()
    {
        $stylesheets = NULL;
        $javascripts = NULL;

        //STYLESHEETS

        if(!empty($this->assests['css']))
        {            
            foreach($this->assests['css'] as $stylesheet)
            {
                if(is_array($stylesheet))
                {
                    if(isset($stylesheet['media']))
                    {
                        $stylesheets .= '&lt;link rel="stylesheet" href="'.($this-&gt;config->item('assests_dir', 'application')).'css/'.$stylesheet[0].'.css" media="'.$stylesheet['media'].'" type="text/css">'."\n";
                    }
                    else
                    {
                        $stylesheets .= '&lt;link rel="stylesheet" href="'.($this-&gt;config->item('assests_dir', 'application')).'css/'.$stylesheet.'.css" type="text/css">'."\n";
                    }
                }
                else
                {
                    $stylesheets .= '&lt;link rel="stylesheet" href="'.($this-&gt;config->item('assests_dir', 'application')).'css/'.$stylesheet.'.css" type="text/css">'."\n";
                }
            }
        }



        //JAVASCRIPT

        if(!empty($this->assests['javascript']))
        {    
            foreach($this->assests['javascript'] as $javascript)
            {
                $javascripts .= '[removed]config->item('assests_dir', 'application')).'javascript/'.$javascript.'.js" type="text/javascript">[removed]'."\n";
            }
        }



        $this->views['header'] = str_replace('#load_css#', $stylesheets, $this->views['header']);
        $this->views['header'] = str_replace('#load_javascript#', $javascripts, $this->views['header']);    
    }
    
    public function __destruct()
    {    
        $this->embed_assests();
        
        //We need to determine if the content view has already been set
        if($this->views['content'] == NULL)
        {
            if(($this->router->class == $this->router->routes['default_controller']) OR ($this->router->method == 'index'))
            {
                $this->views['content'] = $this->router->class;    
            }
            else
            {
                $this->views['content'] = $this->router->class.'/'.$this->router->method;
            }
        }
        
        $this->views['content'] = $this->load->view($this->views['content'], NULL, TRUE);

        echo $this->views['header'].$this->views['content'].$this->views['footer'];
    }
}
?&gt;
#3

[eluser]jedd[/eluser]
Extending the core controller using MY_Controller is my preferred approach, too.

I have waxed lyrical on the subject [url="http://codeigniter.com/wiki/Header_and_Footer_and_Menu_on_every_page_-_jedd/"]in the wiki[/url].

Unsurprisingly this question pops up every few minutes, and there's a few other ways you can approach this problem.
#4

[eluser]JoostV[/eluser]
You can also create a main layout template containing all the standard stuff. Then you let this main template load a specific subtemplate that you specify in the controller.

I like this approach because it doesn't clutter my controllers with view-related stuff.

Controller:
Code:
function user ()
{
    // Get some data
    $data['user'] = $this->user->get_user(12);
    
    // Set subview and load main view
    $data['subview'] = 'user';
    $this->load->view('main_template', $data);
}

Main view:
Code:
&lt;?php header('Content-type: text/html; charset=utf-8'); ?&gt;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;!-- Et cetera --&gt;

&lt;?php
if (isset($subview)) {
    $this->load->view($subview);
}
?&gt;

&lt;!-- Et cetera --&gt;
&lt;/body&gt;
#5

[eluser]überfuzz[/eluser]
I say My_controller. Mine fetches system data, example; css, meta, pathways to script, etc using methods that merely points out the config file. By the way right after I did the first setup I saw that theres heaps of helpers doing this kind of stuff. >:-( So then the model got toned down, or should I say tuned in the right key... Just saying, so you don't make the same mistake. ;-)

I alter standard viewers with custom. So a bunch of controllers might be loading plain_text.php and others like controller news loads view news. But all the view-files are loading html_head, html_foot, maybe menu.php etc.
#6

[eluser]JoostV[/eluser]
Well, I can certainly see an advantage to having specific viewdata handy in your controller. You could for instance promt loading controller-specific stylesheets or javascript files/scripts from you controller. Similar to the headLink(), headScript(), etc functionalities in Zend_View.
Code:
$this->view['assets']['meta']['keywords'] .= 'some,extra,keywords';
array_unshift ($this->view['assets']['script'], 'jscripts/jquery.js');




Theme © iAndrew 2016 - Forum software by © MyBB