我有关于Smarty模板引擎的网站,现在添加了Slim Framework用于路由,更改html中的内容我已经将内容分配给index.tpl
$template->assign(array('BASE' => $CONF['url.base'], 'THEME' => $SET['template'], 'CONTENT' => $CONTENT));
我希望从我尝试使用此代码的路线设置$ CONTENT值,但它不起作用:
$app->get('/', function () use ($template, $CONTENT){
$CONTENT = $template->fetch('Route/home.tpl');
});
我怎么做?
我的Smarty代码是:
/// INCLUDE SMARTY AND CONFIGURE
require_once(CORE_DIR.'Smarty/Smarty.class.php');
$template = new Smarty();
$template->setTemplateDir(THEME_DIR.$WEBSITE['template']);
$template->setCompileDir(THEME_DIR.'/templates_c/');
$template->setCacheDir(CACHE_DIR);
答案 0 :(得分:0)
您似乎需要使用引用传递$CONTENT
变量,此外您需要在使用$CONTENT
变量之前运行您的应用,因此您的代码应如下所示:
$CONTENT = '';
$app->get('/', function () use ($template, &$CONTENT){
$CONTENT = $template->fetch('Route/home.tpl');
});
$app->run();
echo $CONTENT; // here you have modified $CONTENT variable
我的整个测试代码(如果您需要):
<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim(array (
'view' => new \Slim\Views\Smarty()
)
);
$template = $app->view();
$template->parserDirectory = dirname(__FILE__) . '/smarty';
$template->parserCompileDirectory = dirname(__FILE__) . '/compiled';
$template->parserCacheDirectory = dirname(__FILE__) . '/cache';
$template->setTemplatesDirectory(dirname(__FILE__) . '/smarty/templates');
$CONTENT = '';
$app->get('/', function () use ($template, &$CONTENT){
$CONTENT = $template->fetch('Route/home.tpl');
});
$app->run();
echo $CONTENT;