【问题标题】:Setting default assigns with slim using smarty使用 smarty 设置默认分配与 slim
【发布时间】:2013-04-25 07:53:21
【问题描述】:

我即将转换一个站点以使用带有 Smarty 模板引擎的 Slim 框架。 在当前站点上,我已经分配了一些默认的 smarty 变量,但我不确定如何使用 Slim 框架设置它们,所以它们只设置一次。

这就是他们现在的样子:

$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());

这是我未来的 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();

我应该在 SmartyView 渲染函数中设置它们

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

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

【问题讨论】:

    标签: php smarty slim assign


    【解决方案1】:

    通过使用修复它

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2023-01-12
      • 2013-05-27
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多