0

我有一个使用 laravel4 及其刀片视图引擎的项目。有时我需要通过视图文件调用控制器方法来输出动态数据;顺便说一句,这一次它调用了为页面生成 javascript 代码的方法。不管这是否是最好的处理方式,因为我只是从 L3 升级到 L4,这只是一个有争议的问题。

我的观点类似于:

@extends('en/frontend/layouts/default')

{{-- Page title --}}
@section('title')
    Page Title
    @parent
@stop

{{-- Page content --}}
@section('pageContent')
    ...
@stop

{{-- Scripts --}}
@section('scripts')
    <?php echo CaseStudy::script(); ?>
@stop

我已经将 CaseStudy 设置为通过 laravel 外观加载,当前的类很简单:

class CaseStudy
{

    public function display()
    {

    }

    /**
     * Returns the javascript needed to produce the case study
     * interactivity
     *
     * @return \Illuminate\View\View|void
     */
    public function script()
    {
        $language = \Config::get('app.locale');

        $scriptElementView = $language . '.frontend.elements.casestudy_script';

        if ( ! \View::exists($scriptElementView))
        {
            $scriptElementView = "Training::" . $scriptElementView;
        }

        return \View::make($scriptElementView, array());
    }
}

似乎是响应 CaseStudy::script 的响应是导致空白正文的原因;但是没有进一步的错误消息,我不知道发生了什么。我认为这是因为我的静态 CaseStudy 的 View 实例与刀片引擎正在使用的实例冲突。我将如何让 CaseStudy::script() 返回呈现视图的字符串形式?

谢谢你。

4

1 回答 1

2

在你看来

{{-- Scripts --}}
@section('scripts')
    {{ CaseStudy::script() }}
@stop

在你的图书馆

class CaseStudy
{
    public function script()
    {
         $string = "your script here";
         return $string;
    }
}

注意 - CaseStudy 应该是真正的“图书馆”或“助手”或其他东西。不是控制器 - 并不真正符合 MVC 方法。

于 2013-08-01T12:18:08.300 回答