Welcome Guest, Not a member yet? Register   Sign In
How do I load a view from outside the default views folder in this application?
#1

I am working on a CMS with [b]CodeIgniter 3.1.8[/b] and Bootstrap 4. I have decided to add themes to it.

The [i]themes[/i] directory is outside [i]application[/i] as can be see in the image below:
[color=var(--blue-700)][Image: syL0A.jpg]

Inside [i]themes[/i] I have the theme directory (of course) which contains the "master view",  layout.php:
[/color][color=var(--blue-700)][Image: gOGzZ.jpg]
In application/core 
[/color]I have added a Loader.php file with the following contents:


Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH."../system/core/Loader.php";

    class MY_Loader extends CI_Loader {
   
      function ext_view($folder, $view, $vars = array(), $return = FALSE) {
        $this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH . $folder . '/' => TRUE));
        return $this->_ci_load(array(
                '_ci_view' => $view,
                '_ci_vars' => $this->_ci_prepare_view_vars($vars),
                '_ci_return' => $return
            ));
      }
   
}?>

In my Posts controller's index() 
method, I load the view passing it the data:


Code:
public function index() {
    //more code here
     $this->load->ext_view('third_party', 'themes/caminar/layout', $data);
}

I get this error message:

Code:
Call to undefined method CI_Loader::ext_view()

[b]NOTE:[/b] the application is [b]not HMVC[/b], only MVC.


What am I doing wrong?
Reply
#2

This is not tested.

Add this to index.php

PHP Code:
$theme_path 'themes';

if ((
$_temp realpath($theme_path)) !== FALSE)
{
    $theme_path $_temp.DIRECTORY_SEPARATOR;
}
else
{
    // Ensure there's a trailing slash
    $theme_path strtr(
        rtrim($theme_path'/\\'),
            '/\\',
        DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
    
).DIRECTORY_SEPARATOR;
}

// Path to the themes directory
define('THEMEPATH'$theme_path); 

The your code would be something like this.

PHP Code:
<?php

// application/core
class MY_Loader extends CI_Loader
{
    function ext_view($folder$view$vars = array(), $return FALSE)
    {
        $this->_ci_view_paths array_merge($this->_ci_view_paths, array(THEMEPATH $folder '/' => TRUE));
        return $this->_ci_load(array(
            '_ci_view' => $view,
            '_ci_vars' => $this->_ci_object_to_array($vars),
            '_ci_return' => $return
        
));
    }
}

$view_data = array('my_name' => 'dino');

// you may need to create a folder for the layout using this.
$this->load->ext_view('view_folder''my_new_view'$view_data); 

Give that a try.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(10-29-2020, 12:47 PM)InsiteFX Wrote: This is not tested.

Add this to index.php

PHP Code:
$theme_path 'themes';

if ((
$_temp realpath($theme_path)) !== FALSE)
{
    $theme_path $_temp.DIRECTORY_SEPARATOR;
}
else
{
    // Ensure there's a trailing slash
    $theme_path strtr(
        rtrim($theme_path'/\\'),
            '/\\',
        DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
    
).DIRECTORY_SEPARATOR;
}

// Path to the themes directory
define('THEMEPATH'$theme_path); 

The your code would be something like this.

PHP Code:
<?php

// application/core
class MY_Loader extends CI_Loader
{
    function ext_view($folder$view$vars = array(), $return FALSE)
    {
        $this->_ci_view_paths array_merge($this->_ci_view_paths, array(THEMEPATH $folder '/' => TRUE));
        return $this->_ci_load(array(
            '_ci_view' => $view,
            '_ci_vars' => $this->_ci_object_to_array($vars),
            '_ci_return' => $return
        
));
    }
}

$view_data = array('my_name' => 'dino');

// you may need to create a folder for the layout using this.
$this->load->ext_view('view_folder''my_new_view'$view_data); 

Give that a try.

Thanks!

I solved it. Have a look here if you're curios.
Reply
#4

Great, glad you got it to work.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(10-30-2020, 11:22 AM)InsiteFX Wrote: Great, glad you got it to work.

There is another question.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB