slimphp / Slim-Views

Slim Framework 2 custom views

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

templates.path

robot-c0der opened this issue · comments

commented

Setting templates.path in the Slim configuration options doesn't set the path for the Twig view. This creates exceptions like:
Uncaught exception 'Twig_Error_Loader' with message 'The "" directory does not exist.

It should at least be documented that $view->setTemplatesDirectory(PATH_TO_DIRECTORY) needs to be called for this to work

I will test this out later.

I have this issue too.
It's really blocking when you try to add a twigExtension such as i18n:

$twig = $app->view()->getEnvironment();
$twig->addExtension(new Twig_Extensions_Extension_I18n());

Try this instead

$twig = $app->view()->getEnvironment();
$twig->addExtension(new \Twig_Extensions_Extension_I18n());

Nope, sorry, but it's the same : The exception @awkerney listed is thrown by the getEnvironment method.
In fact it's thrown by the getInstance method, to be precise.

Here is a simple full snippets which lead to the same Exception

$app = new \Slim\Slim(
    array(
        'view' => new \Slim\Views\Twig(),
        'templates.path' => '/path/to/templates'
    )
);

$app->view()->getEnvironment();

wheres as using the view (instead of trying to get it) perfectly works :

$app = new \Slim\Slim(
    array(
        'view' => new \Slim\Views\Twig(),
        'templates.path' => '/path/to/templates'
    )
);

$app->get('/', function () use ($app) {
    $app->render('index.tpl');
});

$app->run();

I don't know, it may be a kind of lazy load issue ?

@loranger I can't see the difference in your 2 examples.

In the 2nd example he doesn't explicitly call getEnvironment(). I'm having this issue as well. Fails when adding helpers:

$app->view()->getEnvironment()->addGlobal('form', new \MyHtmlHelper\Form());

I'm going to investigate it.

        $this->view->setTemplatesDirectory($this->config('templates.path'));

Isn't called until the \Slim::render() function. It must either be called manually before trying to getEnvironment(), or it needs to be earlier in the call chain of Slim itself.

@silentworks yes, there is a difference. If you run both for those, you'll see what @zulrang, @awkerney and I are talking about...

Hi Guys, I did some test on this and couldn't replicate the issue you are currently having, you can test this by downloading this project https://github.com/memphisphp/dev-box and use vagrant to set it up, you will see there is the template directory being set using the template.path config of Slim.

Out of Interest what version of Slim are you using when having this problem?

commented

I'm pretty sure I'm using Slim 2.3.1, with slim/views 0.1.0

I created a gist with the code that I've got that's throwing the exception if you want to see it, maybe it'll give you an idea what's going on:

https://gist.github.com/awkerney/a8a558d2eccaceb14f3d

Like @awkerney, composer brings me Slim 2.3.1 with slim-views 0.1.0

I also created a gist with my minimal requirements and code.
Download it, extract, run composer install and you'll be ready to throw the Twig_Error_Loader exception
I did test it using Debian 7, apache2 + PHP5.5 and OSX, nginx + PHP 5.4 FPM, the result remains the same.

I have tested both your codes and its working, the only issue I had with @awkerney code was that the paths that I was adding didn't exist, but once I created those directories inside my template directory then all worked fine. Can you both test out the dev-box vagrant setup and replicate the issue you are having then send that code over to me.

I didn't need to setTemplatesDirectory as your code suggested for it to work either.

commented

I'm still getting the following PHP Fatal error message:

Uncaught exception 'Twig_Error_Loader' with message 'The "" directory does not exist.' in /var/sites/dev.sandbox/vendor/twig/twig/lib/Twig/Loader/Filesystem.php:93
Stack trace:
#0 /var/sites/dev.sandbox/vendor/twig/twig/lib/Twig/Loader/Filesystem.php(75): Twig_Loader_Filesystem->addPath(NULL, '__main__')
#1 /var/sites/dev.sandbox/vendor/twig/twig/lib/Twig/Loader/Filesystem.php(33): Twig_Loader_Filesystem->setPaths(Array)
#2 /var/sites/dev.sandbox/vendor/slim/views/Slim/Views/Twig.php(118): Twig_Loader_Filesystem->__construct(Array)
#3 /var/sites/dev.sandbox/vendor/slim/views/Slim/Views/Twig.php(98): Slim\\Views\\Twig->getInstance()
#4 /var/sites/dev.sandbox/public/index.php(34): Slim\\Views\\Twig->getEnvironment()
#5 {main} 
 thrown in /var/sites/dev.sandbox/vendor/twig/twig/lib/Twig/Loader/Filesystem.php on line 93

All I did was put my code in the dev-box vagrant setup, and have it use the default autoload.php that the setup comes with. I updated the code in the gist to reflect the index.php that I ran in the dev-box I still have to have the line

$view->setTemplatesDirectory($templateDir);

for it to work. I'm beginning to think it might just be something screwed up with my code…?
I updated the gist I linked to earlier with the code I tried

@awkerney but you just replaced the code in the dev-box vagrant setup, so your error would persist. I will rewrite your code and push it up somewhere for you to checkout.

@awkerney Checkout this repo https://github.com/silentworks/templatepathtest and then do a composer install, then vagrant up once you have done all the setup, you will see the setup you require working there. I haven't included your other dependencies to the composer.json as I wanted to make sure that Slim and Slim Views are working as they should. You can add your other dependencies after you see this working correctly.

commented

@silentworks Yeah, your code works so I think it's just something weird in either my server setup or my code. Thanks for your help. At any rate I've rewritten my code as part of a refactor so it seems to work now

I ran into this issue myself today, and I can't really see how you resolved it in your templatepathtest repo.
What exactly needs to be set for this to work?

Same issue here. May I ask you: Are you on Vagrant ? If so, what does your config.vm.synced_folder line look like ?

This is my setup, which is working:

$app = new \Slim\Slim(array(
        'view' => new \Slim\Views\Twig()
    ));

    $env = $app->environment;
    $env['PATH_INFO'] = strtok($_SERVER["REQUEST_URI"],'?');

    $app->config(array(
        'log.writer' => new Slim\Extras\Log\DateTimeFileWriter(array(
            'path' => '../logs',
            'name_format' => 'Y-m-d',
            'message_format' => '%label% - %date% - %message%'
        )),
        'log.level' => \Slim\Log::DEBUG,
        'log.enabled' => true,
        'debug' => true,
        'templates.path' => './views'
    ));

    $view = $app->view();
    $view->setTemplatesDirectory($app->config('templates.path'));

    $view->parserOptions = array(
        'debug' => true,
        'cache' => dirname(__FILE__) . '/cache'
    );
    $view->parserExtensions = array(
        new \Slim\Views\TwigExtension(),
    );