HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/be.naniguide.com/app/Library/BuilderJSHelper.php
<?php

namespace App\Library;

class BuilderJSHelper
{
    /**
     * Read all template HTML files from a theme directory and return as an associative array.
     * Keys are template names (e.g. "Page", "Block"), values are the raw HTML content.
     * This eliminates the need for the browser to fetch templates one by one at runtime.
     *
     * @param string $theme Theme name (e.g. "default")
     * @return array<string, string> Template name => HTML content
     */
    public static function loadTemplates(string $theme = 'default'): array
    {
        $themePath = resource_path('themes/' . $theme);
        $indexFile = $themePath . '/index.json';

        if (!file_exists($indexFile)) {
            return [];
        }

        $configData = json_decode(file_get_contents($indexFile));
        $allNames = array_merge($configData->pages ?? [], $configData->templates ?? []);

        $templates = [];
        foreach ($allNames as $name) {
            $file = $themePath . '/' . $name . '.template.html';
            if (file_exists($file)) {
                $templates[$name] = file_get_contents($file);
            }
        }

        return $templates;
    }
}