Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 - Render recursive @include

I'd like to get the rendered string of a template with recursive @include tags. Unfortunately it seems that the render() method doesn't support recursivity:

return View::make('bind', $data)->render();

This is my view:

{{$namespace}}\Decorators\{{$decorators[$i++]}}
<?php $tab = str_repeat("\t", $i) ?>

{{$tab}}(
    {{$tab}}new @if(count($decorators) < $i)@include('bind')@endif

{{$tab}})

This is what I should get:

Workflows\Decorators\Foo
(
    new Workflows\Decorators\Bar
    (
        new 
    )
)

This is what I get:

Workflows\Decorators\Foo
(
    new @include('bind')
)

Nevertheless if I display the view instead of rendering it, I can see the correct source code.

Is there a way to render views recursively?

like image 540
Andrea Marco Sartori Avatar asked Mar 24 '26 18:03

Andrea Marco Sartori


1 Answers

Laravel requires that @ directives appear on separate lines. Said another way, only one at sign per line. In some cases, Laravel gives you a compile error: in others, you just get mysterious results (as was your case).

So, rewrite your code as below and it should work:

@if (count($decorators) < $i)
    @include('bind')
@endif
like image 166
bishop Avatar answered Mar 26 '26 08:03

bishop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!