- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚开始学习 Laravel,我遇到了一个关于跨文件传递值的小问题。
在路由文件中,我有以下函数。
Route::get('/', function()
{
$data = [
'first_name' => 'Jane',
'last_name' => 'Doe',
'email' => 'jane@hotmail.com',
'location' => 'London'];
return View::make('hello')->with($data);
});
我将 $data
数组传递给名为 hello.blade.php 的文件。我想打印出这个数组中的所有值。问题是我无法遍历它们并输出其中的值。我收到错误 Undefined variable: data。
这是我的 Blade 文件。
@extends('layouts.main')
@section('content')
@foreach ($data as $item)
<li>{{{ $item }}}</li>
@endforeach
@stop
我了解到我可以在 Route 文件中执行类似这样的操作 return View::make('hello')->withData($data);
并让它工作。但我不喜欢像 withData
这样附加变量名的方式。
有没有办法传递数组变量并从 Blade 文件访问它?
谢谢。
最佳答案
你传递了一个关联数组的参数,这告诉 Blade:嘿,把这个数组的键作为变量的名称,并使它们的值对应于数组中键的值。
这意味着,您现在在 View 中有一个值为“Jane”的变量 $first_name
,一个值为“Doe”的变量 $last_name
和等等。
这和做的一样
return View::make('hello')
->with('first_name', 'Jane')
->with('last_name', 'Doe');
你明白了。
如果你想传递数组本身,你必须告诉 blade:嘿,获取这个数组并通过给定名称使其在 View 中可用:
return View::make('hello')->with('data', $data);
现在您可以通过变量 $data
在 View 中使用整个数组。
关于php - 将数组传递给 Blade 文件并访问其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26444628/
我知道 Laravel 的 Blade 模板位于/resource/views 目录中。它的扩展名是 .blade.php 。 我的问题是我已经为前端设计人员设置了一个单独的 repo 来处理 bla
我知道 Laravel 的 Blade 模板位于/resource/views 目录中。它的扩展名是 .blade.php 。 我的问题是我已经为前端设计人员设置了一个单独的 repo 来处理 bla
在我的数据库中,我保存包含 Blade 标记的文本,例如: Hello {!! $name !!} how are you today. 我将此文本传递到变量 $text 中的电子邮件模板。在我使用的
我想从3个表构建 Blade View : “inputs_details”-字段:article_type(值:“p”代表产品,'s'代表服务),article_id,...。 “产品”-字段:ID
我正在使用 Laravel 5.1。我正在尝试将 Blade 指令 (@extend) 与我的自定义 Blade 指令一起使用。 Blade::directive('base', function()
我的 Blade 中有一个表格,其中包含 2 个日期供用户选择,例如: {{csrf_field()}} From
我正在尝试使用表单生成器创建一个按钮 {!! Form::button(' 'yourstyle']); 关于php - 在 Blade 代码中插入 Blade 代码,我们在Stack Overflo
我正在渲染一个页面,该页面主要是 Laravel 中带有 view::make 的表单,它正在崩溃,导致 ERR_CONNECTION_RESET。经过长时间的调查和许多红鲱鱼,我开始从 Blade
我有以下路径的部分 View 。 /views/acp/review/check.details.blade.php 我如何使用 Blade 包含这样的文件,因为如果我这样做 @include('vi
我继承了一个 Laravel 项目,该项目有许多 Blade 文件, Blade 文件中的标签内带有 javascript。问题是有很多重复的 JS 逻辑,所以我想提取 JS 并创建 JS 文件以包含
我正在为 laravel 项目中基于 Blade 的组件创建文档,并希望将语法突出显示的 Blade 代码片段显示为文档的一部分,就像这样: 我已经安装了 graham-campbell/markdo
在我激活 Facade 的 Lumen 应用程序中($app->withFacades() in app's bootstrap)我无法在 Blase 模板中使用 URL 类。 {{ URL::pre
我正在为 laravel 项目中基于 Blade 的组件创建文档,并希望将语法突出显示的 Blade 代码片段显示为文档的一部分,就像这样: 我已经安装了 graham-campbell/markdo
在我激活 Facade 的 Lumen 应用程序中($app->withFacades() in app's bootstrap)我无法在 Blase 模板中使用 URL 类。 {{ URL::pre
有人知道是否可以延长 child Blade 吗? 我的应用程序有一个通用的布局模板,然后每个页面都从该模板@extends。每个页面都可以根据需要为其他 HTML block (例如模态)引入一系列
我试图在 blade 中设置一个 php 变量。它可以在同一个 Blade 上工作,而不是在其他 Blade 上设置 var。 例子: header.blade.php @if(isset($comp
情况: 我正在制作一个侧面菜单,该菜单通过从表 (MySQL) 中获取数据来填充。 每代表菜单的一项: item 1 item 2 item 3 等等... 问题: 当我看到它的 Blade 时一切正
我在主视图中有 3 个元素 标题(导航栏) 内容 页脚 在 app.blade.php 中 @include('header') @yield('content')
我想将输入值从一个 Blade 文件传递到另一个 Blade 文件。 我是 PHP Laravel 的新手,在尝试使用它时遇到错误。 我认为我的语法在这里是错误的。有人可以帮忙吗? channel .
所以我的困境是这样的。 Email: info@example.com 正在作为 Blade 代码处理,不会在我的 Laravel 4 框架中的响应式 Bootstrap 网页中调整大小。 关于如何让
我是一名优秀的程序员,十分优秀!