1

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?

1 Answer 1

1

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())
Sign up to request clarification or add additional context in comments.

8 Comments

This was just an example. The actual controller logic I'll need is a lot more complicated, so doing just a regular if doesn't work... Which would lead me to write a lot of PHP in a view.
Sure. I have two arrays, $a and $b and, for each @include('item', array('item' => $item)) i will need to test if that particular item belongs to $a or $b, and set a few flags accordingly, that will change what the layout looks like.
Right now I'm doing something similar: I have a static method that gets $a, $b and the item and returns what I need ... however this seems a bit silly and too much of a burden - I'm not really using the class per se, I'm just hacking my way into having some logic behind an include. I was hoping there could be an alternative for it.
I have to confess, I don't really know how it works with symfony (and a quick google search couldn't help me with that). So why don't you quickly explain that to me. Does the controller render the actual html is it just "preparing" objects for another view?
Imagine that you could do something like @include('item', array('controller' => 'HomeController@parseItem'), array('stuff' => $stuff)) ... your parseItem($stuff) would deal with $stuff, and you could return whatever new $vars you needed, that would be available in 'item'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.