[英]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_link
在subaccount.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.