【问题标题】:is it possible to include a dynamic blade view in another blade view?是否可以在另一个刀片视图中包含动态刀片视图?
【发布时间】:2017-10-23 06:09:18
【问题描述】:

我知道传统上我们在刀片视图中包含文件,例如 @include('file.path.etc') 或@extends('file.path.etc') 但在这里我试图包含我的动态视图(其中包括包含来自数据库的数据的变量)以用作我的静态欢迎页面视图中的一个部分(这是默认的在您创建项目时建立)。当我尝试包含它时,它给了我错误,因为默认的welcome.blade.php 中无法包含变量 这是我得到的错误error picture 已经尝试过将文件包含在其中并在其中生成部分。 这是我试图包含的代码

<section class="section swatch-red-white">
        <div class="container">
            @if($continents)
                <header class="section-header underline">
                    <h1 class="headline hyper hairline">Meet the team</h1>
                    <p class="">Your bones don&#x27;t break, mine do. That&#x27;s clear. Your cells react to bacteria and viruses differently than mine. You don&#x27;t get sick, I do. That&#x27;s also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke.</p>
                </header>
                <div class="row">
                    <ul class="list-unstyled row box-list">
                        @foreach($continents as $continent)
                            <li class="col-md-4 os-animation" data-os-animation="fadeInUp" data-os-animation-delay=".0s">
                                <div class="box-hex flat-shadow box-huge">
                                    <div class="box-dummy"></div>
                                    <figure class="box-inner">
                                        <img class="svg-inject" src="{{$continent->photo ? asset($continent->photo->file) : asset('images/404.png')}}" alt="a man with a mustache">
                                        <figcaption class="box-caption">
                                            <h4>Likes</h4>
                                            <p>Coffee and Beer</p>
                                        </figcaption>
                                    </figure>
                                </div>
                                <h3 class="text-center">
                                    <a href="#">{{$continent->name}}</a>
                                </h3>
                                <ul class="list-inline text-center social-icons social-simple">
                                    <li>
                                        <a href="#" target="_self"><i class="fa fa-facebook"></i></a>
                                    </li>
                                    <li>
                                        <a href="#" target="_self"><i class="fa fa-twitter"></i></a>
                                    </li>
                                </ul>
                            </li>
                        @endforeach
                    </ul>
                </div>
            @endif
        </div>
    </section>

这是控制器的代码

public function index()
    {
        $continents = Continent::all();
        return view('admin.continent.continent',compact('continents'));
}

这里是welcome.blade.php 中我试图调用数据表单的部分。

<div id="content" role="main">
    <section class="section swatch-red-white">
        <div class="background-media" style="background-image: url({{asset('images/design/section-bg/bg-2.jpg')}}); background-repeat: ; background-size: ; background-attachment: ; background-position: ; background-size: cover" data-start="background-position: 50% 0px" data-top-bottom="background-position: 50% -200px"></div>
        <div class="background-overlay" style="background-color:rgba(231,76,60,0.8)"></div>
        <div class="container">
            <header class="section-header underline">
                <h1 class="headline hyper hairline">Welcome to One58 Travel & Tour</h1>
                <p class="big">We make your dream vacations come true</p>
            </header>
        </div>
    </section>
    <section class="section swatch-white-red has-top">
        <div class="decor-top">
            <svg class="decor" height="100%" preserveaspectratio="none" version="1.1" viewbox="0 0 100 100" width="100%" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 0 L100 100 L0 100" stroke-width="0"></path>
            </svg></div><div class="container">
            <header class="section-header underline">
                <h1 class="headline super hairline">Features</h1>
                <p class="">Angle is a uniquely designed wordpress theme. It&#x27;s super easy to modify and uses advanced CSS techniques built on the latest Bootstrap Framework.</p>
            </header>
            <div class="row">
                <ul class="list-unstyled row box-list">
                    <li class="col-md-3 text-center os-animation" data-os-animation="fadeInUp" data-os-animation-delay=".0s">
                        <div class="box-round">
                            <div class="box-dummy">
                            </div>
                            <a class="box-inner" href="#">
                                <img class="svg-inject" src="{{asset('images/design/custom-icons/custom-icon-composer.png')}}" alt="a clock" data-animation="">
                            </a>
                        </div>
                        <h3 class="text-center">
                            <a href="#">Retina ready</a>
                        </h3>
                        <p class="text-center">Angle is build to look sharp ,cool and awesome on all displays. Images will look sharp and clear.</p>
                    </li>
                    <li class="col-md-3 text-center os-animation" data-os-animation="fadeInUp" data-os-animation-delay=".3s">
                        <div class="box-round">
                            <div class="box-dummy">
                            </div>
                            <a class="box-inner" href="#">
                                <img class="svg-inject" src="{{asset('images/design/custom-icons/custom-icon-bag.png')}}" alt="a clock" data-animation="">
                            </a>
                        </div>
                        <h3 class="text-center">
                            <a href="#">Free updates</a>
                        </h3>
                        <p class="text-center">No need to worry on keeping your theme updated. With Angle you get free updates for life.</p>
                    </li>
                    <li class="col-md-3 text-center os-animation" data-os-animation="fadeInUp" data-os-animation-delay=".6s">
                        <div class="box-round">
                            <div class="box-dummy">
                            </div>
                            <a class="box-inner" href="#">
                                <img class="svg-inject" src="{{asset('images/design/custom-icons/custom-icon-iphone-white.png')}}" alt="a clock" data-animation="">
                            </a>
                        </div>
                        <h3 class="text-center">
                            <a href="#">Responsive Design</a>
                        </h3>
                        <p class="text-center">We have designed this site mobile first so it looks great on mobile devices of all sizes.</p>
                    </li>
                    <li class="col-md-3 text-center os-animation" data-os-animation="fadeInUp" data-os-animation-delay=".9s">
                        <div class="box-round">
                            <div class="box-dummy">
                            </div>
                            <a class="box-inner" href="#">
                                <img class="svg-inject" src="{{asset('images/design/custom-icons/custom-icon-clipboard.png')}}" alt="a clock" data-animation="">
                            </a>
                        </div>
                        <h3 class="text-center">
                            <a href="#">100% support</a>
                        </h3>
                        <p class="text-center">With Angle as with all our themes you will receive our 100% rock solid support.</p>
                    </li>
                </ul>
            </div>
        </div>
    </section>
    {{--@include('admin.continent.continent')--}}
    {{--@section('continent')--}}
        {{--{{View::make('continents')->with('continents', $continents)}}--}}
        {{--@endsection--}}
    <section class="section swatch-white-red has-top">
        <div class="decor-top">
            <svg class="decor" height="100%" preserveaspectratio="none" version="1.1" viewbox="0 0 100 100" width="100%" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 0 L100 100 L0 100" stroke-width="0"></path>
            </svg>
        </div>
        <div class="container">
            <header class="section-header underline">
                <h1 class="headline super hairline">Meet the team</h1>
            </header>
            <div class="row">
                <ul class="list-unstyled row box-list">
                    <li class="col-md-4 os-animation" data-os-animation="fadeInUp" data-os-animation-delay=".0s">
                        <div class="box-round flat-shadow box-big">
                            <div class="box-dummy">
                            </div>
                            <figure class="box-inner">
                                <img class="svg-inject" src="{{asset('images/design/people/man-1-800x800.png')}}" alt="a clock">
                                <figcaption class="box-caption">
                                    <h4>Likes</h4>
                                    <p>Coffee and Beer</p>
                                </figcaption>
                            </figure>
                        </div>
                        <h3 class="text-center">
                            <a href="#">John Langan</a>
                            <small class="block">Art Director</small>
                        </h3>
                        <p class="text-center">Your bones don’t break, mine do. That’s clear. Your cells react to bacteria and viruses differently than mine. You don’t get sick, I do. That’s also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke.</p>
                        <ul class="list-inline text-center social-icons social-simple">
                            <li>
                                <a href="#" target="_self">
                                    <i class="fa fa-facebook"></i>
                                </a>
                            </li>
                            <li>
                                <a href="#" target="_self">
                                    <i class="fa fa-twitter"></i>
                                </a>
                            </li>
                        </ul>
                    </li>
                    <li class="col-md-4 text-center os-animation" data-os-animation="fadeInUp" data-os-animation-delay=".3s">
                        <div class="box-round flat-shadow box-big">
                            <div class="box-dummy"></div>
                            <figure class="box-inner">
                                <img class="svg-inject" src="{{asset('images/design/people/woman-1-800x800.png')}}" alt="a clock">
                                <figcaption class="box-caption">
                                    <h4>Says</h4>
                                    <p>I like ancient stuff</p>
                                </figcaption>
                            </figure>
                        </div>
                        <h3 class="text-center">
                            <a href="#">Kate Ross</a>
                            <small class="block">Creative Director</small>
                        </h3>
                        <p class="text-center">Your bones don’t break, mine do. That’s clear. Your cells react to bacteria and viruses differently than mine. You don’t get sick, I do. That’s also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke.</p>
                        <ul class="list-inline text-center social-icons social-simple">
                            <li>
                                <a href="#" target="_self">
                                    <i class="fa fa-pinterest"></i>
                                </a>
                            </li>
                            <li>
                                <a href="#" target="_self">
                                    <i class="fa fa-instagram"></i>
                                </a>
                            </li>
                        </ul>
                    </li>
                    <li class="col-md-4 text-center os-animation" data-os-animation="fadeInUp" data-os-animation-delay=".6s">
                        <div class="box-round flat-shadow box-big">
                            <div class="box-dummy"></div>
                            <figure class="box-inner">
                                <img class="svg-inject" src="{{asset('images/design/people/man-2-800x800.png')}}" alt="a clock">
                                <figcaption class="box-caption">
                                    <h4>Moto</h4>
                                    <p>Live and let die</p>
                                </figcaption>
                            </figure>
                        </div>
                        <h3 class="text-center">
                            <a href="#">Manos Jones</a>
                            <small class="block">IOS Developer</small>
                        </h3>
                        <p class="text-center">Your bones don’t break, mine do. That’s clear. Your cells react to bacteria and viruses differently than mine. You don’t get sick, I do. That’s also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke.</p>
                        <ul class="list-inline text-center social-icons social-simple">
                            <li>
                                <a href="#" target="_self">
                                    <i class="fa fa-facebook-square"></i>
                                </a>
                            </li>
                            <li>
                                <a href="#" target="_self">
                                    <i class="fa fa-dribbble"></i>
                                </a>
                            </li>
                            <li>
                                <a href="#" target="_self">
                                    <i class="fa fa-google-plus-square"></i>
                                </a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </section>
    <section class="section swatch-red-white has-top">
        <div class="decor-top">
            <svg class="decor" height="100%" preserveaspectratio="none" version="1.1" viewbox="0 0 100 100" width="100%" xmlns="http://www.w3.org/2000/svg">
                <path d="M0 100 L100 0 L100 100" stroke-width="0">
                </path>
            </svg>
        </div>
        <div class="container">
            <div class="row">
                <div id="slider-flex3" class="flexslider text-left" data-flex-speed="7000" data-flex-animation="slide" data-flex-controls="hide" data-flex-directions="show" data-flex-controlsalign="center" data-flex-captionhorizontal="alternate" data-flex-captionvertical="bottom" data-flex-controlsposition="" data-flex-directions-type="">
                    <ul class="slides">
                        <li>
                            <figure>
                                <img style="height: 150px; width:150px " src="{{asset('images/design/vector/slide-1.png')}}" alt="some image">
                                <figcaption>
                                    <h3>Colorful and clean</h3>
                                    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae, iure voluptatem dolore pariatur deleniti nihil cupiditate vel sunt consectetur.</p>
                                </figcaption>
                            </figure></li>
                        <li>
                    </ul>
                </div>
            </div>
        </div>
    </section>
    <section class="section swatch-red-white has-top">

这是大陆的模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Continent extends Model
{
    protected $fillable = [
        'name','photo_id',
    ];
    public function tour(){
        return $this->belongsTo('App\Tour');
    }
    public function photo(){
        return $this->belongsTo('App\Photo');
    }
}

【问题讨论】:

  • 您是否在 web.php 文件中使用 View::make('continents'); 来渲染此刀片?我很确定无论您创建什么视图,都必须将您的变量与它一起传递。
  • 是的,我在welcome.blade.php 中尝试过,但没有成功。
  • 我要问的是你是如何导航到页面的?因为当您导航到页面时,您必须将变量传递给页面。除非您将其声明为全局。
  • 这就是我要问的问题,真正的问题是,当刀片文件包含在另一个地方时,你将如何传递在一个地方定义的变量来工作???
  • 你在哪里定义变量?如果您在后端定义它们,那么您将使用 View::make('continents')-&gt;with('continents', $continents); 发送它们

标签: php laravel laravel-5 laravel-5.4 laravel-blade


【解决方案1】:

您可以通过三种方式与视图共享数据:

将数据传递给视图

传递一个数据数组来查看:

return view('greetings', ['name' => 'Victoria']);

或者,使用with 方法:

return view('greeting')->with('name', 'Victoria');

与所有视图共享数据

有时,您可能需要与应用程序呈现的所有视图共享一条数据。您可以使用视图外观的 share 方法来执行此操作。通常,您应该在服务提供者的引导方法中调用共享。您可以随意将它们添加到 AppServiceProvider 或生成单独的服务提供者来容纳它们。

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('key', 'value');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

查看作曲家

如果您希望在每次呈现视图时将数据绑定到该视图,则视图合成器可以帮助您将该逻辑组织到一个位置。

  1. 在服务提供者中注册视图编辑器
  2. 定义作曲家类

您还可以通过将视图数组作为第一个参数传递给 composer 方法,一次将视图编辑器附加到多个视图。

例如见https://laravel.com/docs/5.5/views#view-composers

【讨论】:

    【解决方案2】:

    从生成大陆列表的任何位置执行这行代码。

    View::make('continents')->with('continents', $continents);
    

    【讨论】:

    • 还是不行,我是这样通过的 @include('admin.continent.continent') @section('continent') {{View::make('continents')- >with('continents', $continents)}} @endsection
    • 抱歉,我不太确定您要做什么。传统的方法是让 Vue.js 处理一个 ajax 请求并渲染你的 html,但是因为我不知道你是如何渲染页面的,所以我不知道你什么时候设置 $continents 变量,你在哪里发送它来自或类似的东西。如果您想得到明确的答案,您可能需要提供更多信息。
    【解决方案3】:

    您应该将大洲作为参数传递给包含的视图

    @include('admin.continent.continent', ['continents'=>$continents])
    

    【讨论】:

    • 有可能 Continent::all() 产生了一些值,compact 无法处理,因此视图无法获取值。
    • 我也不明白你为什么在控制器中调用'admin.continent.continent'视图而不是'welcome'
    • 不,只有 name 和 photo_id 应该是紧凑的,因为相同的代码正在另一个页面上工作。我在控制器中使用“admin.continent.continent”,因为我想在不同的地方有相同的视图。我正在尝试在welcome.blade.php中包含“admin.continent.continent”视图
    猜你喜欢
    • 2020-03-14
    • 2022-10-13
    • 2016-12-29
    • 2013-07-29
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 2013-08-03
    相关资源
    最近更新 更多