Laravel - 加载常用页眉和页脚以查看

问题描述 投票:0回答:4

我是 Laravel 的新手,我正在尝试在通用模板中从控制器加载页眉、页脚和视图文件,并在视图文件中显示来自控制器的数据。但我收到错误

未找到视图['admin.dashboard']。

仪表板文件位于视图内的管理文件夹中。

控制器

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class common extends Controller
{

   public function login()
   {
        $data['title'] = 'Dashboard';
        $data['template'] = 'admin/dashboard';
        return view('common_template', compact('data'));

   }
}

common_template.blade 视图

<?php echo View::make('includes/header'); ?>

<?php echo $template = "'".$data['template']."'";
echo View::make($template); ?>
<?php echo View::make('includes/footer'); ?>

当我直接在

$data['template']
中添加“admin/dashboard”而不是
$template
时,它会加载仪表板文件,而当我从控制器将其作为字符串传递时,它不会加载。

仪表板.刀片视图

<p><?php echo $data['title']; ?></p> // Printing the data from the controller
php laravel laravel-5
4个回答
18
投票

要将 Blade 模板包含到另一个模板中,请使用

@include
:

@include('admin.dashboard')

或者

@include($data['template']) // This should be the name of template, like 'admin.dashboard', but not path

另外,检查视图是否具有正确的名称并且位于正确的目录中:

resources/views/admin/dashboard.blade.php

13
投票

首先,您的代码需要根据 Laravel Blade 代码标准进行更正。尝试以下代码:

common_template.blade 视图

@include('includes.header')

@yield('content')

@include('includes.footer')

仪表板.刀片视图

@extends('common_template')

@section('content')
    {{$data['title']}}
@endsection

8
投票

要将 Blade 模板包含到另一个模板中,

布局/index.blade.php

<!DOCTYPE html>
<html lang="en" class="no-js">

<head>
    <!-- Site Title -->
    <title>{{ $title }}</title> // Dynamic title
    <link rel="stylesheet" href="{{ asset('website/css/main.css') }}">
@stack('css') // Internal CSS
</head>

<body>
    @include('../website/layouts/header') // Include header
    @yield('content') // Include content
    @include('../website/layouts/footer') // Include footer
    <!-- Start footer Area -->
    <!-- End footer Area -->
    <script src="{{asset('website/js/vendor/jquery-2.2.4.min.js ') }}"></script>
    @stack('js') // Internal js
</body>

</html>

布局/footer.blade.php

// Footer code
<h1>This area for footer code

布局/header.blade.php

// Header code
<h1>This area for headercode

/home.blade.php

<?php $title = "dynamic title"; ?> // Title

@extends('layouts/index') // Include index page

@Push('css') // This is for internal js
*{
    color: black;
}
@endpush

@section('content') // Section for content
This area for home page content
@stop // Content ended

@Push('js') // This is for internal js
<script>
    $(document).ready(function() {
        var loggedIn = {!! json_encode(Auth::check()) !!};
        $('.send').click(function() {
            if(!loggedIn) {
                moda.style.display = "block";
                return false;
            }
        });
    });
@endpush

0
投票

您可以使用此两个选项制作公共视图

在 resources/views/includes/ 中创建“includes”文件夹

  1. 创建文件1:header.blade.php
  2. 创建文件 2:footer.blade.php

此文件应在包含文件夹之外创建

  1. 创建文件3:home.blade.php

home.blade.php 内部

选项1:使用include方法

@include('includes.header')

<Add home page code here>

@include('includes.footer')

选项2:使用扩展方法

  1. 创建一个通用文件 - template.blade.php

template.blade 文件内

@include('includes.header')

@yield('content')

@include('includes.footer')
  1. 在 home.blade.php 文件中
@extends('template')

@section('content')
    {{$data['name']}}
@endsection

在此方法中,@section将在模板内容部分使用yield方法调用并替换内容。

© www.soinside.com 2019 - 2024. All rights reserved.