CodeIgniter Forums
dont get js and css from MY_Controller (as in wiki) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: dont get js and css from MY_Controller (as in wiki) (/thread-56652.html)



dont get js and css from MY_Controller (as in wiki) - El Forum - 01-09-2013

[eluser]Jan_1[/eluser]
What do I have to do more to get stylesheet-data in my page?
The folderpath seems to be right. Who is working with the MY_Controller from codeigniter github wiki and could give me a hint.

Thanks!!

Code:
/**
* This library extends the default controller and adds some useful extra features.
* Features:
*         1. Enables the usage of master page/templates.
*         2. Can add css and javascript files.
*         3. Uses the _output method for controling the controllers output.
*         4. Can load relative content with in the form of modules (views)
*/
class MY_Controller extends CI_Controller {
    /* Output control constants */
    const OUTPUT_TEMPLATE = 10;
    const OUTPUT_NORMAL = 11;
    /* Private properties */

    //the default template
    private $template_view = 'default';
    
    //the default public folder this means that content inside this folder will be accessed directly
    //without using the routing.
    //Note!!! This folder must be enabled in the .htaccess file.
    private $public_folder = 'assets/';
    
    //the default location for the templates inside the views folder this means (views/templates/)
    private $template_folder = 'templates/';
    
    //the default css location for the css files inside the $public_folder("public/" by default) (public/css/)
    private $css_folder = 'css/';
    
    //the default js location for the css files inside the $public_folder("public/" by default) (public/js/)
    private $js_folder = 'js/';
    
    //Inline scripting (Javascript)
    private $inline_scripting = '';

    private $modules = array(); //An array that contains the modules output.

    private $charset = 'utf8'; //The page charset

    private $title = 'Testpage'; //The page Title
    
    //Media files and data
    private $media = array('css'=>array(),
                            'js'=>array(),
                             //meta tags
                            'meta'=>array(  'viewport'      =>  'width=device-width, initial-scale=1.0',
                                            'robot'         =>  'noindex,nofollow',
                                            'description'   =>  'just a testpage'
                                            ),
                             //RDF are 3rd genreration meta tags
                            'rdf'=>array());
    //The requested controller
    protected $controller = '';
    //The requested method to be called
    protected $method        = '';
    
    private $_output_data = array();

    /**
     * The MY_Controller constructor method.
     */
    function __construct(){
        parent::__construct();
                
        //Initializing the controller
        //Get the default charset from the config file.
        $this->charset = $this->config->item('charset');

        //Set the default mode to use a template view
        $this->_setOutputMode(self::OUTPUT_TEMPLATE);

        //Passing as properties the controller and the method that should be called.
        $this->controller = $this->uri->rsegment(1);
        $this->method      = $this->uri->rsegment(2);
    }
Code:
/**
     * CodeIgniter magic method that controls the output of a controller.
     * You can't call it directly.
     *
     * @see http://codeigniter.com/user_guide/general/controllers.html#output
     * @final this method cannot be overloaded
     * @param string $output the controller output
     * @return void
     */
    final function _output($output){
        switch($this->mode){
            //Use the template
            case self::OUTPUT_TEMPLATE:
                $data = array(    'meta'=>$this->media['meta'],
                                'rdf'=>$this->media['rdf'],
                                'js'=>$this->media['js'],
                                'css'=>$this->media['css'],
                                'title'=>$this->title,
                                'charset'=>$this->charset,
                                'output'=>$output,
                                'modules'=>(object)$this->modules,
                                'inline_scripting'=>$this->inline_scripting);
                
                //Merge the data arrays
                $data = array_merge($data, $this->_output_data);
                
                //Load the final output
                $out = $this->load->view($this->template_folder . $this->template_view, $data, TRUE);
                
                //The End
                echo $out;
            break;
            //or just echo output.
            case self::OUTPUT_NORMAL:
            default:
                echo $output;
            break;
        }
    }
Code:
/**
     * Adds a CSS file into the template.
     *
     * @access protected
     * @param string $file a css file located inside the public/js/ folder or an url.
     * @param boolean $custom_url Default FALSE. A flag to determine if the given $file is an url or just a file insite the public/js/ folder.
     * @return bool
     */
    public function _add_css_file($file, $custom_url=false){            
        if(!$custom_url){
            
            $filepath = $this->public_folder . $this->css_folder . str_replace('.css','',$file) . ".css";

            if(!$this->_is_file_exists($filepath)){
                show_error('Cannot locate css file: <tt><a href="'.$filepath.'">'.$filepath.'</a></tt>');
                return false;
            }
            
            $filepath = base_url() . $filepath;
        }else{
            $filepath = $file;
        }

        if(array_search($filepath, $this->media['css']) === false)
            $this->media['css'][] = $filepath;
        else return false;
        
        return true;
    }