Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gammamatrix authored Mar 24, 2024
1 parent 392b257 commit ceda74b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 11 deletions.
82 changes: 82 additions & 0 deletions resources/views/components/snippets.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
// dd([
// '__METHOD__' => __METHOD__,
// '__FILE__' => __FILE__,
// '__LINE__' => __LINE__,
// '$snippets' => $snippets,
// ]);
if (empty($snippets) || !is_array($snippets)) {
return;
}
?>
@foreach ($snippets as $snippet)
@continue(empty($snippet['title']) && empty($snippet['content']))
<?php
if (empty($snippet['rank'])) {
$stack = 'snippet-main';
} elseif ($snippet['rank'] >= -3000 && $snippet['rank'] <= -2000) {
$stack = 'snippet-banner';
} elseif ($snippet['rank'] > -2000 && $snippet['rank'] <= -1000) {
$stack = 'snippet-header';
} elseif ($snippet['rank'] > -1000 && $snippet['rank'] < 0) {
$stack = 'snippet-main-header';
} elseif ($snippet['rank'] > 0 && $snippet['rank'] <= 1000) {
$stack = 'snippet-main';
} elseif ($snippet['rank'] > 1000 && $snippet['rank'] <= 2000) {
$stack = 'snippet-content';
} elseif ($snippet['rank'] > 2000 && $snippet['rank'] <= 3000) {
$stack = 'snippet-main-footer';
} elseif ($snippet['rank'] > 3000 && $snippet['rank'] <= 4000) {
$stack = 'snippet-footer-top';
} elseif ($snippet['rank'] > 4000 && $snippet['rank'] <= 5000) {
$stack = 'snippet-footer-bottom';
} else {
$stack = 'snippet-content';
}
$containerClass = isset($snippet['meta']['container']) && isset($snippet['meta']['container']['class']) && is_string($snippet['meta']['container']['class']) ? $snippet['meta']['container']['class'] : 'container-fluid';
$h = isset($snippet['meta']['header']) && isset($snippet['meta']['header']['level']) && is_numeric($snippet['meta']['header']['level']) && in_array((int) $snippet['meta']['header']['level'], [1, 2, 3, 4, 5, 6]) ? (int) $snippet['meta']['header']['level'] : 3;
$withTitle = true;
if (! empty($snippet['snippet_type'])
&& is_string($snippet['snippet_type'])
&& in_array($snippet['snippet_type'], [
'banner',
'header',
'footer',
])) {
$withTitle = false;
}
$attributes = trim(implode(' ', array_filter([
sprintf('data-id="%1$s"', $snippet['id'] ?? ''),
sprintf('data-created_at="%1$s"', $snippet['created_at'] ?? ''),
sprintf('data-updated_at="%1$s"', $snippet['updated_at'] ?? ''),
sprintf('data-published_at="%1$s"', $snippet['published_at'] ?? ''),
sprintf('data-published="%1$s"', $snippet['published'] ?? ''),
sprintf('data-allow_public="%1$s"', $snippet['allow_public'] ?? 0),
sprintf('data-allow_guest="%1$s"', $snippet['allow_guest'] ?? 0),
sprintf('data-allow_admin="%1$s"', $snippet['allow_admin'] ?? 0),
sprintf('data-rank="%1$s"', $snippet['rank'] ?? ''),
])));
?>
@push($stack)
<div class="{{ $containerClass }}" {!! $attributes !!}>

<div class="snippet">

@if ($withTitle && !empty($snippet['title']))
<?= sprintf('<h%d>', $h) ?>{{ $snippet['title'] }}<?= sprintf('</h%d>', $h) ?>
@endif

@if (!empty($snippet['content']))
<div class="snippet-content">
{!! $snippet['content'] !!}
</div>
@endif

</div>

</div>
@endpush
@endforeach
34 changes: 24 additions & 10 deletions resources/views/layouts/bootstrap.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
/**
* @var boolean $withSnippets Show the snippets in the layout.
*/
$withSnippets = isset($withSnippets) && is_bool($withSnippets) ? $withSnippets : false;
$withSnippets = isset($withSnippets) && is_bool($withSnippets) ? $withSnippets : true;
/**
* @var boolean $withVue Enable Vue JS
Expand All @@ -103,15 +103,15 @@

<title>{{ !empty($appName) ? sprintf('%1$s: ', $appName) : '' }}@yield('title')</title>

@foreach (Playground\Blade\Facades\Ui::headAssets($theme) as $asset_slug => $asset)
@if (!$withEditor)
@continue(in_array($asset_slug, ['ckeditor', 'ckeditor-style', 'ckeditor-bootstrap']))
@endif
@if (!$withVue)
@continue(in_array($asset_slug, ['vue']))
@endif
{!! $asset !!}
@endforeach
@foreach (Playground\Blade\Facades\Ui::headAssets($theme) as $asset_slug => $asset)
@if (!$withEditor)
@continue(in_array($asset_slug, ['ckeditor', 'ckeditor-style', 'ckeditor-bootstrap']))
@endif
@if (!$withVue)
@continue(in_array($asset_slug, ['vue']))
@endif
{!! $asset !!}
@endforeach

@stack('scripts')
@yield('head')
Expand All @@ -127,8 +127,16 @@

<body class="{{ $withBodyClass }}" {!! $theme->bodyStyle() !!}>

@if ($withSnippets && !empty($snippets))
<x-playground::snippets :snippets="$snippets" />
@endif

@stack('snippet-banner') {{-- snippet-banner rank: {-3000, -2000} --}}

@yield('pre-header')

@stack('snippet-header') {{-- snippet-header rank: {-1999, -1000} --}}

@yield('header')

@yield('pre-nav')
Expand All @@ -154,12 +162,18 @@
@yield('pre-main')

<main role="main" class="{{ $withMainClass }}">
@stack('snippet-main-header') {{-- snippet-main rank === {-999, 0} --}}
@yield('breadcrumbs')
@includeWhen($withAlerts, sprintf('%1$slayouts/bootstrap/alerts', $package_config['view']))
@includeWhen($withErrors, sprintf('%1$slayouts/bootstrap/errors', $package_config['view']))
@yield('main')
@stack('snippet-main') {{-- snippet-main rank === {0, 1000} --}}
@yield('content')
@stack('snippet-content') {{-- snippet-content rank === {1001, 2000}, {rank < -3000 || rank > 5000} --}}
@yield('content-end')
@stack('snippet-main-footer') {{-- snippet-main rank === {2001, 3000} --}}
{{-- snippet-footer-top rank === {3001, 4000} --}}
{{-- snippet-footer-bottom rank === {4001, 5000} --}}
</main>

@yield('pre-footer')
Expand Down
3 changes: 2 additions & 1 deletion resources/views/layouts/bootstrap/footer.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<footer class="mt-auto py-3">
@stack('snippet-footer-top') {{-- snippet-footer rank === {3001, 4000} --}}
<nav class="nav nav-pills flex-column flex-sm-row">
<span class="flex-sm-fill text-center navbar-text">
{{ config('app.name') }}
Expand All @@ -19,5 +20,5 @@
@endauth
@endif
</nav>
<!-- @stack('snippet-footer') {{-- snippet-main rank === 200 --}} -->
@stack('snippet-footer-bottom') {{-- snippet-footer rank === {4001, 5000} --}}
</footer>
35 changes: 35 additions & 0 deletions src/View/Components/Snippets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);
/**
* Playground
*/
namespace Playground\Blade\View\Components;

use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

/**
* \Playground\Blade\View\Components\Snippets
*/
class Snippets extends Component
{
public function __construct(
/**
* @var array<int, array<string, mixed>> $snippets
*/
public array $snippets = [],
) {
}

public function render(): Factory|View
{
$prefix = config('playground-blade.view');

return view(sprintf(
'%1$scomponents.snippets',
is_string($prefix) ? $prefix : ''
));
}
}

0 comments on commit ceda74b

Please sign in to comment.