访问变量来自php中的刀片模板

时间:2016-05-26 04:43:15

标签: php laravel-5.1 laravel-blade

我有一个包含其他刀片模板的刀片模板。 这是名为accountnav.blade.php

的主模板
@if (count($subaccounts) > 0)
   <ul class="nav navbar-nav">
       @foreach ($subaccounts as $account)
           <?php $p_link = $account->link?>
           @include('frontend.template.subaccount', array('p_link' => $p_link))
       @endforeach
   </ul>
@endif

以下是标题为subaccount.blade.php

的附带模板
<li {{count($account->children) > 0 ? "class = dropdown" : ""}}>
        @if(count($account->children) == 0 )
            <a href="{{$account->link}}">{{$account->name}}</a>
        @else
            <a class="dropdown-toggle" data-toggle="dropdown" href="#" onclick="return false;" aria-expanded="false">
            {{$account->name}}
                <i class="icon-caret-down"></i>
            </a>
        @endif
    @if (count($account->children) > 0)
        <ul class="dropdown-menu">
            @foreach($account->children as $account)
                <?php $p_link .= $account->link?> //the $p_link is not defined here
                 @include('frontend.template.subaccount', array('p_link' => $account->link))
            @endforeach
        </ul>
    @endif
</li>

但是,当我尝试访问语句p_linksubaccount.blade.php中的变量<?php $p_link .= $account->link?>时,尽管从the variable $p_link is not defined传递了navaccount.blade.php,但仍有错误显示@include {1}}。

在{{1}}中传递变量以及如何访问传递的变量的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

在您调用的subaccount.blade.php文件的foreach循环中

@foreach($account->children as $account)

您正在覆盖孩子的$ account对象。

你应该把它改成

@foreach($account->children as $child)

我不确定你为什么要命名p_link值,因为你从不在视图中使用p_link。为什么不像这样传递它

@foreach($account->children as $child)
    @include('frontend.template.subaccount', array('account' => $child))
@endforeach