fuel / parser

Fuel PHP Framework - v1.x template parser package adapters
http://fuelphp.com
64 stars 45 forks source link

Changing view paths causes Twig to throw an error #17

Closed mrardon closed 13 years ago

mrardon commented 13 years ago

Issue:

When I change paths (i.e. views/contacts to views/layouts) Twig throws an error)

Think it has something to do with Twig holding on to the paths from a previous instance. (I dunno if its a problem with Parser or Twig as a quick glance of each's internals revealed nothing to me.)

To recreate:

Call $data["content"] = render('contacts/show.twig', $data); Then call $layout = View::factory('layouts/application.twig', $data, false);

Views Folder Structure:

views
| 
-- layouts
   |
   --application.twig
-- contacts
   |
   --show.twig

Error w/ Backtrace:

APPPATH actually showed my path in the error - I just removed it

Error!
Twig_Error_Loader [ Error ]: Unable to find template "application.twig" (looked into: APPPATH/views/contacts, APPPATH/views).

APPPATH/vendor/Twig/Loader/Filesystem.php @ line 129

124            if (is_file($path.'/'.$name)) {
125                return $this->cache[$name] = $path.'/'.$name;
126            }
127        }
128
129        throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths)));
130    }
131
132    protected function validateName($name)
133    {
134        if (false !== strpos($name, "\0")) {
Backtrace

APPPATH/vendor/Twig/Loader/Filesystem.php @ line 98
APPPATH/vendor/Twig/Environment.php @ line 259
APPPATH/vendor/Twig/Environment.php @ line 294
PKGPATH/parser/classes/view/twig.php @ line 42
COREPATH/classes/view.php @ line 418
COREPATH/classes/view.php @ line 197
DOCROOT/index.php @ line 47

Something I tried

I var_dumped the $_parser_loader when the exception was caught (Around line 48 in parser/classes/view/twig.php) and got this

object(Twig_Loader_Filesystem)#16 (2) {
  ["paths":protected]=>
  array(2) {
    [0]=>
    string(51) "APPPATH/views/layouts"
    [1]=>
    string(43) "APPPATH/views"
  }
  ["cache":protected]=>
  array(0) {
  }
}

This leads me to believe that something "statically" being set is not in fact changing on the Twig side of things as the var_dump reveals the paths were set correctly in the Twig_Loader_Filesystem and in the Error w/Backtrace shows that the paths are different (i.e. APPPATH/views/layouts, APPPATH/views vs. APPPATH/views/contacts, APPPATH/views)

Let me know if you need other details, I tried to be as thorough as possible. Thanks, Matt

WanWizard commented 13 years ago

Issue might be in parser/classes/view/twig.php.

The method parser(), responsible for creating a parser object, stores the created object in a static variable so it can be reused later. Because of that, the newly created parser_loader object is not used in the twig environment on subsequent calls, leading to this error.

To verify this, comment line 58 in that file (where the static is returned so it will create a new environment every time you call the parser, and see if that fixes the problem.

WanWizard commented 13 years ago

Correction, change it so it reads

    if ( ! empty(static::$_parser))
    {
        static::$_parser->setLoader(static::$_parser_loader);
        return static::$_parser;
    }

This will update the loader of the current Twig object.

mrardon commented 13 years ago

That definitely fixed it! Great work!