添加块级修改hook: wrapperhook

This commit is contained in:
Edward Yang 2023-02-02 15:45:43 +08:00
parent 59f47ec84c
commit 59da252a18
4 changed files with 59 additions and 42 deletions

View File

@ -8,11 +8,6 @@ use Illuminate\Support\ServiceProvider;
class HookServiceProvider extends ServiceProvider
{
public function boot()
{
$this->bootDirectives();
}
public function register()
{
$this->commands([
@ -24,18 +19,26 @@ class HookServiceProvider extends ServiceProvider
});
}
protected function bootDirectives()
public function boot()
{
$this->bootHookDirectives();
$this->bootWrapperHookDirectives();
}
/**
* 添加 blade hook 标签, 不需要 @endhook
* @hook('xxx'), 添加 hook 直接输出到页面
*/
protected function bootHookDirectives()
{
Blade::directive('hook', function ($parameter) {
$parameter = trim($parameter, '()');
$parameter = trim($parameter, '()');
$parameters = explode(',', $parameter);
$name = trim($parameters[0], "'");
// $parameters[1] => bool => is this wrapper component?
if (!isset($parameters[1])) {
return ' <'.'?php
return ' <?php
$__definedVars = (get_defined_vars()["__data"]);
if (empty($__definedVars))
{
@ -44,17 +47,31 @@ class HookServiceProvider extends ServiceProvider
$output = \Hook::get("' . $name . '",["data"=>$__definedVars],function($data) { return null; });
if ($output)
echo $output;
?'.'>';
} else {
return ' <'.'?php
$__hook_name="'. $name .'";
?>';
});
}
/**
* 添加 blade wrapper hook 标签
*
* @wrapperhook('xxx') --- @endwrapperhook, 将某段代码打包输出再添加 hook 输出
*/
protected function bootWrapperHookDirectives()
{
Blade::directive('wrapperhook', function ($parameter) {
$parameter = trim($parameter, '()');
$parameters = explode(',', $parameter);
$name = trim($parameters[0], "'");
return ' <?php
$__hook_name="' . $name . '";
ob_start();
?'.'>';
}
?>';
});
Blade::directive('endhook', function ($parameter) {
return ' <'.'?php
Blade::directive('endwrapperhook', function () {
return ' <?php
$__definedVars = (get_defined_vars()["__data"]);
if (empty($__definedVars))
{
@ -66,7 +83,7 @@ class HookServiceProvider extends ServiceProvider
unset($__hook_content);
if ($output)
echo $output;
?'.'>';
?>';
});
}
}

View File

@ -76,12 +76,12 @@
{{-- <x-admin-form-input name="video" title="视频" :value="old('video', $product->video ?? '')" /> --}}
<x-admin-form-input name="position" :title="__('common.sort_order')" :value="old('position', $product->position ?? '0')" />
@hook('admin.product.edit.brand', true)
@wrapperhook('admin.product.edit.brand')
<x-admin::form.row :title="__('admin/brand.index')">
<input type="text" value="{{ $product->brand->name ?? '' }}" id="brand-autocomplete" class="form-control wp-400 " />
<input type="hidden" name="brand_id" value="{{ old('brand_id', $product->brand_id ?? '') }}" />
</x-admin::form.row>
@endhook
@endwrapperhook
<x-admin-form-select :title="__('admin/tax_class.index')" name="tax_class_id" :value="old('tax_class_id', $product->tax_class_id ?? '')" :options="$tax_classes" key="id" label="title" />
<x-admin-form-switch name="active" :title="__('common.status')" :value="old('active', $product->active ?? 1)" />

View File

@ -2,7 +2,7 @@
<div class="top-wrap">
<div class="container d-flex justify-content-between align-items-center">
<div class="left d-flex align-items-center">
@hook('header.top.currency', true)
@wrapperhook('header.top.currency')
<div class="dropdown">
<a class="btn dropdown-toggle ps-0" href="javascript:void(0)" role="button" id="currency-dropdown" data-toggle="dropdown"
aria-expanded="false">
@ -34,9 +34,9 @@
@endforeach
</div>
</div>
@endhook
@endwrapperhook
@hook('header.top.language', true)
@wrapperhook('header.top.language')
<div class="dropdown">
<a class="btn dropdown-toggle" href="javascript:void(0)" role="button" id="language-dropdown" data-toggle="dropdown"
aria-expanded="false">
@ -51,14 +51,14 @@
@endforeach
</div>
</div>
@endhook
@endwrapperhook
</div>
@if (system_setting('base.telephone', ''))
<div class="right nav">
@hook('header.top.telephone', true)
@wrapperhook('header.top.telephone')
<span class="px-2"><i class="bi bi-telephone-forward me-2"></i> {{ system_setting('base.telephone') }}</span>
@endhook
@endwrapperhook
</div>
@endif
</div>
@ -66,11 +66,11 @@
<div class="header-content d-none d-lg-block py-3">
<div class="container navbar-expand-lg">
@hook('header.menu.logo', true)
@wrapperhook('header.menu.logo')
<div class="logo"><a href="{{ shop_route('home.index') }}">
<img src="{{ image_origin(system_setting('base.logo')) }}" class="img-fluid"></a>
</div>
@endhook
@endwrapperhook
<div class="menu-wrap">
@if (!is_mobile())
@include('shared.menu-pc')
@ -78,7 +78,7 @@
</div>
<div class="right-btn">
<ul class="navbar-nav flex-row">
@hook('header.menu.icon', true)
@wrapperhook('header.menu.icon')
<li class="nav-item"><a href="#offcanvas-search-top" data-bs-toggle="offcanvas"
aria-controls="offcanvasExample" class="nav-link"><i class="iconfont">&#xe8d6;</i></a></li>
<li class="nav-item"><a href="{{ shop_route('account.wishlist.index') }}" class="nav-link"><i
@ -110,7 +110,7 @@
@endauth
</ul>
</li>
@endhook
@endwrapperhook
<li class="nav-item">
<a class="nav-link position-relative" {{ !equal_route('shop.carts.index') ? 'data-bs-toggle=offcanvas' : '' }}
href="{{ !equal_route('shop.carts.index') ? '#offcanvas-right-cart' : 'javascript:void(0);' }}" role="button"

View File

@ -52,9 +52,9 @@
<div class="col-12 col-lg-6">
<div class="peoduct-info">
@hook('product.detail.name', true)
@wrapperhook('product.detail.name')
<h1 class="mb-4 product-name">{{ $product['name'] }}</h1>
@endhook
@endwrapperhook
<div class="price-wrap d-flex align-items-end">
<div class="new-price fs-1 lh-1 fw-bold me-2">@{{ product.price_format }}</div>
<div class="old-price text-muted text-decoration-line-through" v-if="product.price != product.origin_price && product.origin_price !== 0">
@ -62,7 +62,7 @@
</div>
</div>
<div class="stock-and-sku mb-4">
@hook('product.detail.quantity', true)
@wrapperhook('product.detail.quantity')
<div class="d-flex">
<span class="title text-muted">{{ __('product.quantity') }}:</span>
<div :class="product.quantity > 0 ? 'text-success' : 'text-secondary'">
@ -70,24 +70,24 @@
<template v-else>{{ __('shop/products.out_stock') }}</template>
</div>
</div>
@endhook
@endwrapperhook
@if ($product['brand_id'])
@hook('product.detail.brand', true)
@wrapperhook('product.detail.brand')
<div class="d-flex">
<span class="title text-muted">{{ __('product.brand') }}:</span>
<a href="{{ shop_route('brands.show', $product['brand_id']) }}">{{ $product['brand_name'] }}</a>
</div>
@endhook
@endwrapperhook
@endif
@hook('product.detail.sku', true)
@wrapperhook('product.detail.sku')
<div class="d-flex"><span class="title text-muted">SKU:</span>@{{ product.sku }}</div>
@endhook
@endwrapperhook
@hook('product.detail.model', true)
@wrapperhook('product.detail.model')
<div class="d-flex" v-if="product.model"><span class="title text-muted">{{ __('shop/products.model') }}:</span> @{{ product.model }}</div>
@endhook
@endwrapperhook
</div>
@if (0)
<div class="rating-wrap d-flex">