Laravel - 如何将变量传递给布局局部视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38418864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:10:15  来源:igfitidea点击:

Laravel - How to pass variable to layout partial view

phplaravelmodel-view-controller

提问by pleasega

I have a partial view in master layout which is the navigation bar. I have a variable $userApps. This variable checks if the user has enabled apps (true), if enabled then I would like to display the link to the app in the navigation bar.

我在主布局中有一个局部视图,即导航栏。我有一个变量$userApps。此变量检查用户是否启用了应用程序 (true),如果启用,那么我想在导航栏中显示应用程序的链接。

homepage extends master.layout which includes partials.navbar

主页扩展了 master.layout,其中包括 partials.navbar

My code in the navbar.blade.php is this:

我在 navbar.blade.php 中的代码是这样的:

@if ($userApps)
    // display link
@endif

However I get an undefined variable error. If I use this in a normal view with a controller it works fine after I declare the variable and route the controller to the view. I dont think I can put a controller to a layout since I cant route a controller to a partial view, so how do I elegantly do this?

但是我得到一个未定义的变量错误。如果我在带有控制器的普通视图中使用它,则在声明变量并将控制器路由到视图后,它可以正常工作。我不认为我可以将控制器放置到布局中,因为我无法将控制器路由到局部视图,那么我该如何优雅地做到这一点?

回答by Yuri Tkachenko

What version of Laravel you use? Should be something like this for your case:

你用的是什么版本的 Laravel?对于您的情况,应该是这样的:

@include('partials.navbar', ['userApps' => $userApps])

Just for a test purpose, I did it locally, and it works:

仅出于测试目的,我在本地进行了操作,并且可以正常工作:

routes.php

路由文件

Route::get('/', function () {
    // passing variable to view
    return view('welcome')->with(
        ['fooVar' => 'bar']
    );
});

resources/views/welcome.blade.php

资源/视图/welcome.blade.php

// extanding layout
@extends('layouts.default')

resources/views/layouts/default.blade.php

资源/视图/布局/default.blade.php

// including partial and passing variable
@include('partials.navbar', ['fooVar' => $fooVar])

resources/views/partials/navbar.blade.php

资源/视图/部分/navbar.blade.php

// working with variable
@if ($fooVar == 'bar')
  <h1>Navbar</h1>
@endif

So the problem must be in something else. Check your paths and variable names.

所以问题一定出在别的地方。检查您的路径和变量名称。

回答by partho

This approach is very simple:

这种方法非常简单:

In parent view :

父视图中:

@include('partial.sub_view1', ['This is value1' => $var1])

In sub view :

子视图中:

{{ $var1 }}

回答by Mohammad Golmoradi

@include('your view address', array('variable in your view for example: id=$something ' => 'value for this variable'))

@include('您的视图地址', array('您视图中的变量,例如:id=$something ' => '此变量的值'))

回答by gpsugy

The other answers did not work for me, or seem to only work for older versions. For newer versions such as Laravel 7.x, the syntax is as follows.

其他答案对我不起作用,或者似乎只适用于旧版本。对于 Laravel 7.x 等较新的版本,语法如下。

In the parent view:

在父视图中:

@include('partial.sub_view', ['var1' => 'this is the value'])

In the sub view:

在子视图中:

{{ $var1 }}