laravel之外的Blade模板。

v2.1.1 2020-02-29 08:42 UTC

This package is auto-updated.

Last update: 2024-08-29 04:09:12 UTC


README

允许您在Laravel之外使用blade模板的包。

Build Status

使用说明

Blade构造函数接受4个参数,其中2个是可选的

$viewPaths: // either a string or array of paths where your views will be fetched from
$cachePath: // string representing the path to the cache directory (to store cached version of the views)
Container $app = null: // instance of the Illuminate\Container\Container (optional)
Dispatcher $events = null: // instance of the Illuminate\Events\Dispatcher (optional)
Filesystem $events = null: // instance of the Illuminate\Events\Filesystem (optional)

使用Blade类的新实例,您可以通过view()助手以与在Laravel内部相同的方式调用view()方法。

$blade = new Blade(
    realpath(__DIR__ . '/../resources/views'),
    realpath(__DIR__ . '/../resources/cache')
);

传递变量

$user = User::find(1);

$blade->view('index', compact('user'));

$blade->view('index', ['user' => $user]);

$blade->view('index')->with('user', $user);

确定视图是否存在

$blade->view()->exists('test');

与所有视图共享数据

$blade->share('user', $user);

视图构建器

$blade->view()->composer('dashboard', function(View $view) {

    $user = new stdClass;
    $user->name = 'Martin';

    $view->with('user', $user);

});

$blade->view('dashboard');
// has instance of $user available

Blade视图模板

Laravel相同的方式使用blade视图模板

// index.blade.php

@extends('template.layout')

@section('content')

<h1>Hallo {{ $user->name }}</h1>

@endsection

示例

// /public/index.php

$blade = new Blade(
    realpath(__DIR__ . '/../resources/views'),
    realpath(__DIR__ . '/../resources/cache')
);

$user = User::find(1);

echo $blade->view('pages.index', compact('user'));


// /resources/views/template/layout.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Title</title>
</head>
<body>

<div class="row">

    <div class="column">

        @yield('content')

    </div>

</div>

</body>
</html>


// /resources/views/pages/index.blade.php

@extends('template.layout')

@section('content')

<h1>Hallo {{ $user->name }}</h1>

@endsection