doujiazong0322
2014-11-20 16:11
浏览 62
已采纳

Laravel:包含带控制器逻辑的视图

I come from the symfony (v1) world, where we had two types of partial rendering: partials, which loaded the same as:

@include('some.view')

and controllers, which would act the exact same way but would be ran with some controller logic behind hit. So, calling the same as above would first go to the matching some.view controller, and operate with the logic it had.

I'm trying to do the same with Laravel. Basically, I have this:

@foreach($array as $thing)
  @include('controller.like.view', array('thing' => $thing))
@endforeach

... and I'd like my included view to run something like this (this is just an example, the actual code is a lot more complicated, otherwise I'd just write it with an if clause in Blade):

...
if ($thing%2) {
      return 'a';
}

return 'b';

... so that only 'a' or 'b' would be printed in my loop. What's the best way to achieve this without having a bunch of PHP code in a Blade template?

图片转代码服务由CSDN问答提供 功能建议

我来自symfony(v1)世界,我们有两种类型的局部渲染:partials,它们加载了 相同如下:

  @include('some.view')
   
 
 

和控制器,它们将起作用 完全相同的方式,但将在命中后面的一些控制器逻辑运行。 因此,调用与上面相同的方法将首先进入匹配的some.view控制器,并使用它具有的逻辑进行操作。

我正在尝试对Laravel执行相同的操作。 基本上,我有这个:

  @foreach($ array as $ thing)
 @include('controller.like.view',array('thing'=>  $ thing))
 @ endforeach 
   
 
 

...我希望我的包含视图运行这样的东西(这只是一个例子,实际 代码要复杂得多,否则我只用Blade中的if子句写它:

  ... 
if($ thing%2){
 返回'a'; 
} 
 
返回'b'; 
   
 
 

...以便只打印'a'或'b' 我的循环。 如果没有刀片模板中的大量PHP代码,最好的方法是什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douquan9826 2014-11-20 16:16
    最佳回答

    Why not just like that?

    @foreach($array as $thing)
        @if($thing%2)
            a
        @else
            b
        @endif
    @endforeach
    

    In general though, it's mostly a good way to prepare the data in your controller before passing it to the view. This way the view is just for presenting the data.

    You could also write a little helper function or even a full class (with optional Facade for easy access) But it really depends on your needs

    Update

    I'm not sure this is the best solution for you but it's the only one I can think of.
    Put as much of the logic as you can in your "thing" class and then use that in the included view. Here's an example:

    class Thing {
        public function isA(){
            // do the magic
            return true;
        }
    }
    

    View

    @if($this->isA())
        a
    @else
        b
    @endif
    

    Update 2

    Or to make a bit more like the controller from symfony you described:

    class Thing {
        public function getVars(){
            // do stuff
            return array(
                    'all' => 'the',
                    'vars' => 'you',
                    'need' => 'in',
                    'the' => 'view'
                );
        }
    }
    

    And then when you include the view

    @include('item', $thing->getVars())
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题