Przeglądaj źródła

view extends & injects

Alex Shabalin 3 tygodni temu
rodzic
commit
c42d75c4bf

+ 14 - 0
app/Services/CustomService.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace App\Services;
+
+class CustomService
+{
+	public function getSmthUsefull()
+	{
+		$data = [
+			'content' => 'Some very <u>precious</u> info!',
+		];
+		return view('widgets.custom', $data);
+	}
+}

+ 12 - 14
resources/views/hello.blade.php

@@ -1,18 +1,20 @@
-<!DOCTYPE html>
-<html>
-<head>
-	<meta charset="utf-8">
-	<meta name="viewport" content="width=device-width, initial-scale=1">
-	<title>Hello</title>
+@extends('layouts.users')
+
+@inject('custom', 'App\Services\CustomService')
+
+@section('title', 'Custom title for hello page')
+
+@push('css')
 	<style type="text/css">
 		.vip {
 			color: red;
 		}
 	</style>
-</head>
-<body>
+@endpush
 
-	@include('partials.header')
+@section('content')
+
+	{{ $custom->getSmthUsefull() }}
 
 	<h1>Hello</h1>
 
@@ -24,8 +26,4 @@
 		</li>
 	@endforeach
 	</ul>
-
-	@include('partials.footer')
-
-</body>
-</html>
+@endsection

+ 5 - 10
resources/views/home.blade.php

@@ -1,13 +1,8 @@
-<!DOCTYPE html>
-<html>
-<head>
-	<meta charset="utf-8">
-	<meta name="viewport" content="width=device-width, initial-scale=1">
-	<title>Welcome to my site!</title>
-</head>
-<body>
+@extends('layouts.base')
 
+@section('title', 'Welcome to my site!')
+
+@section('content')
 	<h1>Welcome to my site!</h1>
+@endsection
 
-</body>
-</html>

+ 21 - 0
resources/views/layouts/base.blade.php

@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+<head>
+	<meta charset="utf-8">
+	<meta name="viewport" content="width=device-width, initial-scale=1">
+	<title>@yield('title')</title>
+	<meta name="description" content="@yield('description')">
+	@stack('css')
+</head>
+<body>
+
+	@include('partials.header')
+
+	<p>This is base layout</p>
+
+	@yield('content')
+
+	@include('partials.footer')
+
+</body>
+</html>

+ 9 - 0
resources/views/layouts/users.blade.php

@@ -0,0 +1,9 @@
+@extends('layouts.base')
+
+@push('css')
+<style>
+h1 {
+	color: orange;
+}
+</style>
+@endpush

+ 3 - 0
resources/views/widgets/custom.blade.php

@@ -0,0 +1,3 @@
+<hr>
+<b>{{ $content }}</b>
+<hr>