Morten Hagh
Morten Hagh

Reputation: 2113

Setting default assigns with slim using smarty

I am about to convert a site to use the Slim Framework with the Smarty template engine. On the current site I have assigned some default smarty vars, but I am not sure how to set them with the Slim framework, so they are set only once.

This is how they are now:

$smarty->assign("fid", $user_profile["id"]);
$smarty->assign("fid_name", $user_profile["name"]);
$smarty->assign("fid_email", $user_profile["email"]);
$smarty->assign('postzip', $users->find_zip_or_post_new($user_profile["id"]));  
$smarty->assign('userzip', $users->find_my_zip_code($user_profile["id"]));
$smarty->assign('url', $url);
$smarty->assign('catlist', $adverts->getcategories());

And this is my to-be index.php

/***
 * This is the script that will generate the complete site 
 */
include_once("includes/classes/class.config.php"); //Configuration file

/*
 * Include the Slim framework
 */
require 'libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
use Slim\Slim;

/*
 * Include the Smarty template view
 */
require 'libs/Slim/Extras/Views/Smarty.php';
/*
 * Set new Slim object
 */

$app = new Slim(array(
    'view' => new \Slim\Extras\Views\SmartyView(),
    'debug' => true,
    'log.enable' => true,
    'log.path' => 'logs/',
    'log.level' => 4,
    'mode' => 'development'
));

//Ad view
$app->get('/', function() use ($app) { 
    $adverts = new Adverts();   
    $view = $app->view();
    $app->render(
        'index.tpl',
        array(
            'adverts' => $adverts->listadverts()
        )
    );
 });

$app->run();

Should I set them in the SmartyView render function

public function render($template) {        
    $instance = self::getInstance();
    $instance->assign($this->data);

    return $instance->fetch($template);
}

Upvotes: 0

Views: 887

Answers (1)

Morten Hagh
Morten Hagh

Reputation: 2113

Fixed it by using

$app->hook('slim.before.dispatch', function () use ($app) {
});

Upvotes: 1

Related Questions