如何将 Smarty 3 包含到 Laravel 4 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25900826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include Smarty 3 into Laravel 4?
提问by jbx
I am new to Laravel, so still getting used to the concepts, however I have around 10 years of experience using Smarty. So I wish to capitalize on that (apart from the fact that Blade seems to lack too many features I find useful and ready out of the box in Smarty, but anyway besides the point of this question).
我是 Laravel 的新手,所以仍然习惯了这些概念,但是我有大约 10 年的 Smarty 使用经验。所以我希望利用这一点(除了 Blade 似乎缺乏太多我觉得有用的功能,并且在 Smarty 中准备就绪,但无论如何,除了这个问题的重点之外)。
I have been looking around to find the right way of using Smarty in Laravel, and although on a few forums such as hereit seems to be possible its not clear what I need to do to use it properly within the framework. More concretely my questions are:
我一直在四处寻找在 Laravel 中使用 Smarty 的正确方法,尽管在一些论坛上,例如here似乎可能不清楚我需要做什么才能在框架内正确使用它。更具体地说,我的问题是:
Which Composer package should I include in my
composer.json
? There seems to be thiswhich includes Smarty itself, because it was modified (not too keen about that). And herethey also suggest http://bundles.laravel.com/bundle/SmartyView. Not sure if it is the same, because the bundles sub-domain of laravel.com isn't even coming up. It used to come up a few days ago, but I don't know if they turned it off because bundles are now obsolete and replaced by packages... not sure. There is also thisHow should I configure Laravel to use Smarty views instead of Blade views?
Given that Laravel uses REST style pretty URLs, how should I include CSS and JS files from Smarty views, such that the path to them is dynamically set by Laravel? In Blade you do something like:
{{ HTML::style('css/style.css') }}
. Can I use something similar? Or better still set a template variable from the code by calling the HTML class? (I don't really like calling PHP code within templates that should just be doing presentation logic.)
我应该在我的
composer.json
? 似乎有这个包括 Smarty 本身,因为它被修改了(不太热衷于此)。而在这里,他们也建议http://bundles.laravel.com/bundle/SmartyView。不确定它是否相同,因为 laravel.com 的 bundles 子域甚至没有出现。几天前它曾经出现过,但我不知道他们是否关闭了它,因为捆绑包现在已经过时并被包所取代......不确定。还有这个我应该如何配置 Laravel 以使用 Smarty 视图而不是 Blade 视图?
鉴于 Laravel 使用 REST 风格的漂亮 URL,我应该如何从 Smarty 视图中包含 CSS 和 JS 文件,以便 Laravel 动态设置它们的路径?在刀片你做这样的事情:
{{ HTML::style('css/style.css') }}
。我可以使用类似的东西吗?或者最好还是通过调用 HTML 类从代码中设置模板变量?(我真的不喜欢在模板中调用 PHP 代码,这些代码应该只是做表示逻辑。)
Sorry if some questions are a bit trivial.
对不起,如果有些问题有点微不足道。
回答by jbx
OK so after some more research, I managed to painlessly integrate Smarty 3 within Laravel 4. I am not sure if it is the best way, but its working perfectly, so open to comments or further suggestions.
好的,经过更多研究,我设法轻松地将 Smarty 3 集成到 Laravel 4 中。我不确定这是否是最好的方法,但它工作得很好,所以欢迎评论或进一步的建议。
I installed thisSmarty View for Laravel and followed the instructions to add it in the list of providers
in app/config/app.php
. After running the php artisan config:publish latrell/smarty
command the configuration was automatically created and I was ready to go. This package also seems to use the proper Smarty libraries rather than some modified templates.
我为 Laravel安装了这个Smarty View 并按照说明将它添加providers
到app/config/app.php
. 运行php artisan config:publish latrell/smarty
命令后,配置自动创建,我准备好了。这个包似乎也使用了适当的 Smarty 库而不是一些修改过的模板。
I then just created a plain old HTML file, with a .tpl
extension in the app/views
directory, and a corresponding controller in the app/controllers
directory, together with the corresponding route in routes.php
and hey presto, it worked without a hitch.
然后我刚刚创建了一个普通的旧 HTML 文件,.tpl
在app/views
目录中有一个扩展名,在目录中有一个相应的控制器app/controllers
,以及相应的路由 inroutes.php
和嘿presto,它工作顺利。
I even modified the BaseController to maintain a common list of template variables (such as the CSS styles etc.) to inject in the HTML without putting ugly PHP code in the template. (I don't know if there's a better way to set them directly into the View
from the BaseController
rather than expecting the sub-class to pass them in the call to the make
method, but I guess thats a different question.)
我什至修改了 BaseController 来维护一个通用的模板变量列表(例如 CSS 样式等),以便在 HTML 中注入而不在模板中放置丑陋的 PHP 代码。(我不知道是否有更好的方法将它们直接设置到View
from 中,BaseController
而不是期望子类在对make
方法的调用中传递它们,但我想那是一个不同的问题。)
Code below for who might need it:
以下代码供可能需要它的人使用:
HelloWorld.tpl
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
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
HelloWorldController.php
class HelloWorldController extends BaseController
{
public function showHelloWorld()
{
$this->addTemplateVar('name', 'World!');
return View::make('helloworld', $this->template_vars);
}
}
In routes.php
:
在routes.php
:
Route::get('helloworld', 'HelloWorldController@showHelloWorld');
Hope its useful for someone else.
希望它对其他人有用。