如何将Smarty 3纳入Laravel 4? [英] How to include Smarty 3 into Laravel 4?

查看:159
本文介绍了如何将Smarty 3纳入Laravel 4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手,因此仍然对这些概念很熟悉,但是我在使用Smarty方面已有大约10年的经验.因此,我想利用这一点(除了Blade似乎缺少太多我发现在Smarty中有用且易于使用的功能这一事实,无论如何,除了这个问题之外).

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).

尽管在一些论坛上,例如在此似乎不太清楚我需要做些什么才能在框架内正确使用它.更具体地说,我的问题是:

I have been looking around to find the right way of using Smarty in Laravel, and although on a few forums such as here it seems to be possible its not clear what I need to do to use it properly within the framework. More concretely my questions are:

  1. 我应该在composer.json中包括哪个Composer软件包?似乎有,其中包括Smarty本身,因为它已被修改(对此不太热衷) .并且此处,他们还建议

  1. Which Composer package should I include in my composer.json? There seems to be this which includes Smarty itself, because it was modified (not too keen about that). And here they 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 this

我应该如何配置Laravel使用Smarty视图而不是Blade视图?

How should I configure Laravel to use Smarty views instead of Blade views?

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

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.)

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

Sorry if some questions are a bit trivial.

推荐答案

好的,因此,在进行了更多研究之后,我成功地将Smarty 3轻松集成到Laravel 4中. ,因此可以发表评论或提出进一步的建议.

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 View for Laravel,并按照说明将其添加到了app/config/app.phpproviders的列表.运行php artisan config:publish latrell/smarty命令后,配置已自动创建,我可以开始了.该软件包似乎还使用了正确的Smarty库,而不是某些经过修改的模板.

I installed this Smarty 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.

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

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.

我什至修改了BaseController,以维护模板变量(例如CSS样式等)的通用列表,以将它们注入HTML中,而无需在模板中放置难看的PHP代码. (我不知道是否有更好的方法将它们直接从BaseController设置到View中,而不是期望子类将它们传递给对make方法的调用,但是我想那是不同的问题.)

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.)

下面的代码说明谁可能需要它:

Code below for who might need it:

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');

希望它对其他人有用.

Hope its useful for someone else.

这篇关于如何将Smarty 3纳入Laravel 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆