Welcome Guest, Not a member yet? Register   Sign In
Moving and renaming views folder
#1

[eluser]EEssam[/eluser]
Hello,

How can I move the views folder to the application root and rename the folder to: templates?

Thanks.
#2

[eluser]Thorpe Obazee[/eluser]
I don't know if this would cause problems but you could try and modify the Loader class and change the path of.

Code:
$this->_ci_view_path = APPPATH.'views/';
#3

[eluser]Dam1an[/eluser]
I don't think it does cause any problems...
Someone asked how to do it before and I said pretty much exactly the same as bargainph said, and didn't hear anything else on the issue, so assumed it worked lol
#4

[eluser]xwero[/eluser]
If you often need to switch between different view paths because the page is build with a mix of developer and user view files you could extend the loader like this
Code:
class MY_Loader extends CI_Loader {

var _ci_old_view_path = '';
var _reset_view_path = false;

function set_view_path($path,$reset = true)
{
   if(is_dir($path))
   {
      $this->_ci_old_view_path = $this->_ci_view_path;
      $this->_reset_view_path = $reset;
      $this->_ci_view_path = $path;
      return true;
   }

   return false;
}
/* if you used false as reset parameter in the set_view_path function */
function reset_view_path()
{
   if( ! empty($this->_ci_old_view_path))
   {
      $this->_reset_view_path = false;
      $this->_ci_view_path = $this->_ci_old_view_path;
      return true;
   }

   return false;
}
/* existing function altered for the new functionality */
function _ci_load($_ci_data)
    {
        // Set the default data variables
        foreach (array('_ci_view', '_ci_vars', '_ci_path', '_ci_return') as $_ci_val)
        {
            $$_ci_val = ( ! isset($_ci_data[$_ci_val])) ? FALSE : $_ci_data[$_ci_val];
        }

        // Set the path to the requested file
        if ($_ci_path == '')
        {
            $_ci_ext = pathinfo($_ci_view, PATHINFO_EXTENSION);
            $_ci_file = ($_ci_ext == '') ? $_ci_view.EXT : $_ci_view;
            $_ci_path = $this->_ci_view_path.$_ci_file;
        }
        else
        {
            $_ci_x = explode('/', $_ci_path);
            $_ci_file = end($_ci_x);
        }
        
        if ( ! file_exists($_ci_path))
        {
            show_error('Unable to load the requested file: '.$_ci_file);
        }
    
        // This allows anything loaded using $this->load (views, files, etc.)
        // to become accessible from within the Controller and Model functions.
        // Only needed when running PHP 5
        
        if ($this->_ci_is_instance())
        {
            $_ci_CI =& get_instance();
            foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
            {
                if ( ! isset($this->$_ci_key))
                {
                    $this->$_ci_key =& $_ci_CI->$_ci_key;
                }
            }
        }

        /*
         * Extract and cache variables
         *
         * You can either set variables using the dedicated $this->load_vars()
         * function or via the second parameter of this function. We'll merge
         * the two types and cache them so that views that are embedded within
         * other views can have access to these variables.
         */    
        if (is_array($_ci_vars))
        {
            $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
        }
        extract($this->_ci_cached_vars);
                
        /*
         * Buffer the output
         *
         * We buffer the output for two reasons:
         * 1. Speed. You get a significant speed boost.
         * 2. So that the final rendered template can be
         * post-processed by the output class.  Why do we
         * need post processing?  For one thing, in order to
         * show the elapsed page load time.  Unless we
         * can intercept the content right before it's sent to
         * the browser and then stop the timer it won't be accurate.
         */
        ob_start();
                
        // If the PHP installation does not support short tags we'll
        // do a little string replacement, changing the short tags
        // to standard PHP echo statements.
        
        if ((bool) @ini_get('short_open_tag') === FALSE AND config_item('rewrite_short_tags') == TRUE)
        {
            echo eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($_ci_path))));
        }
        else
        {
            include($_ci_path); // include() vs include_once() allows for multiple views with the same name
        }
        
        log_message('debug', 'File loaded: '.$_ci_path);
        
                // Modification for changing view path functionality
                if($this->_reset_view_path == true &&  ! empty($this->_ci_old_view_path))
                {
                    $this->_reset_view_path = false;
                    $this->_ci_view_path = $this->_ci_old_view_path;
                }
        // Return the file data if requested
        if ($_ci_return === TRUE)
        {        
            $buffer = ob_get_contents();
            @ob_end_clean();
            return $buffer;
        }

        /*
         * Flush the buffer... or buff the flusher?
         *
         * In order to permit views to be nested within
         * other views, we need to flush the content back out whenever
         * we are beyond the first level of output buffering so that
         * it can be seen and included properly by the first included
         * template and any subsequent ones. Oy!
         *
         */    
        if (ob_get_level() > $this->_ci_ob_level + 1)
        {
            ob_end_flush();
        }
        else
        {
            // PHP 4 requires that we use a global
            global $OUT;
            $OUT->append_output(ob_get_contents());
            @ob_end_clean();
        }
    }
}
Now you have two ways of altering the view path, grouping the alternate view path view method calls if you are working with a template.
Code:
$this->load->set_view_path(FCPATH.'views/',false);

$data['user_nav'] = $this->load->view('nav','',true);
$data['user_footer'] = $this->load->view('footer','',true);

$this->load->reset_view_path();
Or outputting the views as they are stacked to create the page
Code:
$user_view_path = FCPATH.'views/';

$this->load->set_view_path($user_view_path);
$this->load->view('nav');

$this->load->view('nav'); // CI view path

$this->load->set_view_path($user_view_path);
$this->load->view('footer');
#5

[eluser]EEssam[/eluser]
Thank you so much. You're very helpful.
#6

[eluser]Dam1an[/eluser]
[quote author="xwero" date="1245937507"]
Code:
class MY_Loader{

var _ci_old_view_path = '';
var _reset_view_path = false;

.....
[/quote]

I think you forgot to extend the CI loader, it should be

Code:
class MY_Loader extends CI_Loader {

var _ci_old_view_path = '';
var _reset_view_path = false;

.....
#7

[eluser]xwero[/eluser]
You are right, i changed the code in the snippet to fix it, thank you.
#8

[eluser]elfbaba[/eluser]
I`m trying to make the class compatible with CI 2, where is $this->_ci_is_instance() defined? Because right now i`m getting Call to undefined method MY_Loader::_ci_is_instance(), i traced it back to the parent class(CI_Loader) but could not find it.
#9

[eluser]elfbaba[/eluser]
I think i fixed it, the class is now working with CI 2, i replace
Code:
if ($this->_ci_is_instance())
        {
            $_ci_CI =& get_instance();
            foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
            {
                if ( ! isset($this->$_ci_key))
                {
                    $this->$_ci_key =& $_ci_CI->$_ci_key;
                }
            }
        }
with
Code:
$_ci_CI =& get_instance();
        foreach (get_object_vars($_ci_CI) as $_ci_key => $_ci_var)
        {
            if ( ! isset($this->$_ci_key))
            {
                $this->$_ci_key =& $_ci_CI->$_ci_key;
            }
        }
inside _ci_load() method




Theme © iAndrew 2016 - Forum software by © MyBB