Implement dynamic menu template rendering: Updated MenuService to retrieve and render menu templates from Settings, integrating menu structure with dynamic HTML content. Enhanced documentation to include setup instructions for the menu template system, detailing creation, usage, and caching mechanisms.

This commit is contained in:
Ümit Tunç
2025-11-03 15:54:03 -03:00
parent 4a2a66c55c
commit ef1e8e8d5c
2 changed files with 107 additions and 14 deletions
+17 -10
View File
@@ -3,6 +3,8 @@
namespace App\Services;
use App\Models\Page;
use App\Models\MenuTemplate;
use App\Models\Setting;
use Illuminate\Support\Facades\Cache;
class MenuService
@@ -10,10 +12,9 @@ class MenuService
/**
* Render menu structure as HTML
*
* @param array|null $menuTemplate Optional menu template data
* @return string Rendered menu HTML
*/
public static function render(?array $menuTemplate = null): string
public static function render(): string
{
// Get menu items from Pages
$menuItems = Cache::remember('menu_items', 3600, function () {
@@ -31,15 +32,21 @@ class MenuService
->get();
});
// If menu template is provided, use it to wrap the menu
if ($menuTemplate && !empty($menuTemplate['html_content'])) {
$html = $menuTemplate['html_content'];
// Get menu template from Settings
$menuTemplateId = Setting::get('menu_template_id');
if ($menuTemplateId) {
$menuTemplate = MenuTemplate::where('is_active', true)->find($menuTemplateId);
// Replace {menu} placeholder with rendered menu structure
$renderedMenu = self::buildMenuStructure($menuItems);
$html = str_replace('{menu}', $renderedMenu, $html);
return $html;
if ($menuTemplate && !empty($menuTemplate->html_content)) {
$html = $menuTemplate->html_content;
// Replace {menu} placeholder with rendered menu structure
$renderedMenu = self::buildMenuStructure($menuItems);
$html = str_replace('{menu}', $renderedMenu, $html);
return $html;
}
}
// Otherwise, return default menu structure