php - 如何将 Smarty 3 包含到 Laravel 4 中?

标签 php laravel smarty

我是 Laravel 的新手,所以还在习惯这些概念,但是我有大约 10 年的 Smarty 使用经验。所以我希望利用这一点(除了 Blade 似乎缺少太多我发现有用的功能并且在 Smarty 中开箱即用的事实之外,但无论如何除了这个问题的重点)。

我一直在四处寻找在 Laravel 中使用 Smarty 的正确方法,尽管在 here 等一些论坛上似乎不清楚我需要做什么才能在框架内正确使用它。更具体地说,我的问题是:

  1. 我应该在我的 composer.json 中包含哪个 Composer 包?似乎有 this 包括 Smarty 本身,因为它被修改了(不太热衷于此)。而 here 他们也建议 http://bundles.laravel.com/bundle/SmartyView 。不确定它是否相同,因为 laravel.com 的 bundles 子域甚至都没有出现。几天前它曾经出现过,但我不知道他们是否将其关闭,因为 bundle 现在已经过时并被包裹取代......不确定。还有this

  2. 我应该如何配置 Laravel 以使用 Smarty View 而不是 Blade View ?

  3. 鉴于 Laravel 使用 REST 风格的漂亮 URL,我应该如何从 Smarty 中包含 CSS 和 JS 文件 View ,以便它们的路径由 Laravel 动态设置?在 Blade 中,您可以执行以下操作:{{ HTML::style('css/style.css') }}。我可以使用类似的东西吗?或者更好的方法是通过调用 HTML 类从代码中设置一个模板变量? (我真的不喜欢在模板中调用 PHP 代码,这些代码应该只执行表示逻辑。)

抱歉,如果有些问题有点琐碎。

最佳答案

好的,经过更多研究后,我设法轻松地将 Smarty 3 集成到 Laravel 4 中。我不确定这是否是最好的方法,但它工作完美,所以欢迎评论或进一步的建议。

我安装了 this Laravel 的 Smarty View 并按照说明将其添加到 app/config/app.php 中的 providers 列表中。运行 php artisan config:publish latrell/smarty 命令后,配置自动创建,我就可以开始了。这个包似乎也使用了适当的 Smarty 库而不是一些修改过的模板。

然后我创建了一个普通的旧 HTML 文件,在 app/views 目录中有一个 .tpl 扩展名,在 app/中有一个相应的 Controller controllers 目录,以及 routes.php 中的相应路由,嘿,很快,它工作顺利。

我什至修改了 BaseController 以维护一个通用的模板变量列表(例如 CSS 样式等)以注入(inject) HTML,而无需在模板中放置丑陋的 PHP 代码。 (我不知道是否有更好的方法将它们从 BaseController 直接设置到 View 而不是期望子类在对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);
    }
}

routes.php 中:

Route::get('helloworld', 'HelloWorldController@showHelloWorld');

希望它对其他人有用。

关于php - 如何将 Smarty 3 包含到 Laravel 4 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25900826/

相关文章:

php - Smarty:如何修复 "unknown modifier ' 重写'”异常?

php - 如何使用 smarty 正确显示数组中的数据?

javascript - smarty模板引擎可以用来在客户端创建模板吗?

php - 如何使用 PHP 在 MongoDB 中按时间查询?

php - PDO - SQL COUNT 在 PHP 函数中不起作用

php - Laravel Eloquent 链式方法返回 true 而不是模型

php - Laravel 5.2 中的简单标签系统

php - laravel Blade 模板不呈现

laravel - 如何在测试完成后修复 "Can' t 关闭模拟,它在另一个测试中可见”

php - AWS S3 无法通过 PHP SDK 删除存储桶中的对象