繁体   English   中英

从包含在 php 刀片模板中访问变量

[英]Access variable from include in blade template in php

我有一个刀片模板,其中包含其他刀片模板。 这是名为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传递navaccount.blade.php

@include传递变量的正确方法是什么以及如何访问传递的变量?

在您正在调用的 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM