Welcome Guest, Not a member yet? Register   Sign In
Load view outside the "views" folder
#1

[eluser]docmattman[/eluser]
I have several views in my application/views folder. I want my app to be theme-able. The theme folder is located outside of my application folder. The theme folder would have it's own views folder (i.e. theme/views).

What I'm trying to do is check if a view file exists in the theme view folder and if so, use it, otherwise use the application/views file. The purpose of this is that I can create all the views I need for my application, but the person doing and design work can decide to overwrite my app view with a custom one (in case they want to add more HTML tags or make other changes). I have tried this:

Code:
$this->load->view('/full/path/to/my/theme/views/file.php');

However, this always gives me an error:

Quote:Unable to load the requested file: /full/path/to/my/theme/views/file.php

I know the full file path exists and is readable. I believe the load->view function is restricting view files to only my app folder. Is there a way to load a view from a folder outside my application/views folder?
#2

[eluser]CroNiX[/eluser]
Kind of, but not exactly the same. Check load::file(); Of course, you can always just include() your file...
#3

[eluser]docmattman[/eluser]
[quote author="CroNiX" date="1370927811"]Check load::file(); Of course, you can always just include() your file...[/quote]

My issues with these suggestions:
1. I need to be able to pass variables into the view.
2. I need to be able to return the view as a string.
3. I need to be able to load another "themeable" view from inside the current "themeable" view.
#4

[eluser]CroNiX[/eluser]
Check how they load view files. You can extend the loader class with your own view_theme() method and use your own path instead of using the VIEWPATH constant.
#5

[eluser]docmattman[/eluser]
Okay, I'll have to check into that check into that. I was hoping there was a built in way to do it. Thanks for the help.
#6

[eluser]CroNiX[/eluser]
If all of your views are going to come from this directory, and not use the default ./application/views dir, then you can just set the $view_folder in index.php.
#7

[eluser]docmattman[/eluser]
I'm keeping all of my views in the application/views folder. But I want the whole thing to be themeable. So if someone creates a theme, they can choose to override one of my view files by putting a file with the same name in the theme/views folder. For this reason, I want to create a "load->themeableview()" function to first check if the file exists in the theme before loading my default one.

Looks like I'll most likely have to extend the loader class to accomplish this.
#8

[eluser]docmattman[/eluser]
For anyone trying to accomplish this same thing, I got it working by extending the loader class. It first checks to see if the file with the same name is located in the theme/views folder and if that doesn't exist, it loads it from the application/views folder install. Here is the code I used to do it.

Code:
<?php

class MY_Loader extends CI_Loader
{
public function __construct()
{
  parent::__construct();
}

/**
  * View Loader
  *
  * Loads "view" files from theme or view folder.
  *
  * @param string $view View name
  * @param array $vars An associative array of data
  *    to be extracted for use in the view
  * @param bool $return Whether to return the view output
  *    or leave it to the Output class
  * @return void
  */
public function themeable($view, $vars = array(), $return = FALSE)
{
        $themeFile = '/full/path/to/theme/views/'.$view.'.php';
        if(file_exists($themeFile)) return $this->_ci_load(array('_ci_path' => $themeFile, '_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));    // Get from theme
        return $this->view($view, $vars, $return);        // Get from views
}
}
#9

[eluser]Pert[/eluser]
This should also work if I remember correctly

Code:
$this->load->view('../../themes/default-theme/viewfile');

Another option is adding package path, this will also add additional folders where CodeIgniter checks views from.




Theme © iAndrew 2016 - Forum software by © MyBB