【发布时间】:2015-07-30 09:25:28
【问题描述】:
我有一个使用RainTpl3 作为其模板引擎的现有项目。我正在尝试将其移植到 Slim 框架 2。
Q1 : 有什么方法可以使 slim 使用我的 $tpl 对象来呈现其视图(通过 DI 或任何其他类似方法)。
我设法破解了一种在外部函数中配置创建模板对象的方法,然后该函数应返回随后 slim 应回显的内容。早些时候它无法正确配置 tpl,所以我在我的 init 文件中配置了它。现在,当我使用 $tpl->draw('index'); 时,它表示找不到模板索引。 Q2:为什么找不到模板?
:MY_APP_ROOT_/Public/index.php
require_once "../init.php";
# Fire up an app
$app = new Slim\Slim();
$app->get('/', function(){echo MyNS\Router\APIDefault::showHome();});
$app->get('/Hello', function() {
echo "Get Route is working.";
});
# Run the Slim application
$app->run();
:MY_APP_ROOT_/Routes/APIDefault.php
<?php
namespace MyNS\Router;
class APIDefault
{
private function __construct( $argument)
{
throw new \Exception("Error Constructor not allowed", 1);
}
public static function showHome()
{
$tpl = new \Rain\Tpl();
$tpl->assign('Name','Abhinav');
$a = $tpl->draw('index',$return_string = true);
return $a;
}
}
index.tpl 存储在 MY_APP_ROOT_/Templates/ 中。
【问题讨论】:
标签: php template-engine slim