好吧,经过更多的研究,我成功地将 Smarty 3 轻松地集成到 Laravel 4 中。我不确定这是否是最好的方法,但它工作得很好,所以欢迎评论或进一步的建议。
我安装了this http://packalyst.com/packages/package/latrell/smartySmarty View for Laravel 并按照说明将其添加到列表中providers
in app/config/app.php
。运行后php artisan config:publish latrell/smarty
命令配置已自动创建,我已准备就绪。这个包似乎还使用了正确的 Smarty 库,而不是一些修改过的模板。
然后我创建了一个普通的旧 HTML 文件,其中包含.tpl
扩展在app/views
目录,以及相应的控制器app/controllers
目录,以及相应的路由routes.php
嘿,很快,它就顺利地工作了。
我什至修改了 BaseController 以维护模板变量的通用列表(例如 CSS 样式等)以注入 HTML,而无需在模板中放入难看的 PHP 代码。 (不知道有没有更好的方法直接将它们设置到View
来自BaseController
而不是期望子类在调用中将它们传递给make
方法,但我想这是一个不同的问题。)
下面的代码适合可能需要它的人:
HelloWorld.tpl
<!doctype html>
<html lang="en">
<head>
<title>Hello World</title>
<meta charset="UTF-8">
{$style}
</head>
<body>
<div>
<h1>Hello {$name}</h1>
</div>
</body>
</html>
BaseController.php
class BaseController extends Controller {
protected $template_vars = array();
protected function addTemplateVar($key, $value)
{
$this->template_vars[$key] = $value;
}
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
//not sure if I need this any more since I am using Smarty
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
$this->addTemplateVar("style", HTML::style("css/bootstrap.css"));
}
}
HelloWorldController.php
class HelloWorldController extends BaseController
{
public function showHelloWorld()
{
$this->addTemplateVar('name', 'World!');
return View::make('helloworld', $this->template_vars);
}
}
In routes.php
:
Route::get('helloworld', 'HelloWorldController@showHelloWorld');
希望它对其他人有用。