Vheissu / Ci-Smarty

Smarty templating engine integration for Codeigniter 3.0+ with inbuilt theming and asset management.
http://ilikekillnerds.com
179 stars 43 forks source link

Template not found during HMVC Modules::run #28

Closed imevul closed 10 years ago

imevul commented 11 years ago

It seems that if you call Modules::run('differentModule/controller') in a Smarty template, then any templates loaded in that module will not be found. This is because the current module is no longer $this->_module. I don't know if this is a bug though or if I'm doing something wrong, but I thought I'd report it along with my solution.

Modifying _find_view() around line 310 in MY_Parser as follows seems to solve the changing module problem. This will detect the module change and inject the relevant paths just for this template call.

        // We have no path by default
        $path = NULL;
        $locations = $this->_template_locations;

        $current_module = $this->current_module();

        // If the current module differs from the stored one, we have a Modules::run() call.
        // Add the new module's path to this search.
        if ($current_module !== $this->_module)
        {
            $new_locations = array(
                    config_item('theme_path') . $this->_theme_name . '/views/modules/' . $current_module .'/layouts/',
                    config_item('theme_path') . $this->_theme_name . '/views/modules/' . $current_module .'/',
                    APPPATH . 'modules/' . $current_module . '/views/layouts/',
                    APPPATH . 'modules/' . $current_module . '/views/'
            );

            // Add module paths first so they take precedence
            foreach($new_locations as $new_location)
            {
                array_unshift($locations, $new_location);
            }
        }

        // Iterate over our saved locations and find the file
        foreach($locations AS $location)
candfe commented 10 years ago

That's great; I had the same problem and that solved it. Now I have the same issue when calling the smarty method "fetch". Do you have any idea how to solve it ?

$this->parser->fetch('news.tpl'); // Works only when called from the same module !