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

templates.path doesn't change

mike's Avatar

mike

20 May, 2013 07:28 AM

I've read through god know's how many pages but cant find the answer anywhere. I'm trying to not use a template engine like h2o or twig, since all I need is basic rendering.

I have the following code set

` $app->config(array( 'templates.path' => './ui', ));

$app = new \Slim\Slim();

$view = $app->view();
$view->setTemplatesDirectory('./ui');`

But no slim wants to use './templates' so says the debug log
Type: RuntimeException Message: View cannot render template./templates/index.html. Template does not exist.

  1. Support Staff 1 Posted by Andrew Smith on 20 May, 2013 07:40 AM

    Andrew Smith's Avatar

    This works for me. Can you provide details with which version of Slim you are using? are you using Slim Extras to add in the extra views? if so which version of that are you using?

    Can you also show your entire code please.

  2. 2 Posted by mike on 20 May, 2013 07:44 AM

    mike's Avatar

    The latest version freshly installed by composer.

    require_once "vendor/autoload.php";

    $dsn = "mysql:dbname=**********;host=localhost";
    $username = "**********";
    $password = "**********";
    
    $pdo = new PDO($dsn, $username, $password);
    $db = new NotORM($pdo);
    
    $app = new \Slim\Slim(array(
        "mode" => "development",
        'debug' => true
    ));
    
    $app->config(array(
        'templates.path' =>  './ui',
    ));
    
    $app = new \Slim\Slim();
    
    $view = $app->view();
    $view->setTemplatesDirectory('./ui');
    
    $app->get('/', 'getIndex');
    function getIndex() {
        $app = \Slim\Slim::getInstance();
        $app->render('index.html', array('data' => 'index page'));
    }
    
    
    
    
    $app->run();
  3. Support Staff 3 Posted by Andrew Smith on 20 May, 2013 11:24 AM

    Andrew Smith's Avatar

    I am not sure why you are instantiating Slim twice?

    Try this instead.

    $app = new \Slim\Slim(array(
        'mode' => 'development',
        'debug' => true
    ));
    
    $app->config(array(
        'templates.path' => './ui',
    ));
    
    $app->get('/', 'getIndex');
    function getIndex() {
        $app = \Slim\Slim::getInstance();
        $app->render('index.html', array('data' => 'index page'));
    }
    
    $app->run();
    
  4. Support Staff 4 Posted by Brian Nesbitt on 20 May, 2013 09:28 PM

    Brian Nesbitt's Avatar

    Not sure that will work.

    You have 2 choices as indicated in the docs.

    You can use the config array on slim creation or after creation you need to
    get the view instance and user the set templates method. This is shown in
    the docs.

    You can see how I do it in my blog:
    https://github.com/briannesbitt/nesbot.com/blob/master/app.php#L18

    Sent from my iPhone

  5. 5 Posted by Mark Jordan on 25 Jun, 2013 02:33 AM

    Mark Jordan's Avatar

    Does the post-instantiation method actually work in 2.2.0? Following the docs for the setTemplatesDirectory method, I can't get this to work as I understand the docs to read:

    <?php  
    require 'lib/Slim/Slim.php';  
    \Slim\Slim::registerAutoloader();
    $app = new \Slim\Slim();
    
    $app->get('/test', function () use ($app) {
      $view = $app->view();
      $view->setTemplatesDirectory('./alt_templates/');
      $app->render('myTemplate.html');
    });
    
    $app->run();
    ?>
    

    The default template directory is still being used, not the one I'm setting using $view->setTemplatesDirectory().

  6. Support Staff 6 Posted by Brian Nesbitt on 25 Jun, 2013 04:04 AM

    Brian Nesbitt's Avatar

    Apparently every call to render() resets the template directory on the current view.

    https://github.com/codeguy/Slim/blob/master/Slim/Slim.php#L670

    .... thus rendering (haha) your call to set the templates directly on the view useless.

    Pass it in to the Slim ctor.

  7. 7 Posted by Mark Jordan on 25 Jun, 2013 02:23 PM

    Mark Jordan's Avatar

    My app doesn't decide which template directory to use depending on logic in a route, which means the Slim constructor is already set. So I can't pass it to the Slim constructor.

    So this is either a bug or the documentation is wrong.

  8. Support Staff 8 Posted by Brian Nesbitt on 25 Jun, 2013 05:12 PM

    Brian Nesbitt's Avatar

    In previous versions it only wrote it on view creation in the Slim ctor, hence the documentation being out of date. It writes it now on every render() call.

    From looking at the code doing this before calling render should work.:

    $app->config('templates.path', './my/template/dir/');

  9. 9 Posted by Mark Jordan on 25 Jun, 2013 06:15 PM

    Mark Jordan's Avatar

    Excellent -- this works:

    ```
    <?php

    // Slim setup.
    require 'lib/Slim/Slim.php';
    \Slim\Slim::registerAutoloader();
    $app = new \Slim\Slim();

    $app->get('/test', function () use ($app) {
      $app->config('templates.path', './alt_templates/');
      $app->render('myAltTemplate.html');
    });

    $app->run();
    ?>
    ```

    Thanks for your help,

    Mark

  10. Support Staff 10 Posted by Brian Nesbitt on 25 Jun, 2013 06:23 PM

    Brian Nesbitt's Avatar

    Np.

  11. Brian Nesbitt closed this discussion on 25 Jun, 2013 06:23 PM.

Comments are currently closed for this discussion. You can start a new one.

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