【问题标题】:Laravel 4 - Render recursive @includeLaravel 4 - 渲染递归 @include
【发布时间】:2014-10-25 22:54:37
【问题描述】:

我想获取带有递归@include 标记的模板的渲染字符串。不幸的是,render() 方法似乎不支持递归:

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

这是我的看法:

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

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

{{$tab}})

这是我应该得到的:

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

这是我得到的:

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

尽管如此,如果我显示视图而不是渲染它,我可以看到正确的源代码。

有没有办法递归渲染视图?

【问题讨论】:

  • 把@指令放在不同的行上?我相信有一个限制,规定每行一个 @ 事物。
  • @bishop 简直不敢相信这么简单,谢谢!请添加您的答案,我会接受的:)
  • 你去!欣赏!

标签: php laravel laravel-4 template-engine blade


【解决方案1】:

Laravel 要求 @ 指令出现在不同的行上。换句话说,每行只有一个 at 标志。在某些情况下,Laravel 会给你一个编译错误:在其他情况下,你只会得到神秘的结果(就像你的情况一样)。

所以,重写你的代码如下,它应该可以工作:

@if (count($decorators) < $i)
    @include('bind')
@endif

【讨论】:

    猜你喜欢
    • 2020-09-08
    • 2015-06-07
    • 2016-06-04
    • 2014-01-31
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 2019-12-10
    相关资源
    最近更新 更多