The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Twig getEnvironment function no longer available using Slim Views

Martin K.'s Avatar

Martin K.

13 Aug, 2013 09:05 PM

With Slim 2.2/Slim Extras to use Twig I'd use:

$twig = $app->view()->getEnvironment();
$twig->addGlobal('variablename', $menu);

(http://help.slimframework.com/kb/views/using-twig-functions-inside-...)

But that is no longer available in 2.3/Sliv Views and I get:

Slim Application Error
The application could not run because of the following error:
Details
Type: Twig_Error_Loader
Message: The "" directory does not exist.

Any help please?
Many thanks,
Martin

  1. Support Staff 1 Posted by Andrew Smith on 14 Aug, 2013 12:03 AM

    Andrew Smith's Avatar

    Can you provide some code example of what you currently have, I can tell you that the method still exist, although it is set to be deprecated in favour of getInstance(). So you can do:

    $app = new Slim\Slim();
    $app->view(new Slim\Views\Twig());
    
    $twig = $app->view->getEnvironment();
    $twig->addGlobal('variablename', $menu);
    

    Note that this should be done outside of your routes, so not inside of your $app->get/post/map/delete/put.

  2. 2 Posted by Martin K. on 14 Aug, 2013 02:08 PM

    Martin K.'s Avatar

    Hi Andrew,

    thanks for replying. The sample code is as follows:

        // import channel-specific order files with procedures
        $app->get('/sales/import/:channel/:id', 'checkLogin', function ($channel_id, $id) use ($app, $data) {
            $data['id'] = $id;
            $data['channel_id'] = $channel_id;
            $twig = $app->view()->getEnvironment();
            $twig->addGlobal('additional_scripts', array('/js/vendor/bootstrap-fileupload.min.js', '/js/file-upload.js'));
            $twig->addFunction('replace', new Twig_Function_Function('replace'));

            $app->render('/sales/import.twig', $data);
        });

    This used to work in 2.2 with Slim/Extras. Any idea please?

    Many thanks,
    Martin

  3. 3 Posted by Nate on 06 Sep, 2013 03:39 PM

    Nate's Avatar

    I'm having the same problem. I get an "The "" directory does not exist." error when calling $app->view->getEnvironment(); This worked fine in 2.2, but throws this error in 2.3

  4. Support Staff 4 Posted by Andrew Smith on 06 Sep, 2013 03:45 PM

    Andrew Smith's Avatar

    Nate when using 2.3 please use Slim Views instead of Slim Extras.

    Martin I can't see anything wrong with your code, try moving all the $twig stuff outside of the route and see if it works. So do this instead:

    $twig = $app->view()->getEnvironment(); 
    $twig->addGlobal('additional_scripts', array('/js/vendor/bootstrap-fileupload.min.js', '/js/file-upload.js')); 
    $twig->addFunction('replace', new Twig_Function_Function('replace'));
    
    $app->get('/sales/import/:channel/:id', 'checkLogin', function ($channel_id, $id) use ($app, $data) { 
        $data['id'] = $id; 
        $data['channel_id'] = $channel_id; 
    
        $app->render('/sales/import.twig', $data); 
    });
    
  5. 5 Posted by Nate on 06 Sep, 2013 04:19 PM

    Nate's Avatar

    Andrew, thank you for the quick response.
    I got it working (Using Slim 2.3.1, Slim-Views, and Twig). Unfortunately, I had to change my code to use:
    $app->view->setTemplatesDirectory("/path/to/templates"); instead of being able to use the templates.path config key

  6. Support Staff 6 Posted by Andrew Smith on 06 Sep, 2013 04:47 PM

    Andrew Smith's Avatar

    This is the second time someone has mentioned this issue https://github.com/codeguy/Slim-Views/issues/2#issuecomment-22625697 as you can see I have a test repo setup which shows this working as it should. I can only think that because you are trying to get an instance of twig inside your route why this issue happens.

  7. 7 Posted by Robert Ford on 10 Oct, 2013 02:24 PM

    Robert Ford's Avatar

    I ran into this issue and solved it by removing the template path from the slim app configuration and adding to to the view instead i.e.

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

    // configure Twig
    $view = $app->view();
    $view->parserOptions = array(
        'debug' => true,
        'cache' => 'cache',
        'charset' => 'utf-8',
        'auto_reload' => true,
        'strict_variables' => false,
        'autoescape' => true
    );
    $view->parserExtensions = array(
        new \Slim\Views\TwigExtension(),
    );
    $app->view->setTemplatesDirectory( dirname(__FILE__) . '/Views');

    I could then define globals as before i.e.

    $twig = $app->view()->getEnvironment();
    $twig->addGlobal('test', 'Slim Awesomeness!');

  8. 8 Posted by Mickie on 21 Jun, 2016 12:51 AM

    Mickie's Avatar

    Supercharge your marketing campaign with our social media promotion services. Get huge likes/followers/views at affoardable prices.

    Packages:

    1) 5,000 Facebook Fans/Likes($75) = Order at:- http://khalaghor.com/5kfb/
    2) 5,000 Twitter Followers($35) = Order at:- http://khalaghor.com/twitter/
    3) 25,000 YouTube Views($25) = Order at:- http://khalaghor.com/youtube/
    4) 1,000 Google+ Ones($50) = Order at:- http://khalaghor.com/google/
    5) 5,000 Instagram Followers($40) = Order at:- http://khalaghor.com/instagram/

    You can also order smaller or bigger package from our official website.

    Don't reply to this mail.We don't monitor inbox.

    Thank You

    To unsubscribe, visit:- http://tinyurl.com/jh2j82q

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac