Welcome Guest, Not a member yet? Register   Sign In
Loading wrong views in twiggy!
#1

(This post was last modified: 07-24-2015, 04:13 AM by weblogics.)

I'm running codeigniter latest version, with twiggy installed and HMVC. I've had this issue before with no answers and was hoping that someone else could get a view on this in order to get to a resolution. I've tried to contact the guy who developed the twiggy plugin with no results.

When loading partials from other modules twiggy is using the path to my theme folder from the partial I'm trying to load from a category module and not my blog module. This results in the category create.html.twig template being loaded when calling my blog create.html.twig template.

PHP Code:
$this->twiggy->set('categories'Modules::run('category/category_manage/dropdown'));
$this->twiggy->display(); 

I'm expecting the result to return the view from my category module to my blog module and allow this to be included to keep things DRY in my controllers and views.

The dropdown function in my categories controller is:

PHP Code:
$this->twiggy->set('categories'$this->category_model->dropdown('id''name'));
$this->twiggy->display('dropdown'); 

Please note I have also tried:

PHP Code:
$this->twiggy->set('categories'$this->category_model->dropdown('id''name'));
return 
$this->twiggy->render('dropdown'); 

and:

PHP Code:
$this->twiggy->set('categories'$this->category_model->dropdown('id''name'));
$this->twiggy->template('dropdown')->render(); 

With no success

Debugging shows this message whilst calling 'blog/create':

Unable to find template "blog/create.html.twig" (looked into: application/../modules/category/themes/admin, application/themes/admin).

Which suggests the wrong theme location is being targetted when it should be looking in the current module 'blog' in 'themes/admin/blog/create.html.twig'

On further investigation it seems to be this line in the twiggy library:

PHP Code:
if(method_exists($this->CI->router'fetch_module'))
        {
            
$this->_module $this->CI->router->fetch_module();  // <----- This seems to be where the module is set / overwritten in the twiggy library

            // Only if the current page is served from a module do we need to add extra template locations.
            
if(!empty($this->_module))
            {
                
$module_locations Modules::$locations;

                foreach(
$module_locations as $loc => $offset)
                {
                    
/* Only add the template location if the same exists, otherwise
                    you'll need always a directory for your templates, even your module
                    won't use templates */
                    
if ( is_dir($loc $this->_module '/' $this->_config['themes_base_dir'] . $theme) )
                        
$this->_template_locations[] = $loc $this->_module '/' $this->_config['themes_base_dir'] . $theme;
                }
            }
        } 

This piece of code is causing the module to be overridden if you run Modules::run() inside another module, can anyone see a way around this as I would like to use modules to keep things DRY and import views into other modules whilst rendering the full template out.

Any help would be apprecitated.
Reply
#2

(This post was last modified: 07-24-2015, 06:47 AM by mwhitney.)

fetch_module() simply returns the last module set in the router. Modules::run() calls the router's locate() method to find the controller and load it, which sets the router's module to the module of the newly-loaded controller, rather than the module initially used to handle the request.

The best work-around (other than avoiding Modules::run() or calling it at a point where the change of modules wouldn't cause an issue) would probably be to store the result of fetch_module() before calling Modules::run(), then set the router's $module property back to the previous value after calling Modules::run().

Edit: Another thought came to me... This might be similar to the issues Wiredesignz HMVC has with the form validation library. You may want to try a similar work-around with Twiggy, but extending the library to make $CI a public property, then setting the CI property to $this in your controller's constructor.
Reply
#3

(This post was last modified: 07-24-2015, 08:28 AM by weblogics.)

(07-24-2015, 06:39 AM)mwhitney Wrote: fetch_module() simply returns the last module set in the router. Modules::run() calls the router's locate() method to find the controller and load it, which sets the router's module to the module of the newly-loaded controller, rather than the module initially used to handle the request.

The best work-around (other than avoiding Modules::run() or calling it at a point where the change of modules wouldn't cause an issue) would probably be to store the result of fetch_module() before calling Modules::run(), then set the router's $module property back to the previous value after calling Modules::run().

Edit: Another thought came to me... This might be similar to the issues Wiredesignz HMVC has with the form validation library. You may want to try a similar work-around with Twiggy, but extending the library to make $CI a public property, then setting the CI property to $this in your controller's constructor.

Thanks for your reply, I can see what the code is doing and what your suggesting, I just don't have the skills to modify it, I've tried a few ways all with no success. Your last thought sounds like a scalable application wide way to code around this problem, do you have any ideas on how this would be implemented?

Here is a running replica of the issues i'm facing:

Download Here

This does seem such a simple error that maybe i'm overthinking it, I'm not sure of where the error exists, is it in the twiggy application as all it is doing is getting the module thats currently running, but due to the way HMVC works it sets the current module. The issue i'm facing is due to loading a Module with-in a Module to get around duplicate code / views etc its then setting the Module as the last running module which leaves the Twiggy library referencing the last Directories theme directory to get the view.

The example above illustrates this problem with no view found and therefore the error states it cannot find the view and this is because its trying to consult the wrong directory.

If anyone can help, i'm happy to cover caffeine and beer costs to get me going, I don't have the time or the know how to resolve this issue today and as usual i'm on a deadline.

Attached Files
.zip   CITwigError.zip (Size: 927.7 KB / Downloads: 61)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB